#!/bin/sh

#-----------------------------------
# Name:     setup
# Version:  1.0.0
# Author:   Solomon Laing
# Description:
# Sets up system with my configs,
# programs, and more.
# Usage:
#   setup yay -- runs the system setup from installing yay onwards
#   setup yay only -- only runs the yay section of the sysetm setup
#-----------------------------------

working_dir=$(pwd)

# perform basic system check
./system-check

# install yay
function install_yay {
    cd $working_dir
    ./install-yay
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

# update the system
function update_system {
    # 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
        [nN][oO] | [nN])
            exit 0
            ;;
        *)
            # Make sure everything is up to date
            echo "Making sure everything is up to date..."
            yay -Syyu --noconfirm
            ;;
    esac
}

# install all required dependencies/programs
function install_dependencies {
    cd $working_dir
    ./install-dependencies
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

# install all default programs
function install_defaults {
    cd $working_dir
    ./install-defaults
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

# install all misc programs
function install_misc {
    cd $working_dir
    ./install-misc
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

function install_dmenu {
    cd $working_dir
    ./install-dmenu-inkletblot
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

function install_dwm {
    cd $working_dir
    ./install-dwm-inkletblot
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

function install_st {
    cd $working_dir
    ./install-st-inkletblot
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

function install_dwmblocks {
    cd $working_dir
    ./install-dwmblocks-inkletblot
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

# install my config
function install_config {
    cd $working_dir
    ./install-config
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

function install_fonts {
    cd $working_dir
    ./install-fonts
    if [ $? -eq 1 ]; then
        exit 1
    fi
}

function help {
    echo "Usage is:"
    echo "  setup <option> -- to start system setup from option"
    echo "  setup <option> only -- to only setup the specified option"
    echo "Options (in order of execution):"
    echo "  yay : Install yay AUR helper"
    echo "  deps : Install the dependencies for my scripts and window manager"
    echo "  defaults : Install my chosen default programs"
    echo "  misc : Install my collection of misc - nice to have - programs"
    echo "  dmenu : Install my customised version of dmenu"
    echo "  dwm : Install my customised version of dwm"
    echo "  st : Install my customised version of st"
    echo "  dwmblocks : Install my customised version of dwmblocks"
    echo "  fonts : install getnf and optionally a font of your choice"
    echo "  dotfiles : Install my dotfiles to the system"
    echo ""
    echo "Note: it is expected that the initial run of this script lets each stage complete sucessfully."
}

if [ "$1" = "help" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    help
    exit 0
fi

update_system

if [ "$2" = "only" ]; then
    case $1 in
        yay)
            install_yay
            ;;
        deps)
            install_dependencies
            ;;
        defaults)
            install_defaults
            ;;
        misc)
            install_misc
            ;;
        dmenu)
            install_dmenu
            ;;
        dwm)
            install_dwm
            ;;
        st)
            install_st
            ;;
        dwmblocks)
            install_dwmblocks
            ;;
        fonts)
            install_fonts
            ;;
        config)
            install_config
            ;;
        *)
            echo "Invalid input..."
            help
            exit 1
            ;;
    esac
else
    case $1 in
        "")
            install_yay
            install_dependencies
            install_defaults
            install_misc
            install_dmenu
            install_dwm
            install_st
            install_dwmblocks
            install_fonts
            install_config
            ;;
        yay)
            install_yay
            install_dependencies
            install_defaults
            install_misc
            install_dmenu
            install_dwm
            install_st
            install_dwmblocks
            install_fonts
            install_config
            ;;
        deps)
            install_dependencies
            install_defaults
            install_misc
            install_dmenu
            install_dwm
            install_st
            install_dwmblocks
            install_fonts
            install_config
            ;;
        defaults)
            install_defaults
            install_misc
            install_dmenu
            install_dwm
            install_st
            install_dwmblocks
            install_fonts
            install_config
            ;;
        misc)
            install_misc
            install_dmenu
            install_dwm
            install_st
            install_dwmblocks
            install_fonts
            install_config
            ;;
        dmenu)
            install_dmenu
            install_dwm
            install_st
            install_dwmblocks
            install_fonts
            install_config
            ;;
        dwm)
            install_dwm
            install_st
            install_dwmblocks
            install_fonts
            install_config
            ;;
        st)
            install_st
            install_dwmblocks
            install_fonts
            install_config
            ;;
        dwmblocks)
            install_dwmblocks
            install_fonts
            install_config
            ;;
        fonts)
            install_fonts
            install_config
            ;;
        dotfiles)
            install_config
            ;;
        *)
            echo "Invalid input..."
            help
            exit 1
            ;;
    esac
fi

if [ $? -eq 1 ]; then
    exit 1
fi

exit 0
