#!/usr/bin/env bash
# file-watch-diff/watch.sh
# Snapshots FIFA 23 local file state and diffs it between labelled game phases.
# Usage: watch.sh snapshot <label>
#        watch.sh diff <label-a> <label-b>
#        watch.sh watch          # live tail of changes (requires inotifywait)
#
# Labels: baseline | main-menu | entered-fut | squad-change | post-match

set -euo pipefail

SNAP_DIR="$(cd "$(dirname "$0")" && pwd)/snapshots"
mkdir -p "$SNAP_DIR"

# All directories that FIFA 23 writes to locally under Wine/Proton
WATCH_PATHS=(
  "$HOME/.local/share/Steam/steamapps/compatdata/3887260930/pfx/drive_c/users/steamuser/Documents/FIFA 23"
  "$HOME/Games/umu/fifa23-tools/pfx/drive_c/users/steamuser/Documents/FIFA 23"
  "/mnt/games/FIFA 23/Logs"
  "/mnt/games/FIFA 23/Patch"
)

# Build a listing (path + size + mtime) of every file under watched dirs
snapshot() {
  local label="$1"
  local out="$SNAP_DIR/${label}.txt"
  echo "# snapshot: $label  $(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$out"
  for d in "${WATCH_PATHS[@]}"; do
    [ -d "$d" ] || continue
    find "$d" -type f -printf '%TY-%Tm-%Td %TH:%TM:%.2TS  %7s  %p\n' 2>/dev/null
  done | sort >> "$out"
  echo "Snapshot '$label' written to $out"
  wc -l "$out"
}

# Diff two snapshots by path+size+mtime
diff_snapshots() {
  local a="$SNAP_DIR/${1}.txt"
  local b="$SNAP_DIR/${2}.txt"
  [ -f "$a" ] || { echo "Missing snapshot: $1"; exit 1; }
  [ -f "$b" ] || { echo "Missing snapshot: $2"; exit 1; }

  echo "=== Files added between '$1' → '$2' ==="
  comm -13 <(grep -v '^#' "$a" | awk '{print $NF}' | sort) \
            <(grep -v '^#' "$b" | awk '{print $NF}' | sort)

  echo ""
  echo "=== Files removed between '$1' → '$2' ==="
  comm -23 <(grep -v '^#' "$a" | awk '{print $NF}' | sort) \
            <(grep -v '^#' "$b" | awk '{print $NF}' | sort)

  echo ""
  echo "=== Files changed (size or mtime) between '$1' → '$2' ==="
  # Join on filename, report lines whose mtime/size differs
  diff <(grep -v '^#' "$a" | sort -k4) \
       <(grep -v '^#' "$b" | sort -k4) \
    | grep '^[<>]' \
    | awk '{print $NF}' \
    | sort | uniq -d
}

# Live watch using /proc/inotify fallback (poll every 2s)
watch_live() {
  if command -v inotifywait &>/dev/null; then
    inotifywait -r -m -e close_write,create,delete,moved_to \
      "${WATCH_PATHS[@]}" 2>/dev/null
  else
    echo "inotifywait not found. Install inotify-tools for live watching."
    echo "Falling back to 2-second poll diff (Ctrl-C to stop)..."
    local tmp_a tmp_b
    tmp_a=$(mktemp)
    tmp_b=$(mktemp)
    for d in "${WATCH_PATHS[@]}"; do
      [ -d "$d" ] || continue
      find "$d" -type f -printf '%TY-%Tm-%Td %TH:%TM:%.2TS  %7s  %p\n' 2>/dev/null
    done | sort > "$tmp_a"

    while true; do
      sleep 2
      for d in "${WATCH_PATHS[@]}"; do
        [ -d "$d" ] || continue
        find "$d" -type f -printf '%TY-%Tm-%Td %TH:%TM:%.2TS  %7s  %p\n' 2>/dev/null
      done | sort > "$tmp_b"
      diff "$tmp_a" "$tmp_b" | grep '^[<>]' | while read -r line; do
        echo "$(date +%H:%M:%S) $line"
      done
      cp "$tmp_b" "$tmp_a"
    done
  fi
}

case "${1:-}" in
  snapshot) snapshot "${2:?Usage: watch.sh snapshot <label>}" ;;
  diff)     diff_snapshots "${2:?}" "${3:?Usage: watch.sh diff <label-a> <label-b>}" ;;
  watch)    watch_live ;;
  *)
    echo "Usage:"
    echo "  $0 snapshot <label>          # take a file state snapshot"
    echo "  $0 diff <label-a> <label-b> # show what changed between two snapshots"
    echo "  $0 watch                     # live monitor (inotifywait or poll)"
    echo ""
    echo "Suggested workflow:"
    echo "  $0 snapshot baseline"
    echo "  # launch FIFA"
    echo "  $0 snapshot main-menu"
    echo "  # navigate to FUT"
    echo "  $0 snapshot entered-fut"
    echo "  # make squad change"
    echo "  $0 snapshot squad-change"
    echo "  $0 diff main-menu entered-fut"
    ;;
esac
