#!/bin/bash
# ARM 1 against fp-driver-v5.sh — EARLY PREFLIGHT FAILURE.
# doyle's condition, restated by todlando for v5: an early preflight failure must
# reach NO product command and NO cargo, because no state is marked before it.
# STUBS ONLY. No cargo, no listener, no elevation, no live probe, no firewall.
set -u
RIG="$(cd "$(dirname "$0")" && pwd)"
SUBJECT='C:/Users/decid/Documents/projects/spt-core/.spt/preserved/hertz-fp-driver-review/v5-as-delivered/fp-driver-v5.sh'
export RIGBIN="$RIG/bin"

pass=0; fail=0
check() { if [ "$2" = "$3" ]; then printf '    %-52s PASS\n' "$1"; pass=$((pass+1));
          else printf '    %-52s FAIL (want %s, got %s)\n' "$1" "$2" "$3"; fail=$((fail+1)); fi; }

build_copy() { # build_copy DEST
  local dest="$1"
  sed -b -e "s#^SP='.*'#SP='$RIG/work'#" \
      -e "s#^EXE=\".*\"#EXE=\"$RIG/spt-stub.sh\"#" \
      "$SUBJECT" > "$dest"
  local difftxt drc count offending
  difftxt=$(diff "$SUBJECT" "$dest"); drc=$?
  [ "$drc" -gt 1 ] && { echo "REFUSED: diff INSTRUMENT FAILURE exit $drc"; exit 9; }
  count=$(printf '%s\n' "$difftxt" | grep -c '^[<>]')
  [ "$count" -eq 0 ] && { echo "REFUSED: zero changed lines — empty is also what a broken diff prints"; exit 9; }
  offending=$(printf '%s\n' "$difftxt" | grep '^[<>]' | grep -cvE "^[<>] *(SP=|EXE=)")
  printf '  config-only diff: %d changed lines, %d outside the config block\n' "$count" "$offending"
  printf '%s\n' "$difftxt" | grep '^[<>]' | cut -c1-110 | sed 's/^/      /'
  [ "$offending" -ne 0 ] && { echo "REFUSED: body modified"; exit 9; }
  printf '%s\n' "$difftxt" > "$RIG/arm1-config-only.diff"
}

rm -rf "$RIG/work"; mkdir -p "$RIG/work"
bash "$RIG/make-stubs-v5.sh" > /dev/null
export RIGLOG="$RIG/work"; : > "$RIGLOG/actions.log"

echo "=== ARM 1 (v5 6f409f91) — capacity below the floor at the first producer gate"
build_copy "$RIG/work/v5-arm1.sh"
chmod +x "$RIG/work/v5-arm1.sh"

( export PATH="$RIGBIN:$PATH" RIGLOG="$RIG/work" FP_FREE_GIB=1.00 FP_ELEVATED=False FP_SPT_MODE=noop
  bash "$RIG/work/v5-arm1.sh" ) > "$RIG/work/arm1.out" 2>&1
rc=$?
cp "$RIG/work/actions.log" "$RIG/arm1-actions.log"

echo "  --- ordered actions requested:"
sed 's/^/      /' "$RIG/arm1-actions.log"
echo "  --- driver exit: $rc"
echo "  --- last findings lines:"
tail -6 "$RIG"/work/fp-run/*/findings.txt 2>/dev/null | sed 's/^/      /'

spt_calls=$(grep -c '^[0-9]* SPT ' "$RIG/arm1-actions.log" || true)
cargo_calls=$(grep -c '^[0-9]* CARGO ' "$RIG/arm1-actions.log" || true)
states=$(ls "$RIG"/work/fp-run/*/state 2>/dev/null | wc -l)
cleanups=$(grep -hc 'CLEANUP_BEGIN' "$RIG"/work/fp-run/*/findings.txt 2>/dev/null || echo 0)

echo "  --- assertions:"
check "driver halts nonzero"                 1 "$([ "$rc" -ne 0 ] && echo 1 || echo 0)"
check "NO product command requested"         0 "$spt_calls"
check "NO cargo requested"                   0 "$cargo_calls"
check "NO resource state was ever marked"    0 "$states"
check "cleanup entered exactly once"         1 "$cleanups"
echo "=== arm 1 tally pass=$pass fail=$fail"
