#!/usr/bin/env bash
# restore-all.sh — snapshot recovery state for every language loop.
#
# Shows: per-language branch, recent commits, test/scoreboard status, plan progress.
# Points at the briefing file for each language. To respawn a loop, paste the
# briefing into Claude via the Agent tool with run_in_background=true and
# isolation=worktree.
#
# Usage:
#   bash plans/restore-all.sh          # status for all languages
#   bash plans/restore-all.sh <lang>   # one language (js|hs|lua|prolog|forth|erlang|haskell)
#   bash plans/restore-all.sh --print  # also cat each briefing
#
set -uo pipefail

cd "$(dirname "$0")/.."

LANGS=(js hs lua prolog forth erlang haskell)
FILTER="${1:-}"
PRINT=""
if [ "$FILTER" = "--print" ]; then PRINT="yes"; FILTER=""; fi

snap_one() {
  local lang="$1"
  local plan
  local dir
  local scoreboard
  local tests_cmd
  local briefing

  case "$lang" in
    js)      plan="plans/js-on-sx.md";      dir="lib/js";        scoreboard="lib/js/test262-scoreboard.md";    briefing="plans/agent-briefings/loop.md";         tests_cmd="bash lib/js/test.sh";;
    hs)      plan="plans/hs-conformance-to-100.md"; dir="lib/hyperscript"; scoreboard="plans/hs-conformance-scoreboard.md"; briefing="plans/agent-briefings/hs-loop.md"; tests_cmd="bash lib/hyperscript/test.sh";;
    lua)     plan="plans/lua-on-sx.md";     dir="lib/lua";       scoreboard="lib/lua/scoreboard.md";           briefing="plans/agent-briefings/lua-loop.md";     tests_cmd="bash lib/lua/conformance.sh";;
    prolog)  plan="plans/prolog-on-sx.md";  dir="lib/prolog";    scoreboard="lib/prolog/scoreboard.md";        briefing="plans/agent-briefings/prolog-loop.md";  tests_cmd="bash lib/prolog/conformance.sh";;
    forth)   plan="plans/forth-on-sx.md";   dir="lib/forth";     scoreboard="lib/forth/scoreboard.md";         briefing="plans/agent-briefings/forth-loop.md";   tests_cmd="bash lib/forth/conformance.sh";;
    erlang)  plan="plans/erlang-on-sx.md";  dir="lib/erlang";    scoreboard="lib/erlang/scoreboard.md";        briefing="plans/agent-briefings/erlang-loop.md";  tests_cmd="bash lib/erlang/conformance.sh";;
    haskell) plan="plans/haskell-on-sx.md"; dir="lib/haskell";   scoreboard="lib/haskell/scoreboard.md";       briefing="plans/agent-briefings/haskell-loop.md"; tests_cmd="bash lib/haskell/conformance.sh";;
    *) echo "unknown lang: $lang"; return 1;;
  esac

  echo "=== $lang ==="
  if [ -f "$plan" ]; then
    echo "plan:      $plan"
  else
    echo "plan:      (missing)"
  fi
  if [ -f "$briefing" ]; then
    echo "briefing:  $briefing"
  else
    echo "briefing:  (missing — cannot respawn)"
  fi
  if [ -d "$dir" ]; then
    echo "dir:       $dir ($(find "$dir" -type f 2>/dev/null | wc -l) files)"
  else
    echo "dir:       $dir (does not exist yet)"
  fi
  if [ -f "$scoreboard" ]; then
    echo "scoreboard: $scoreboard"
    head -3 "$scoreboard" | sed 's/^/  /'
  else
    echo "scoreboard: (not yet generated)"
  fi
  echo "recent commits (vs main):"
  git log --oneline "main..HEAD" -- "$dir" "$plan" 2>/dev/null | head -5 | sed 's/^/  /' || echo "  (none)"
  echo
}

echo "=== environment ==="
echo "branch:     $(git rev-parse --abbrev-ref HEAD)"
echo "HEAD:       $(git log -1 --oneline)"
if [ -x hosts/ocaml/_build/default/bin/sx_server.exe ]; then
  echo "sx_server:  present"
else
  echo "sx_server:  MISSING — build with ./scripts/sx-build-all.sh before spawning any agent"
fi
echo "worktrees:  $(git worktree list | wc -l)"
echo

if [ -n "$FILTER" ]; then
  snap_one "$FILTER"
else
  for l in "${LANGS[@]}"; do snap_one "$l"; done
fi

echo "=== to spawn a loop ==="
echo "Paste the target briefing into Claude via the Agent tool with"
echo "  run_in_background: true"
echo "  isolation: worktree"
echo "The briefing is self-contained; the agent reads the plan's Progress log"
echo "and picks up from wherever the last commit left off."

if [ "$PRINT" = "yes" ]; then
  echo
  echo "=== all briefings ==="
  for f in plans/agent-briefings/*.md; do
    echo
    echo "---- $f ----"
    cat "$f"
  done
fi
