#!/usr/bin/env bash
# Snapshot the plan's touched files, and build review packages by diffing two
# snapshots. Replaces the commit-range flow, since this repo's owner commits
# by hand and subagents must not commit.
#
#   snap take <label>              copy the tracked file set into snap-<label>/
#   snap pkg <from> <to> <out>     write a review package diffing two snapshots
set -euo pipefail

WS="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(git -C "$WS" rev-parse --show-toplevel)"

FILES=(
  src/pgr_assets/sources/_index.py
  src/pgr_assets/sources/gamedir.py
  src/pgr_assets/sources/pcstarter.py
  src/pgr_assets/sources/obbstarter.py
  src/pgr_assets/sources/patchcdn.py
  src/pgr_assets/sources/xbuildconfig.py
  src/pgr_assets/sources/sourceset.py
  src/pgr_assets/sources/__init__.py
  src/pgr_assets/commands/helpers.py
  tests/sources/test_index.py
  tests/sources/test_gamedir.py
  tests/commands/test_dispatch.py
  CLAUDE.md
)

case "${1:-}" in
take)
  label=${2:?usage: snap take LABEL}
  dest="$WS/snap-$label"
  rm -rf "$dest"
  for f in "${FILES[@]}"; do
    if [ -f "$ROOT/$f" ]; then
      mkdir -p "$dest/$(dirname "$f")"
      cp "$ROOT/$f" "$dest/$f"
    fi
  done
  echo "snapshot $dest"
  ;;
pkg)
  from=${2:?usage: snap pkg FROM TO OUT}
  to=${3:?usage: snap pkg FROM TO OUT}
  out=${4:?usage: snap pkg FROM TO OUT}
  {
    echo "# Review package: snap-$from -> snap-$to"
    echo
    echo "## Files changed"
    diff -rqN "$WS/snap-$from" "$WS/snap-$to" || true
    echo
    echo "## Diff"
    diff -ruN -U10 "$WS/snap-$from" "$WS/snap-$to" || true
  } > "$out"
  echo "wrote $out ($(wc -c < "$out" | tr -d ' ') bytes)"
  ;;
*)
  echo "usage: snap take LABEL | snap pkg FROM TO OUT" >&2
  exit 2
  ;;
esac
