#!/usr/bin/env bash
# Author: chal55rus
# type: games
# name: Toontown Rewritten (ENG)
# image: toontownrewritten
# info_en: A multiplayer online game, where players take on the roles of cartoon characters battling foes in humorous and adventurous locations. The game offers cooperative gameplay, quests, mini-games, and opportunities for interaction with other players in a colorful world.
# info_ru: Многопользовательская онлайн-игра, где игроки берут на себя роли мультяшных персонажей, сражаясь с противниками в локациях, наполненных юмором и приключениями. Игра предлагает кооперативный геймплей, квесты, мини-игры и возможность взаимодействия с другими игроками в красочном мире.
########################################################################
URL_VER=$(curl https://www.toontownrewritten.com/play \
| awk -F"https://cdn.toontownrewritten.com/launcher/windows/TTRInstaller-" '{print $2}' \
| awk -F".exe" '{print $1}' | grep "v1" | head -n1)

export PW_PREFIX_NAME="TOONTOWN_REWRITTEN"
export LAUNCH_PARAMETERS="/S"
export PW_AUTOINSTALL_EXE="${PW_USER_TEMP}/TTRInstaller-$URL_VER.exe"
export PORTWINE_CREATE_SHORTCUT_NAME="Toontown Rewritten"

start_portwine
if try_download "https://cdn.toontownrewritten.com/launcher/windows/TTRInstaller-$URL_VER.exe" "${PW_AUTOINSTALL_EXE}" no_mirror
then
    pw_start_progress_bar_install_game "Toontown Rewritten."
    pw_kill_autostart Launcher.exe &
     pw_run "${PW_AUTOINSTALL_EXE}"
    portwine_exe="$WINEPREFIX/drive_c/Program Files (x86)/Toontown Rewritten/Launcher.exe"
    pw_create_unique_exe "Toontown_Launcher"
    try_remove_file "${PW_AUTOINSTALL_EXE}"
    try_remove_file "${portwine_exe}.ppdb"
    kill_portwine
    pw_stop_progress_bar
    portwine_create_shortcut
fi
stop_portwine
