#!/usr/bin/bash

DIR=$(dirname $(realpath "$0"))

if [[ -f /etc/os-release && $1 == "packages" ]]; then
	. /etc/os-release

	distro=${NAME,,}

		
	if [[ $distro == "ubuntu" ]]; then
		export DEBIAN_FRONTEND=noninteractive

		sudo apt-get update && sudo apt-get -y install --no-install-recommends wget curl


	  # install pfetch
		curl -s https://raw.githubusercontent.com/dylanaraps/pfetch/master/pfetch > "$HOME/.local/bin/pfetch"
		chmod +x "$HOME/.local/bin/pfetch"
		
		
		# add google chrome repository
		curl -s https://dl-ssl.google.com/linux/linux_signing_key.pub > /tmp/google.pub
		gpg --no-default-keyring --keyring /etc/apt/keyrings/google-chrome.gpg --import /tmp/google.pub
		echo 'deb [arch=amd64 signed-by=/etc/apt/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list

		sudo add-apt-repository ppa:maveonair/helix-editor
		
		sudo apt-get update && sudo apt-get -y install --no-install-recommends \
				fish grim slurp wl-clipboard sway swaylock swaybg alacritty rofi \
				light network-manager network-manager-openvpn google-chrome-stable xournalpp \
				php python3 helix openjdk-21-jdk openjdk-21-jre gdm3 gnome seahorse

		sudo systemctl enable gdm3
		sudo systemctl enable NetworkManager

		sudo apt-get upgrade -y
				
	elif [[ $distro == "arch linux" ]]; then
		sudo pacman -Sy --noconfirm

		sudo pacman -S git base-devel curl wget --noconfirm

		# install yay AUR helper
		git clone https://aur.archlinux.org/yay.git /tmp/yay
		cd /tmp/yay && makepkg -si --noconfirm
		yay -Sy --noconfirm

		sudo pacman --noconfirm -S \
				fish grim slurp wl-clipboard sway swaylock swaybg alacritty rofi \
				networkmanager networkmanager-openvpn xournalpp \
				php python helix jdk-openjdk docker docker-buildx gdm gnome seahorse

		yay -S google-chrome light snapd --noconfirm

		sudo ln -s /var/lib/snapd/snap /snap
		sudo systemctl enable snapd.socket

		sudo systemctl enable gdm
		sudo systemctl enable NetworkManager
		
		yay --noconfirm
		
	fi

	sudo usermod -aG video $USER
	sudo usermod -aG docker $USER
fi

cd $DIR

# install config files
for filename in $(ls -p | grep -v "install" | grep -v "/" | grep -v "README.md"); do
	app=$(echo $filename | tr "-" "\n" | head -1)
	appdir="$HOME/.config/$app"
	mkdir -p $appdir
	cp $filename "$appdir/${filename:${#app}+1:200}"
done

# install backrounds
mkdir -p "$HOME/.local/share/wallpapers"
cp ./backgrounds/* "$HOME/.local/share/wallpapers/"

mkdir -p "$HOME/.local/bin"

# install pfetch
curl -s https://raw.githubusercontent.com/dylanaraps/pfetch/master/pfetch > "$HOME/.local/bin/pfetch"
chmod +x "$HOME/.local/bin/pfetch"

# provide rickroll
curl -s https://raw.githubusercontent.com/keroserene/rickrollrc/master/roll.sh > "$HOME/.local/bin/rickroll"
chmod +x "$HOME/.local/bin/rickroll"


