#!/bin/bash
# THJ AA gaps (2026-10-04): install the staged spells_us.txt + dbstr_us.txt (= current shipped patch_files + spell 17781
# Dimensional Armory + 7895 Holy Shield, AA strings 18973 Situational Awareness, 50000 Dimensional Armory, 10548 Holy Shield)
# (default: Trevor's desktop client) for testing on the TEST world. Backups: *.bak-thjaagaps (made once, from whatever 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
declare -A md5=( [spells_us.txt]=27c57c019a942743aa4e0f5fcd0ccced [dbstr_us.txt]=a770f7b01b5e444278346eb39374dc75 )
for f in spells_us.txt dbstr_us.txt; do
    if [ $revert = 1 ]; then
        if [ -f "$dst/$f.bak-thjaagaps" ]; then mv -f "$dst/$f.bak-thjaagaps" "$dst/$f"; echo "restored $f"; else echo "no backup of $f"; fi
        continue
    fi
    want=$(md5sum < "$here/$f" | cut -d" " -f1)
    [ "$want" = "${md5[$f]}" ] || { echo "staged $f md5 $want is not the expected ${md5[$f]} (bad copy?)" >&2; exit 1; }
    if [ -f "$dst/$f" ] && [ ! -f "$dst/$f.bak-thjaagaps" ]; then cp -p "$dst/$f" "$dst/$f.bak-thjaagaps"; echo "backed up $f"; fi
    cp "$here/$f" "$dst/$f.new" && mv -f "$dst/$f.new" "$dst/$f"
    echo "installed $f ($want)"
done
[ $revert = 1 ] && exit 0
grep -aq "^17781^Dimensional Armory^" "$dst/spells_us.txt" && echo "spells_us.txt has 17781 Dimensional Armory" || echo "WARNING: spells_us.txt lacks 17781"
grep -aq "^18973^1^Situational Awareness^" "$dst/dbstr_us.txt" && echo "dbstr_us.txt has the new AA strings" || echo "WARNING: dbstr_us.txt lacks 18973^1"
