#!/bin/bash
# Dry-run all UI patches against uifiles/default. Exit 1 on any failure.
#
# Usage:
#   bash ui/verify.sh              # verify patches only
#   bash ui/verify.sh --strict     # also warn if eqgame.exe mtime differs from ui/.verified-build
#   bash ui/verify.sh --record     # verify, then refresh ui/.verified-build from current install

set -euo pipefail

SCRIPT_PATH=$(dirname "${0}")
# shellcheck source=eq-path.sh
source "${SCRIPT_PATH}/eq-path.sh"

STRICT=0
RECORD=0
for arg in "$@"; do
    case "${arg}" in
        --strict) STRICT=1 ;;
        --record) RECORD=1 ;;
        -h|--help)
            echo "Usage: verify.sh [--strict] [--record]"
            exit 0
            ;;
        *)
            echo "Unknown option: ${arg}" >&2
            exit 2
            ;;
    esac
done

GAME_PATH=$(resolve_game_path) || exit 1
DEFAULT_UI="${GAME_PATH}/uifiles/default"
PATCH_DIR="${SCRIPT_PATH}"
VERIFIED_FILE="${PATCH_DIR}/.verified-build"

if [[ ! -d "${DEFAULT_UI}" ]]; then
    echo "Error: missing ${DEFAULT_UI}" >&2
    exit 1
fi

shopt -s nullglob
diffs=( "${PATCH_DIR}"/*.diff )
if [[ ${#diffs[@]} -eq 0 ]]; then
    echo "Error: no *.diff files in ${PATCH_DIR}" >&2
    exit 1
fi

FAIL=0
for diff_file in "${diffs[@]}"; do
    base=$(basename "${diff_file}" .diff)
    default_xml="${DEFAULT_UI}/${base}.xml"
    if [[ ! -f "${default_xml}" ]]; then
        echo "FAIL: ${base} — missing ${default_xml}"
        FAIL=1
        continue
    fi
    if patch --dry-run --ignore-whitespace --fuzz 3 -o /dev/null \
        "${default_xml}" < "${diff_file}" >/dev/null 2>&1; then
        echo "OK:   ${base}"
    else
        echo "FAIL: ${base}"
        patch --dry-run --ignore-whitespace --fuzz 3 -o /dev/null \
            "${default_xml}" < "${diff_file}" 2>&1 | tail -5
        FAIL=1
    fi
done

# Required theme textures
for tga in dark_tile_64.tga dark_tile_192.tga; do
    if [[ ! -f "${PATCH_DIR}/${tga}" ]]; then
        echo "FAIL: missing texture ${PATCH_DIR}/${tga}"
        FAIL=1
    fi
done

if [[ ${FAIL} -ne 0 ]]; then
    echo "Verify failed." >&2
    exit 1
fi

echo "All ${#diffs[@]} patches apply cleanly against ${DEFAULT_UI}"

if [[ ${STRICT} -eq 1 && -f "${VERIFIED_FILE}" ]]; then
    pinned=$(grep -E '^eqgame_mtime=' "${VERIFIED_FILE}" | cut -d= -f2- | tr -d '\r\n' || true)
    current=$(stat -c '%y' "${GAME_PATH}/eqgame.exe" 2>/dev/null | tr -d '\r\n')
    if [[ -n "${pinned}" && "${pinned}" != "${current}" ]]; then
        echo "WARN: eqgame.exe mtime differs from ui/.verified-build" >&2
        echo "  pinned:   ${pinned}" >&2
        echo "  current:  ${current}" >&2
        echo "  Re-run: bash ui/verify.sh --record  (after confirming patches still OK in-game)" >&2
        exit 1
    fi
fi

if [[ ${RECORD} -eq 1 ]]; then
    eq_mtime=$(stat -c '%y' "${GAME_PATH}/eqgame.exe")
    pw_mtime=$(stat -c '%y' "${DEFAULT_UI}/EQUI_PlayerWindow.xml" 2>/dev/null || echo unknown)
    patch_list=$(basename -a "${diffs[@]}" .diff | paste -sd, -)
    cat > "${VERIFIED_FILE}" <<EOF
# Last install verified to apply all UI patches. Refresh with: bash ui/verify.sh --record
verified_date=$(date -Iseconds)
eqgame_mtime=${eq_mtime}
EQUI_PlayerWindow_mtime=${pw_mtime}
game_path=${GAME_PATH}
patch_count=${#diffs[@]}
patches=${patch_list}
EOF
    echo "Recorded ${VERIFIED_FILE}"
fi

exit 0
