#!/usr/bin/env bash

cloneAndMake() {
    if [ -z "$password" ]; then
        password=$(whiptail --passwordbox "Enter your password" 8 25 3>&1 1>&2 2>&3)
        echo "$password" | sudo -S bash -c "" > /dev/null 2>&1
    fi
    
    local TERM=ansi; whiptail --infobox "Downloading '${1}'" 7 0
    git clone "$2"

    makeMsg=laptop && [[ -v LAPTOP ]] || makeMsg=install
    (cd "$1" || { echo "Couldn't cd into '$1'." 1>&2 && exit 1; }; echo "$password" | sudo make "$makeMsg")
}

downloadAndInstallPackages() {
    DOTFILES_CONFIG="$HOME/.config"
    cd "$DOTFILES_CONFIG" || { echo "Couldn't cd into '$DOTFILES_CONFIG'." 1>&2 && exit 1; }

    if [[ ! -d "cdwmblocks" ]]; then
        cloneAndMake "cdwmblocks" "https://gitlab.com/slococo/cdwmblocks.git"
    fi
    if [[ ! -d "cdwm" ]]; then
        cloneAndMake "cdwm" "https://gitlab.com/slococo/cdwm.git"
    fi
    if [[ ! -d "cst" ]]; then
        cloneAndMake "cst" "https://gitlab.com/slococo/cst.git"
    fi
    if [[ ! -d "cdmenu" ]]; then
        cloneAndMake "cdmenu" "https://gitlab.com/slococo/cdmenu.git"
    fi
    if [[ ! -d "clipmenu" ]]; then
        cloneAndMake "clipmenu" "https://github.com/cdown/clipmenu.git"
    fi
    if [[ ! -d "cslock" ]]; then
        cloneAndMake "cslock" "https://gitlab.com/slococo/cslock.git"
    fi
}

runScript() {
    downloadAndInstallPackages
}

runScript