#!/bin/sh

#-----------------------------------
# Name:     install-yay
# Version:  1.0.0
# Author:   Solomon Laing
# Description:
# Installs the AUR helper yay on the
# system.
#-----------------------------------

clear

default_programs="./default-programs"
dependencies=$(sed -n '/DEPENDENCIES_START/,/DEPENDENCIES_END/p' $default_programs | sed -e 's/DEPENDENCIES_START//' -e 's/DEPENDENCIES_END//')
laptop_dependencies=$(sed -n '/DEPENDENCIES_LAPTOP_START/,/DEPENDENCIES_LAPTOP_END/p' $default_programs | sed -e 's/DEPENDENCIES_LAPTOP_START//' -e 's/DEPENDENCIES_LAPTOP_END//')
laptop=0

# check user is happy to run this script
echo "This script is going to install all of the programs required for my scripts and dotfiles to function in a bisic manner. As such it will ask for root privilages."
echo "Dependencies:$dependencies"

echo ""

echo "If you are installing this on a laptop there are a couple of laptop specific dependencies that need to be installed as well..."
read -r -p "Would you like to install these? [y/N] " laptop_input
case $laptop_input in
[yY][eE][sS] | [yY])
    echo "The extra dependencies will be installed..."
    laptop=1
    ;;
[nN][oO] | [nN])
    echo "Not installing extra dependencies..."
    ;;
*)
    echo "Invalid input... Not installing extra dependencies..."
    ;;
esac

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

failures=()

# run though each dependency and install it if it's not already installed
for dependency in $dependencies
do
    clear
    sudo -v
    if ! yay -Qi $dependency &> /dev/null; then #TODO: check whether this works as I'm expecting...
        echo "Installing $dependency and it's dependencies..."
        yay -S --noconfirm $dependency
    fi

    if ! yay -Qi $dependency &> /dev/null; then
        # there are likely some dependencies that won't have an associated command
        # TODO: find a better way to deal with this, eg have something like xorg COMMAND xprop in default programs

        # just catch everything, ik there are better ways to do this
        case $dependency in
            xorg)
                echo "xorg is has no command."
            ;;
            *)
                failures+=("$dependency")
            ;;
        esac
    fi
done

if [ $laptop -eq 1 ]; then
    # run though each laptop dependency and install it if it's not already installed
    for dependency in $laptop_dependencies
    do
        clear
        sudo -v
        if ! yay -Qi $dependency &> /dev/null; then
            echo "Installing $dependency and it's dependencies..."
            yay -S --noconfirm $dependency
        fi

        if ! yay -Qi $dependency &> /dev/null; then
            failures+=("$dependency")
        fi
    done
fi

total_failures=$((${#failures[@]}))

if [ $total_failures -gt 0 ]; then
    echo "$total_failures of the programs failed to install..."
    echo -e "$failures"
    echo "Please manually rectify this and then rerun this setup with: ./setup deps"
    exit 1
else
    echo "Dependencies sucessfully installed"
fi

exit 0
