#!/bin/bash
# EoM buff icons (2026-10-03): install the staged spells_us.txt + dbstr_us.txt (shipped files + spells 44895 Echo of
# Insight / 44896 Echo of Plenty and their buff descriptions) into one client folder (default: Trevor's desktop
# client) for testing on the TEST world. Backups: *.bak-eombufficons (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]=c0e724e4f6dd04b696fd442089903972 [dbstr_us.txt]=e71118feb27b03ad4c0e229c5ed23475 )
for f in spells_us.txt dbstr_us.txt; do
    if [ $revert = 1 ]; then
        if [ -f "$dst/$f.bak-eombufficons" ]; then mv -f "$dst/$f.bak-eombufficons" "$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-eombufficons" ]; then cp -p "$dst/$f" "$dst/$f.bak-eombufficons"; 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
if [ "$(grep -ac '^4489[56]^' "$dst/spells_us.txt")" = 2 ]; then echo "spells_us.txt has 44895 + 44896"; else echo "WARNING: spells_us.txt lacks 44895/44896"; fi
