#!/usr/bin/env bash
# overnight.sh — run the rap-mv runner through its free gates unattended, to a delivery.
#
# The runner stops (exit 10) at every gate and "re-run is approval". The operator
# went to bed with "do what you need to have the rap video all working in the
# morning" (New Era, 2026-08-31): that is his word on the free gates below, so this
# loop re-runs on each of them and stops on anything else — a real error, a gate that
# needs a file he has to make, a lane that needs a login, and ALWAYS before publish
# (PUBLISH_GO is his alone). Every pass is logged with the gate it passed.
#
# Usage: overnight.sh <pack> <slug> [wait-pid] [max-passes]
set -u
PACK="$1"; SLUG="$2"; WAIT_PID="${3:-}"; MAX="${4:-24}"
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
P="${VCLAW_WORKSPACE:-$HOME/videoclaw}/projects/$SLUG"
LOG="$P/build/overnight.log"
say(){ printf '%s  %s\n' "$(date +%H:%M:%S)" "$*" | tee -a "$LOG" >&2; }

# gates this loop may pass on the operator's standing word (free, reversible)
AUTO_GATES='approve the cut|judge the first take|some windows did not render|sign off the BODY|sign off the FILM|check the lines|render the deferred|confirm the two styles|verify who sings|review the per-window submit contract'

if [ -n "$WAIT_PID" ]; then
  say "waiting for pid $WAIT_PID (the render driver) to exit"
  while kill -0 "$WAIT_PID" 2>/dev/null; do sleep 60; done
  say "render driver exited"
fi

pass=0
while [ "$pass" -lt "$MAX" ]; do
  pass=$((pass + 1))
  out="$P/build/overnight-pass-$pass.log"
  bash "$HERE/make-rap-mv.sh" "$PACK" "$SLUG" > "$out" 2>&1
  rc=$?
  gate="$(grep -o 'GATE: [^=]*' "$out" | tail -1 | sed 's/GATE: //; s/ *$//')"
  reached="$(tail -1 "$P/build/rap-state" 2>/dev/null)"
  landed_now=$(ls "$P/build/queue/explore/clips" "$P/build/broll/clips" 2>/dev/null | grep -c '\.mp4$')
  # the same gate twice in a row with the same last stage AND no new clip = nothing is
  # moving; stop. A window that fails two draws while other clips land is not that —
  # the lane's moderation is random and the operator's word is retry, never bisect.
  if [ "$rc" -eq 10 ] && [ "$gate|$reached|$landed_now" = "${last_gate:-}|${last_reached:-}|${landed_prev:-}" ]; then
    say "pass $pass: gate '$gate' repeated with nothing new (stage $reached, $landed_now clips) — stopping; the operator decides"
    break
  fi
  last_gate="$gate"; last_reached="$reached"; landed_prev="$landed_now"
  if [ "$rc" -eq 0 ]; then
    say "pass $pass: exit 0 — the runner finished (last stage: $reached)"
    break
  elif [ "$rc" -eq 10 ]; then
    if printf '%s' "$gate" | grep -Eq "$AUTO_GATES"; then
      say "pass $pass: passed gate '$gate' on the operator's standing word (last stage: $reached)"
      continue
    fi
    say "pass $pass: STOP at gate '$gate' — not one this loop may pass (last stage: $reached)"
    break
  else
    say "pass $pass: exit $rc (not a gate) at stage '$reached' — see $out"
    break
  fi
done
say "done after $pass pass(es); last stage: $(tail -1 "$P/build/rap-state" 2>/dev/null)"
