#!/bin/bash

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


installD2(){
    D2_INSTALLER_PATH="/home/$USER/D2"

    echo -e "\nInstalling Diablo II...\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}

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

}

installLOD(){
    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}

    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
        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"/

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

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

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


    echo -n -e "\nRun game now?(Y/n): "
    read choice

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

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


}

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
    ;;
    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