#!/usr/bin/env bash
# Author: cefeiko
########################################################################
export LAUNCH_PARAMETERS=("/VERYSILENT")
[ "${update_loc}" = "RUS" ] && export WGC_LOC=RU || export WGC_LOC=EU
export PW_AUTOINSTALL_EXE="${PW_USER_TEMP}/Wargaming_Game_Center_Install_WoT_${WGC_LOC}.exe"

try_download_wgc () {
    set -o pipefail
    wget -t 5 -T 3 "$1" --output-document="$2" 2>&1 | \
    tr '\r' '\n' | sed -u 's/.* \([0-9]\+%\)\ \+\([0-9,.]\+.\) \(.*\)/\1\n#Downloading at \1\/, \2\/s, ETA \3/; s/^20[0-9][0-9].*/#Done./' | \
    "${pw_yad_new}" --center --progress --percentage=0 --title="Download $(basename $1)" --text=Starting... --auto-close --auto-kill --width=500 --height=90
    [ "${PIPESTATUS[0]}" != 0 ] && print_error "failed to download $1. Scipping." && return 1 || return 0
}

start_portwine
if try_download_wgc "redirect.wargaming.net/WGC/Wargaming_Game_Center_Install_WoT_${WGC_LOC}.exe"  "${PW_AUTOINSTALL_EXE}"
then
    pw_start_progress_bar_block "Starting WGC installation..."
    pw_kill_autostart wgc.exe &
    pw_run "${PW_AUTOINSTALL_EXE}"
    portwine_exe="$WINEPREFIX/drive_c/ProgramData/Wargaming.net/GameCenter/api/wgc_api.exe"
    export PORTWINE_CREATE_SHORTCUT_NAME="Wargaming Game Center"
    try_remove_file "${PW_AUTOINSTALL_EXE}"
    kill_portwine
    portwine_create_shortcut
fi
stop_portwine
