#!/usr/bin/bash

# Colors
RED='\033[0;31m'
LRED="\033[1;31m"
BLUE="\033[0;34m"
LBLUE="\033[1;34m"
GREEN="\033[0;32m"
LGREEN="\033[1;32m"
YELLOW="\033[1;33m"
CYAN="\033[0;36m"
LCYAN="\033[1;36m"
PURPLE="\033[0;35m"
LPURPLE="\033[1;35m"
BWHITE="\e[1m"
NC='\033[0m' # No Color

# Environment Vars
adminCmd="su -c"
packageRepoUrl="https://github.com/void-linux/void-packages.git"
packageRepoDir="/home/${USER}/.repos"
repoPath="${packageRepoDir}/void-packages"
package="$2"
autoPull="False"
debugOn="False"

function devInfo(){
    local gitBinPath=$(which git)
    printf "${LPURPLE}Git binary path: ${LCYAN}${gitBinPath}${NC}\n"
    printf "${LPURPLE}Repo Storage Directory: ${LCYAN}${packageRepoDir}${NC}\n"
    printf "${LPURPLE}void-packages location: ${LCYAN}${repoPath}${NC}\n"
    printf "${LPURPLE}Restricted settings config: ${LCYAN}${repoPath}/etc/conf${NC}\n\n"

}


function checkSetup() {
# Path & file checks
    printf "${LCYAN}Running environment requirement checks...${NC}\n"
    # Check Dependencies and handle admin rights method
    
    local setAllowRestricted="XBPS_ALLOW_RESTRICTED=yes"
    local checkAllowRestricted=$(cat ${repoPath}/etc/conf)
    

    if [ -f /usr/bin/sudo ];then
        adminCmd="sudo"
        printf "${YELLOW}Setting admin command to 'sudo'...${NC}\n"
    fi

    if [ ! -f /usr/bin/git ];then

        if [ "${adminCmd}" = "su -c" ];then
            printf "${LRED}Enter password for user 'root'${NC}\n"
            $adminCmd 'xbps-install -S git'
    
        else
            $adminCmd xbps-install -S git
        fi

        if [ ! -f /usr/bin/git ];then
            printf "${LPURPLE}Git installed: ${LRED}[check failed] ${NC}\n"
            gitCheck="Failed"
        fi
    
    elif [ -f /usr/bin/git ];then
        printf "${LPURPLE}Git installed: ${LGREEN}[check passed]...${NC}\n"
        gitCheck="Passed"
        
    fi

    # Check for the default repo storage location and create it if we need to

    if [ ! -d ${packageRepoDir} ];then
        mkdir ${packageRepoDir}

        if [ ! -d ${packageRepoDir} ];then
            printf "${LRED}${packageRepoDir} failed to be created, please check permissions...[check failed]"
            repoStorageCheck="Failed"
        fi

    elif [ -d ${packageRepoDir} ];then
        printf "${LPURPLE}Repo storage directory: ${LGREEN}[check passed]...${NC}\n"
        repoStorageCheck="Passed"
    fi

    # Get a copy of void-packages if it doesn't already exist
    if [ ! -d ${repoPath} ];then
        git clone ${packageRepoUrl} ${packageRepoDir}/void-packages

       if [ ! -d ${repoPath} ];then
            printf "${LRED}void-packages could not be created in ${packageRepoDir}, check permissions...[check failed]${NC}\n"
            repoCloneCheck="Failed"
        fi

    elif [ -d ${repoPath} ];then
        printf "${LPURPLE}Repository cloned: ${LGREEN}[check passed]...${NC}\n"
        repoCloneCheck="Passed"
    fi

    if [ "${checkAllowRestricted}" != "${setAllowRestricted}" ];then
        echo "${setAllowRestricted}" > ${repoPath}/etc/conf
        
        if [ "${checkAllowRestricted}" != "${setAllowRestricted}" ];then
            printf "${LRED} Could enable restricted software building...[check failed]${NC}\n"
            restrictedCheck="Failed"
        fi

    elif [ "${checkAllowRestricted}" = "${setAllowRestricted}" ];then
        printf "${LPURPLE}Enable restricted software building: ${LGREEN}[check passed]${NC}\n"
        restrictedCheck="Passed"
    fi

    
    if [ "${checkBootstrap}" = "" ];then
        local checkBootstrap=$(${repoPath}/xbps-src list)
        ${repoPath}/xbps-src binary-bootstrap
        local checkBootstrap=$(${repoPath}/xbps-src list)
    
        if [ "${checkBootstrap}" = "" ];then
            printf "${LPURPLE} Binary bootstrap ${LRED}[Check Failed]${NC}\n"
            bootstrapCheck="Failed"

        elif [ "${checkBootstrap}" != "" ];then
        printf "${LPURPLE}Chroot Bootstrap: ${LGREEN}[check passed]...${NC}\n"
        bootstrapCheck="Passed"

        fi

    elif [ "${checkBootstrap}" != "" ];then
        ${repoPath}/xbps-src bootstrap-update
        printf "${LPURPLE}Chroot Bootstrap: ${LGREEN}[check passed]...${NC}\n"
        bootstrapCheck="Passed"

    
    fi

    if [ "${bootstrapCheck}" = "Passed" ] && [ "${gitCheck}" = "Passed" ] && [ "${repoStorageCheck}" = "Passed" ] && [ "${repoCloneCheck}" = "Passed" ] && [ "${restrictedCheck}" = "Passed" ];then
        allChecksPassed="True"

    else
        
        allChecksPassed="False"
        if [ "${debugOn}" = "True" ];then

            printf "\n${YELLOW}** DEBUG INFO **${NC}\n"
            devInfo
            read -n 1 -p "[ PRESS ANY KEY TO CONTINUE... ] "
        fi
    fi

    if [ "${allChecksPassed}" = "True" ];then
        printf "${LGREEN}All checks passed!${NC}\n"
        
        if [ "${debugOn}" = "True" ];then
            printf "\n${YELLOW}** DEBUG INFO **${NC}\n"
            devInfo
            read -n 1 -p "[ PRESS ANY KEY TO CONTINUE... ] "
        fi

    elif [ "${allChecksPassed}" = "False" ];then
        printf "${LRED}Checks failed! Please make sure you have rights to this machine. Otherwise, please submit a bug report.${NC}\n"
        
        if [ "${debugOn}" = "True" ];then
            printf "\n${YELLOW}** DEBUG INFO **${NC}\n"
            devInfo
            read -n 1 -p "[ PRESS ANY KEY TO CONTINUE... ] "
        fi
        printf "${LRED}Exiting due to check failures${NC}\n"
    fi


    
}

search_srcpkgs(){
    ls ${repoPath}/srcpkgs | grep $1
}

case "${1}" in

    install| -i | i )
        ${repoPath}/xbps-src pkg ${package}
        if [ -f /usr/bin/sudo ];then
            adminCmd="sudo"
            printf "${YELLOW}Setting admin command to 'sudo'...${NC}\n"
        fi
        ${adminCmd} xbps-install -R ${repoPath}/hostdir/binpkgs ${package}
    ;;

    install-nonfree|-inf|-inf)
        ${repoPath}/xbps-src pkg ${package}
        if [ -f /usr/bin/sudo ];then
            adminCmd="sudo"
            printf "${YELLOW}Setting admin command to 'sudo'...${NC}\n"
        fi
        ${adminCmd} xbps-install -R ${repoPath}/hostdir/binpkgs/nonfree ${package}
    ;;

    search|-s|s)
        search_srcpkgs "${2}"
    ;;
    
    check | -ck | ck)
        checkSetup
    ;;

    clean | -c | c )
        ${repoPath}/xbps-src clean
    ;;

    zap | -z | z )
        ${repoPath}/xbps-src zap
    ;;

    swoop | -sw | sw )
        checkSetup
        ${repoPath}/xbps-src pkg ${package}
        ${repoPath}/xbps-src clean
        ${repoPath}/xbps-src zap
    ;;


esac
