#!/bin/bash

case ${1} in

    --set-version)
        version="${2}"
    ;;

    *)
        pkgname="go"
        version="1.9.1"
        distfile="https://storage.googleapis.com/golang/${pkgname}${version}.linux-amd64.tar.gz"
        tmpdir="/tmp"
        installdir="/usr/local"
        checksum=d70eadefce8e160638a9a6db97f7192d8463069ab33138893ad3bf31b0650a79

        # 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


        fetch_(){
        printf "${BWHITE}\nFetching file\n${NC}"
        cd ${tmpdir}
    
        if [ ! -f ${pkgname}${version}.linux-amd64.tar.gz ];then
            wget ${distfile}
        fi
        printf "${BWHITE}\nVerifying checksum\n${NC}"

        if [ ! -f checksum.txt ];then
            echo "${checksum} ${pkgname}${version}.linux-amd64.tar.gz" >> checksum.txt
            sha256sum -c checksum.txt

        else
            sha256sum -c checksum.txt
        fi

        if [ $? = 1 ];then
            exit 0;
        fi
    }

    extract-install_(){
        cd ${tmpdir}
    
        if [ ! -f ${pkgname}${version}.linux-amd64.tar.gz ];then
            echo "No file found to extract"
        else
            printf "${BWHITE}\nExtracting and installing to:${NC} " && printf "${LCYAN}${installdir}\n${NC}"
            printf "${LRED}\nAuthentication for root:${NC}\n" && su -c "tar -C ${installdir} -xzvf ${pkgname}${version}.linux-amd64.tar.gz"
        fi
        
        if [ ! -d /usr/local/go ];then
            echo -e "\nCan not find ${pkgname} in ${installdir}\n"
            exit 0;
        else
            echo -e "${LGREEN}\n${pkgname}-${version} has been installed to ${installdir}${NC}"
        fi
    }

    add-path_(){
        echo -e "\n1. Add gopath to ${HOME}/.profile\n"
        echo -e "\n2. Add gopath to ${HOME}/.bashrc\n"
        read choice

        if [ "${choice}" = "1" ];then
            echo 'export PATH=$PATH:/usr/local/go/bin' >> ${HOME}/.profile
        elif [ "${choice}" = "2" ];then
            printf "${BWHITE}\nAdding gopath to .bashrc${NC}"
            echo 'export PATH=$PATH:/usr/local/go/bin' >> ${HOME}/.bashrc
            catpath=$(cat ${HOME}/.bashrc | grep /usr/local/go/bin)

            ${catpath}

            if [ $? = 1 ];then
            printf "${BWHITE}\n\nRunning grep on .bashrc\n\n${NC}"
            printf "${LCYAN}Grep Results:${NC} ${LGREEN}${catpath}\n\n${NC}"

            printf "${YELLOW}Please edit: ${HOME}/.bashrc:${NC}\n\n"
            printf "${YELLOW}Above are results for grep on .bashrc, if nothing is there then please add:\n\n${NC}${BWHITE}"
            echo -e 'export PATH=$PATH:/usr/local/go/bin' && printf "\n\n${NC}"

            else
                printf "${LGREEN}\n\ngo has been added to your path in ${HOME}/.bashrc\n\n${NC}"
            fi
        fi
    }
    fetch_
    extract-install_
    add-path_
    ;;

    --help | -h)
        printf "${BWHITE}\nUseage ${0} [options] [arguments]\n\n${NC}"

        printf "--set-version       enter in an existing go version to download\n"
        printf "--help, -help       Display this help page"
    ;;

esac