#!/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






# Wine Prefix Environment
PREFIX="/home/$USER/diablo"
ARCH="win32"


notice(){
    echo -e "${YELLOW}NOTICE: Currently you must move & rename the installer directories to the following: ${NC}\n"
    echo -e "${LCYAN}Base Game: ${HOME}/D2${NC}" 
    echo -e "${LCYAN}Expansion: ${HOME}/LOD${NC}"
    echo -e "${YELLOW}\nThen run this script with the following option:${NC}"
    echo -e "${LCYAN}${0} -a${NC}"
    echo -e "${YELLOW}\nThe script will then kick off the process of grabbing debs, setting the prefix and installing the game.${NC}"
    echo -e "${YELLOW}User intervention is required to click through the installer setups${NC}"
}

installD2(){
    notice
    read -n 1 -p "~~~~~~ Press enter to continue ~~~~~~"

    D2_INSTALLER_PATH="/home/$USER/D2"

    echo -e "\n${LGREEN}Installing Diablo II...${NC}\n"

    if [ ! -d "${D2_INSTALLER_PATH}" ];then
        echo -e "${D2_INSTALLER_PATH} not found, edit script and set correct path\n"
        exit 0;
    fi

    cd "${D2_INSTALLER_PATH}" || exit 1

    WINEPREFIX="${PREFIX}" WINEARCH="${ARCH}" wine Installer.exe

}

installLOD(){

    notice
    read -n 1 -p "~~~~~~ Press enter to continue ~~~~~~"

    LOD_INSTALLER_PATH="/home/$USER/LOD"

    echo -e "\nInstalling Diablo II: Lord of Destruction...\n"

    if [ ! -d "${LOD_INSTALLER_PATH}" ];then
        echo -e "${LOD_INSTALLER_PATH} not found, edit script and set correct path\n"
        exit 0;
    fi

    cd "${LOD_INSTALLER_PATH}" || exit 1

    WINEPREFIX="${PREFIX}" WINEARCH="${ARCH}" wine Installer.exe
}

getDeps(){
    if [ ! -f /usr/bin/wine ];then
        echo -e "\nInstall wine\n"
        exit 0;
    fi

    if [ ! -f /usr/bin/winetricks ];then
        echo -e "\n Install winetricks\n"
        exit 0;
    fi
}

getTricks(){
    WINEPREFIX="${PREFIX}" WINEARCH="${ARCH}" winetricks directx9 directplay comctl32ocx

    echo -e "\nGetting Glide for D2\n"

    

    
}

getGlide(){
    if [ ! -f /tmp/gl32ogl14e.zip ];then
        cd /tmp || exit 1
        wget http://www.svenswrapper.de/gl32ogl14e.zip

        unzip gl32ogl14e.zip

        echo -e "\nCopying Files to Diablo game directory...\n"

        cp -v {glide-init.exe,glide3x.dll} "${PREFIX}/drive_c/Program Files/Diablo II/"

        echo -e "\nCleaning up temp files...\n"

        rm {glide-readme.txt,glide-liesmich.txt,glide-init.exe,glide3x.dll,gl32ogl14e.zip}

        cd "${PREFIX}/drive_c/"Program Files"/"Diablo II"/" || exit 1

        WINEPREFIX="${PREFIX}" WINEARCH="${ARCH}" wine glide-init.exe

        echo -e "\nDone!\n"
    fi
}
runGame(){

    cd "${PREFIX}/drive_c/Program Files/Diablo II/" || exit 1


    echo -n -e "\n${LGREEN}Run game now?(Y/n): ${NC}"
    read -r choice

    if [ "${choice}" = "n" ];then
        exit 0;
    fi

    WINEPREFIX="${PREFIX}" WINEARCH="${ARCH}" wine "Diablo II.exe" " -3dfx" 


}

run_glide(){


    cd "${PREFIX}/drive_c/Program Files/Diablo II/" || exit 1


    WINEPREFIX="${PREFIX}" WINEARCH="${ARCH}" wine "glide-init.exe"

}

run_tricks(){
    WINEPREFIX="${PREFIX}" WINEARCH="${ARCH}" winetricks "${1}" "${2}"
}
runWinecfg(){
    WINEPREFIX="${PREFIX}" WINEARCH="${ARCH}" winecfg
}


case "${1}" in

    play|-p)
        runGame
    ;;

    getdeps|-gd)
        getDeps
    ;;

    gettricks|-gt)
        getTricks
    ;;

    getglide| -gg)
        getGlide
    ;;

    id2|--install-d2)
        installD2
    ;;

    ilod|--install-lod)
        installLOD
    ;;

    cfg|-c)
        runWinecfg
    ;;

    rg| --run-glide)
        run_glide
    ;;

    tricks)
        run_tricks "${1}" "${2}"
    ;;

    all|-a)
        getDeps
        installD2
        installLOD
        getTricks
        getGlide
        runGame
    ;;

    *)
        echo -e "\nUsage: $0 [-a] [all] [-p] [play] [-gt] [gettricks] [-gd] [getdeps] [getglide] [-gg] [id2] [--install-d2] [ilod] [--install-lod]\n"
    ;;

esac