#!/bin/bash
set -eu

installer_dir=$HOME/gopanda2-installer-tmpdata

bin_dir=$HOME/.local/bin
app_dir=$HOME/.local/share/applications
icon_dir=$HOME/.local/share/icons
user_dirs=$HOME/.config/user-dirs.dirs
desktop_dir=$HOME/Desktop

if [ -a $user_dirs ]
then
    source $user_dirs
    desktop_dir=$XDG_DESKTOP_DIR
fi

mkdir -p $installer_dir
cd $installer_dir

if [ -z "${HTTP_CLIENT:-}" ]; then
    if type -p curl >/dev/null 2>&1; then
        if [ ! -z "${https_proxy:-}" ]; then
            CURL_PROXY="-x $https_proxy"
	else
	    CURL_PROXY=""
        fi
        HTTP_CLIENT="curl $CURL_PROXY -f -L -o"
    else
        HTTP_CLIENT="wget -O"
    fi
fi

function download_files()
{
    echo "downloading GoPanda2 and resource files"
    FILES="GoPanda2.AppImage
 GoPanda2.desktop.orig
 GoPanda2.png
 uninstall-GoPanda2.sh
 sgf-mime.xml
 ugf-mime.xml
 ugi-mime.xml"
    for FILE in $FILES; do
	$HTTP_CLIENT $FILE "https://pandanet-igs.com/gopanda2/download/${FILE}"
    done
}

function install_bin()
{
    mkdir -p $bin_dir
    echo "Installing GoPanda2 to $bin_dir"
    cp -p GoPanda2.AppImage $bin_dir/
    cp -p uninstall-GoPanda2.sh $bin_dir/
    chmod +x "$bin_dir/GoPanda2.AppImage" "$bin_dir/uninstall-GoPanda2.sh"
}

function install_icon()
{
    mkdir -p $icon_dir
    echo "Installing icon to $icon_dir"
    cp -p GoPanda2.png $icon_dir/
}

function install_desktop_file()
{
    mkdir -p $desktop_dir
    mkdir -p $app_dir
    echo "Installing desktop file to $desktop_dir and $app_dir"
    cp -p GoPanda2.desktop.orig GoPanda2.desktop
    echo "Exec=$bin_dir/GoPanda2.AppImage" >> GoPanda2.desktop
    echo "Icon=$icon_dir/GoPanda2.png" >> GoPanda2.desktop
    echo "Actions=Uninstall" >> GoPanda2.desktop
    echo "[Desktop Action Uninstall]" >> GoPanda2.desktop
    echo "Name=Uninstall GoPanda2" >> GoPanda2.desktop
    echo "Exec=$bin_dir/uninstall-GoPanda2.sh" >> GoPanda2.desktop
    cp -p GoPanda2.desktop $desktop_dir/
    cp -p GoPanda2.desktop $app_dir/
}


function check_xdg_utils()
{
    command -v xdg-icon-resource >/dev/null 2>&1 || { echo >&2 "File type association requires xdg-utils to be installed."; exit 1; }
    command -v xdg-mime          >/dev/null 2>&1 || { echo >&2 "File type association requires xdg-utils to be installed."; exit 1; }
}

function install_ugi_mime()
{
    check_xdg_utils
    echo "Associating ugi file type"
    xdg-mime install ugf-mime.xml
    xdg-mime install ugi-mime.xml
    xdg-icon-resource install --context mimetypes --size 192 $icon_dir/GoPanda2.png application-x-go-ugf
    xdg-icon-resource install --context mimetypes --size 192 $icon_dir/GoPanda2.png application-x-go-ugi
    xdg-mime default GoPanda2.desktop application/x-go-ugf
    xdg-mime default GoPanda2.desktop application/x-go-ugi
}

function install_sgf_mime()
{
    check_xdg_utils
    echo "Associating sgf file type"
    xdg-mime install sgf-mime.xml
    xdg-icon-resource install --context mimetypes --size 192 $icon_dir/GoPanda2.png application-x-go-sgf
    xdg-mime default GoPanda2.desktop application/x-go-sgf
}

function associate_mime_types()
{
    echo "What file types do you want to associate with GoPanda2?"
    select choice in "Both" "Ugi/Ugf" "Sgf" "None"; do
        case $choice in
            Both )    install_ugi_mime; install_sgf_mime; break ;;
            Ugi/Ugf ) install_ugi_mime; break ;;
            Sgf )     install_sgf_mime; break ;;
            None )    break;;
        esac
    done
}

function cleanup()
{
    echo "Cleaning up.."
    cd
    rm -rf $installer_dir
}

echo ""

download_files
install_bin
install_icon
install_desktop_file
associate_mime_types
cleanup
echo ""
echo "Done!"
echo ""
