#!/usr/bin/bash
# pkhex version 22.12.18 latest full work with wine
# require wine or wine-stable or winehq-stable 9.0 or + and winetricks 20240105 or +
# install bin
#sudo install -d "/usr/share/pkhex/"
#sudo install -D -m 755 "launcher" "$(DESTDIR)$(PREFIX)/bin/pkhex"
#download https://projectpokemon.org/home/files/file/1-pkhex/?do=download&csrfKey=cd29ab3c78e0744900e1bfd604bbfd83 PKHeX (24.01.12).zip
#backup
#download https://github.com/amidevous/PKHeX/releases/download/24.01.12/PKHeX.24.01.12.zip
#unzip PKHeX.24.01.12.zip
#rm -f PKHeX.24.01.12.zip
#sudo install -D -m 644 "PKHeX.exe" "$(DESTDIR)$(PREFIX)/share/pkhex/PKHeX.exe"
# require install wine or wine-stable or winehq-stable + winetricks
#sudo rm -rf "/usr/share/pkhex/wineprefixes/pkhex"

XDG_DATA_HOME="$HOME/.local/share"
PKHEX_DATA_DIRECTORY="$HOME/.local/share/pkhex"
export WINEDEBUG="${WINEDEBUG--all}"
export WINEPREFIX="$HOME/.local/share/pkhex/wineprefixes/pkhex"
#this require net 7 and net 8 or error
export DOTNET_ROOT="C:\Program Files\dotnet"
#this require net 7 and net 8 or error
export DOTNET_BUNDLE_EXTRACT_BASE_DIR="C:\dotnet_bundle_extract"

if [ ! -d "$PKHEX_DATA_DIRECTORY" ]; then
  echo 'Installing PKHeX...'
  mkdir -p "${PKHEX_DATA_DIRECTORY}/bak"
  mkdir -p "$HOME/.local/share/pkhex/wineprefixes/pkhex"
  cd ${PKHEX_DATA_DIRECTORY}
  cp -R /usr/share/pkhex/PKHeX.exe ./
  wget https://download.visualstudio.microsoft.com/download/pr/f18288f6-1732-415b-b577-7fb46510479a/a98239f751a7aed31bc4aa12f348a9bf/windowsdesktop-runtime-8.0.1-win-x64.exe -qO windowsdesktop-runtime-8.0.1-win-x64.exe
  wine windowsdesktop-runtime-8.0.1-win-x64.exe /quiet
  rm  -f windowsdesktop-runtime-8.0.1-win-x64.exe
  rm -f cfg.json
  wine PKHeX.exe
  echo 'Completed installing PKHeX.'
  exit
fi

case "$1" in
  'kill')
    wineserver -k "$2"
    ;;
  'winetricks')
    shift 1
    winetricks "$@"
    ;;
  'wine')
    shift 1
    wine "$@"
    ;;
  '-h'|'--help'|'help')
    echo 'Usage:'
    printf '%s [command]\t\t\tRun PKHeX (with optional command)\n' "$(basename "$0")"
    printf '%s kill [n]\t\t\tKill PKHeX (optinally with signal n)\n' "$(basename "$0")"
    printf '%s wine [command]\t\tExecute Wine command on the Wine prefix (winecfg, regedit, ...)\n' "$(basename "$0")"
    printf '%s winetricks [command]\t\tExecute Winetricks command on the Wine prefix\n' "$(basename "$0")"
    printf '%s {-h --help help}\t\tDisplay this help\n' "$(basename "$0")"
    ;;
  'update')
    echo 'Updating'
    cd ${PKHEX_DATA_DIRECTORY}
    cp -R /usr/share/pkhex/PKHeX.exe ./
    wget https://download.visualstudio.microsoft.com/download/pr/f18288f6-1732-415b-b577-7fb46510479a/a98239f751a7aed31bc4aa12f348a9bf/windowsdesktop-runtime-8.0.1-win-x64.exe -qO windowsdesktop-runtime-8.0.1-win-x64.exe
    wine windowsdesktop-runtime-8.0.1-win-x64.exe /quiet
    rm  -f windowsdesktop-runtime-8.0.1-win-x64.exe
    ;;
  *)
    cd ${PKHEX_DATA_DIRECTORY}
    rm -f cfg.json
    wine ${PKHEX_DATA_DIRECTORY}/PKHeX.exe "$@"
    wineserver -w
    ;;
esac
