#!/usr/bin/env bash
# Ragol's Wrath on Linux / Steam Deck: update the game, then start it. Full steps: https://ragolswrath.web.app/guides/linux-steam-deck/
#
#   bash play-linux.sh                    update, then start psobb.exe with plain Wine (if installed)
#   bash play-linux.sh --check            say what this PC has (steam / wine / protontricks / python3), start nothing
#   bash play-linux.sh <steam command>    STEAM MODE: put this in the game's Steam "Launch Options" as
#                                           bash "/full/path/to/RagolsWrath/play-linux.sh" %command%
#                                         then every press of Play updates first and starts the game through Proton.
# Not available on Linux: the Windows launcher's auto-login, Q auto-run and window-style helper (they press keys through
# Windows). Sign in by hand once; the game remembers the account.
cd "$(dirname "$(readlink -f "$0")")" || exit 1
HERE=$PWD
# Steam shows none of this output, so keep a copy beside the game: play-linux.log (send it to us if something fails)
exec 3>&1 4>&2 > >(tee -a "$HERE/play-linux.log") 2>&1   # 3/4 = the real output, given back to the game below
echo "== $(date '+%Y-%m-%d %H:%M:%S') play-linux.sh $*"
have() { command -v "$1" >/dev/null 2>&1; }

if [ "$1" = "--check" ]; then
  for t in steam wine protontricks python3; do if have $t; then echo "found:   $t"; else echo "missing: $t"; fi; done
  [ -f psobb.exe ] && echo "found:   psobb.exe in $HERE" || echo "missing: psobb.exe in $HERE (extract the whole zip first)"
  exit 0
fi

[ -f psobb.exe ] || { echo "psobb.exe is not in $HERE - extract the whole RagolsWrath zip and run this from inside it."; exit 1; }

echo "== Checking for game updates =="
bash ./update-linux.sh
case $? in
  0) ;;
  1) echo "(Could not reach the update site - playing with what you have.)" ;;
  *) echo "(The update was refused, see the line above. Nothing was changed. Playing with what you have; ask in Discord if this repeats.)" ;;
esac

if [ $# -gt 0 ]; then   # Steam mode: Steam passed us its own "run it through Proton" command
  # the game draws 640x480 unless its size settings exist; Proton keeps this game's Windows settings in $STEAM_COMPAT_DATA_PATH/pfx
  [ -n "$STEAM_COMPAT_DATA_PATH" ] && python3 ./ragolswrath_update.py --game-settings "$STEAM_COMPAT_DATA_PATH/pfx"
  echo "== Starting the game through Steam/Proton =="
  exec "$@" 1>&3 2>&4 3>&- 4>&-
fi

if have wine; then
  echo "== Starting the game with Wine =="
  export WINEPREFIX="${WINEPREFIX:-$HOME/.local/share/ragolswrath-wine}"
  # use this folder's DXVK (the game draws through d3d8.dll, which calls d3d9.dll) and the add-on loader
  export WINEDLLOVERRIDES="d3d8,d3d9=n,b;dinput8=n,b"
  python3 ./ragolswrath_update.py --game-settings "$WINEPREFIX"
  exec wine ./psobb.exe 1>&3 2>&4 3>&- 4>&-
fi

cat <<'MSG'
Wine is not installed here. On a Steam Deck / Steam PC use the Steam way:
  1. Steam > Games > Add a Non-Steam Game to My Library > Browse > pick psobb.exe in this folder.
  2. Right-click it > Properties > Compatibility > force Proton Experimental (or the newest Proton).
  3. Properties > Launch Options:   bash "THIS-FOLDER/play-linux.sh" %command%
  4. Press Play. The game updates first, then starts.
Full guide: https://ragolswrath.web.app/guides/linux-steam-deck/
MSG
exit 1
