#!/usr/bin/env bash

#-----------------------------------
# Name:     defoults
# Version:  1.0.0
# Author:   Solomon Laing
# Description:
# Installs all of my default programs
#-----------------------------------

clear

default_programs="./default-programs"
dependencies=$(sed -n '/DEFAULTS_START/,/DEFAULTS_END/p' $default_programs | sed -e 's/DEFAULTS_START//' -e 's/DEFAULTS_END//')

# check user is happy to run this script
echo "This script is going to install all of my default programs, e.g.: browser, rss reader, etc. As such it will ask for root privilages."
echo "Defaults:$dependencies"

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

failures=()

# run though each dependency and install it if it's not already installed
for dependency in $dependencies; do
    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\n")
    fi
done

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

if [ $total_failures -gt 0 ]; then
    echo "$total_failures of the programs failed to install..."
    echo -e "${failures[@]}"
    exit 1
else
    echo "Default programs sucessfully installed"
fi

exit 0
