#!/bin/bash
# Hotbar cooldowns (2026-10-03): install the staged DLL into one client folder (default: Trevor's desktop
# client) for testing on the TEST world. Backup: dinput8.dll.bak-hotbarcd (made once, from whatever DLL is there).
# Run on the desktop:  bash install_desktop.sh [client_dir]     (after copying this folder there)
# Launch with the "(Direct)" launcher: the Patcher launcher puts the shipped (live) files back.
# Undo: bash install_desktop.sh --revert [client_dir]   (or just run the Patcher launcher once)
set -euo pipefail
here="$(cd "$(dirname "$0")" && pwd)"
revert=0; [ "${1:-}" = "--revert" ] && { revert=1; shift; }
dst="${1:-/home/tj/Desktop/everquest_rof2_tanks}"
[ -f "$dst/eqgame.exe" ] || { echo "no eqgame.exe in $dst" >&2; exit 1; }
if pgrep -i -x eqgame.exe >/dev/null 2>&1; then echo "close the game first" >&2; exit 1; fi
f=dinput8.dll
if [ $revert = 1 ]; then
    if [ -f "$dst/$f.bak-hotbarcd" ]; then mv -f "$dst/$f.bak-hotbarcd" "$dst/$f"; echo "restored $f"; else echo "no backup; nothing to restore"; fi
    exit 0
fi
want=$(md5sum < "$here/$f" | cut -d" " -f1)
[ "$want" = "5fb9a20e0ed808fd2823ce19a88299d7" ] || { echo "staged $f md5 $want is not the expected 5fb9a20e0ed808fd2823ce19a88299d7 (bad copy?)" >&2; exit 1; }
if [ -f "$dst/$f" ] && [ ! -f "$dst/$f.bak-hotbarcd" ]; then cp -p "$dst/$f" "$dst/$f.bak-hotbarcd"; echo "backed up $f -> $f.bak-hotbarcd"; fi
cp "$here/$f" "$dst/$f.new" && mv -f "$dst/$f.new" "$dst/$f"   # rename: a running client keeps its mapped copy
echo "installed $f ($want)"
grep -h "^Exec=" "/home/tj/Desktop/EverQuest RoF2 Tanks (Direct).desktop" 2>/dev/null | grep -q "dinput8=n,b" \
    || echo "WARNING: the (Direct) launcher has no WINEDLLOVERRIDES=dinput8=n,b; wine will use its own dinput8 (run desktop_hjclient.sh install once)"
echo "Log after playing: $dst/hjclient.log   "
