#!/usr/bin/env bash
# Author: chal55rus (Sergey P.)
########################################################################
PR64_VER="$(curl https://www.pj64-emu.com/public-releases | grep 'installer' | head -n 1 \
| awk -F'/project64' '{print $2}' \
| awk -F'installer"' '{print $1}')"
[[ "${PR64_VER}" != *-3-* ]] && yad_error "ERROR: Get the Project64 version." && exit 1

export LAUNCH_PARAMETERS=("/VERYSILENT")
export PW_AUTOINSTALL_EXE="${PW_USER_TEMP}/Project64${PR64_VER}setup.exe"
export PORTWINE_CREATE_SHORTCUT_NAME="Project64"

start_portwine
if try_download "https://www.pj64-emu.com/download/project64${PR64_VER}installer" "${PW_AUTOINSTALL_EXE}" no_mirror
then
    GET_URL_POSTFIX="$(cat "${PW_AUTOINSTALL_EXE}" | grep "Did your download not start?" | awk -F"$PR64_VER" '{print $2}' | awk -F'/' '{print $1}')"
    try_remove_file "${PW_AUTOINSTALL_EXE}"
    try_download "https://www.pj64-emu.com/file/setup-project64${PR64_VER}${GET_URL_POSTFIX}/" "${PW_AUTOINSTALL_EXE}" no_mirror
    pw_start_progress_bar_install_game "Project64."
    pw_run "${PW_AUTOINSTALL_EXE}"
    portwine_exe="$WINEPREFIX/drive_c/Program Files (x86)/Project64 3.0/Project64.exe"
    try_remove_file "${PW_AUTOINSTALL_EXE}"
    try_remove_file "${portwine_exe}.ppdb"
    kill_portwine
    pw_stop_progress_bar
    portwine_create_shortcut
fi
stop_portwine
