#!/bin/bash

set -euo pipefail

# Function to log messages
log() {
	echo -e "\n--- $1 ---\n"
}

distro=""
uname_val=$(uname)

if [ "$uname_val" = "Darwin" ]; then
	distro="mac"
elif [ -f /etc/os-release ]; then
	source /etc/os-release
	distro=$ID
else
	echo 'Your OS is unsupported. Supported: Arch, Debian, Fedora, Mac (Brew)'
	exit 1
fi

# Base packages array
base_pkgs=(
	git
	neovim
	tmux
	rsync
	curl
	less
	fzf
	mosh
	lsd
	ripgrep
	bat
	tree-sitter-cli
	zoxide
	git-delta
	stow
)

# OS specific arrays
mac_pkgs=("${base_pkgs[@]}" fd yazi)
arch_pkgs=("${base_pkgs[@]}" fd yazi ttf-nerd-fonts-symbols ttf-nerd-fonts-symbols-common ttf-nerd-fonts-symbols-mono)
debian_pkgs=("${base_pkgs[@]}" fd-find extrepo)
fedora_pkgs=("${base_pkgs[@]}" fd-find)

case "$distro" in
mac)
	log 'Installing packages with Homebrew'
	brew install "${mac_pkgs[@]}"
	;;
arch)
	log 'Installing yay'

	sudo pacman --sync --refresh --sysupgrade --noconfirm
	sudo pacman --sync --needed --noconfirm base-devel go bat

	if [ ! -d "yay" ]; then
		git clone https://aur.archlinux.org/yay.git
	fi

	cd yay
	bat PKGBUILD
	read -p 'Continue? (y/N): ' ready
	if [[ "$ready" =~ ^[yY] ]]; then
		makepkg --syncdeps --install --noconfirm
	else
		echo "Installation aborted."
		exit 1
	fi
	cd ..

	log 'Installing packages with yay'
	yay --sync --noconfirm "${arch_pkgs[@]}"
	;;
debian | ubuntu)
	log 'Installing packages with APT'
	sudo apt-get --quiet --yes update
	sudo apt-get --quiet --yes upgrade
	sudo apt-get --quiet --yes install "${debian_pkgs[@]}"
	;;
fedora | rhel | rocky | almalinux | centos)
	log 'Installing packages with DNF'
	sudo dnf upgrade --assumeyes
	sudo dnf install --assumeyes --skip-broken "${fedora_pkgs[@]}"
	;;
*)
	echo 'Your OS is unsupported. Supported: Arch, Debian, Fedora, Mac (Brew)'
	exit 1
	;;
esac
