#!/bin/sh

# check user is happy to run this script
echo "This script is going to install all of the programs listed in /home/<user>/default-programs and their dependencies. As such it will ask for root privilages."
read -r -p "Are you sure you want to continue? [y/N] " input
case $input in
[yY][eE][sS] | [yY])
    echo "The program will continue..."
    ;;
[nN][oO] | [nN])
    exit 0
    ;;
*)
    echo "Invalid input..."
    exit 1
    ;;
esac

# if yay is not installed the run the install script
if ! command -v yay &> /dev/null; then
    install-yay
fi

# check with the user they are happy to update, exit if not though.
echo "The system needs to be updated before this installation can happen. The upgrades will be applied without any user oversight which may be dangerous for out of date systems."
read -r -p "Are you sure you want to continue? [y/N] " input
case $input in
[yY][eE][sS] | [yY])
    # Make sure everything is up to date
    echo "Making sure everything is up to date..."
    yay -Syyu --noconfirm
    ;;
[nN][oO] | [nN])
    exit 0
    ;;
*)
    echo "Invalid input..."
    exit 1
    ;;
esac

# run though each default program and install it if it's not already installed
# note that yay, git, steam, dmenu-inkletblot, ly and nvidia related programs are not included
default_programs="/default-programs"
dependencies=$(cat $HOME/$default_programs)
for dependency in $dependencies
do
    if ! command -v $dependency &> /dev/null; then
        echo "Installing $dependency and it's dependencies..."
        yay -S --noconfirm $dependency
    fi
done

# ask the user if they want to install nvidia dependencies, do it or skip
if ! command -v yay &> /dev/null && ! command -v yay &> /dev/null && ! command -v yay &> /dev/null; then
    echo "My computer requires nvidia, as such I install the required drivers and programs"
    read -r -p "Are you sure you want to install? [y/N] " input
    case $input in
    [yY][eE][sS] | [yY])
        echo "Nvidia is being installed..."
        yay -S nvidia nvidia-settings nvidia-prime --noconfirm
        ;;
    [nN][oO] | [nN])
        echo "Nvidia installation has been skipped. Programs: nvidia, nvidia-settings, nvidia-prime"
        ;;
    *)
        echo "Nvidia installation has been skipped. Programs: nvidia, nvidia-settings, nvidia-prime"
        ;;
    esac
fi

# notify about steam issue (multilib)
echo "There are a few programs that can't be automatically installed. The main one of these is Steam which requires multilib to be enabled. Instructions for this can be found at: https://wiki.archlinux.org/index.php/Official_repositories#Enabling_multilib Once this is done steam can be installed."
read -r -p "Press [ENTER] to continue.."

# notify about ly and dmenu-inkletblot scripts
echo "I use two programs that are installed from git directly: ly and dmenu-inkletblot."
echo "I do not have install scripts for both of these. They have dependencies I can't guarantee are installed."
read -r -p "Press [ENTER] to continue.."

# notify user that there may be extra steps to finish install
echo "This script has installed all of my default programs however it may not have configure many things. My dotfiles may cover some of these but things such as gnome-keyring (using the pam method) will need to be implemented. As well as setting up nvidia and xorg. There are likely more that I have forgotton as well."
read -r -p "Press [ENTER] to complete.."

# need scripts for these
# dmenu-inkletblot
# ly
