#!/usr/bin/env bash
# v2 — NEW FILE (v1 ran to completion; never edit a running driver).
set -u
GW=$1; OUT=$GW/.spt-repin2
mkdir "$OUT" 2>/dev/null || { echo "REFUSE: $OUT exists" >&2; exit 4; }
cd "$GW" || exit 9
unset OWL_SESSION_ID SPT_AGENT_ID SPT_ENDPOINT_ID SPT_HOME

ps_join() { powershell.exe -NoProfile -Command "$1" 2>/dev/null | tr -d '\r\n'; }
# BUILDERS CENSUS FIXED. v1 filtered cargo/rustc by CommandLine containing the
# worktree path — measured vacuous: one live cargo had a BLANK CommandLine and
# another read "<toolchain>\cargo.exe build --bin ..." with no path at all, so
# the predicate could not match ANY of my own invocations either. On a shared
# box the honest question is "is any cargo running", so census them all and say so.
all_cargo=$(ps_join "(Get-CimInstance Win32_Process | Where-Object { \$_.Name -in @('cargo.exe','rustc.exe','clippy-driver.exe') } | Select-Object -ExpandProperty ProcessId) -join ','")
holders=$(ps_join "(Get-CimInstance Win32_Process | Where-Object { \$_.ExecutablePath -like '*hertz-repin*' } | Select-Object -ExpandProperty ProcessId) -join ','")
{
  echo "started: $(date -u +%FT%TZ)"
  echo "head: $(git rev-parse HEAD)"
  echo "dirty: $(git status --porcelain | grep -c '^ M')"
  echo "holders(exe-in-pool): [$holders]"
  echo "cargo-anywhere(box-wide, not lane-scoped): [$all_cargo]"
} > "$OUT/census.txt"
[ -z "$holders" ] || { echo "REFUSE holders $holders" >&2; exit 5; }

# FIXTURES: build by PACKAGE over fixture_package()'s whole map, then assert every
# exe is PRESENT. v1 skipped this and gateway_e2e died at its precondition in
# sibling_bin — a missing file that reads like a fast real failure (1.481 s).
cargo build -p mock-adapter --bins > "$OUT/fixtures.raw" 2>&1
cargo build -p spt-daemon --bins >> "$OUT/fixtures.raw" 2>&1
cargo build -p spt --bins >> "$OUT/fixtures.raw" 2>&1
echo $? > "$OUT/fixtures.exit"
missing=""
for f in capture-player console-mode-probe mock-session mock-shell service_fixture gh_fixture git_fixture post_step_fixture translate_proof_fixture; do
  [ -f "target/debug/$f.exe" ] || missing="$missing $f"
done
echo "fixtures-missing:[$missing]" >> "$OUT/census.txt"
[ -z "$missing" ] || { echo "REFUSE: fixtures missing:$missing" >&2; exit 6; }

# CHILD ENV READ BACK, todlando's form — a parent-side scrub that never reached
# the child is the W0 failure; asserting the parent proves nothing.
cargo run -q -p xtask -- pool-claim --pool ./target --label hertz-repin > "$OUT/claim.raw" 2>&1
echo $? > "$OUT/claim.exit"
env | grep -E '^(OWL_SESSION_ID|SPT_AGENT_ID|SPT_ENDPOINT_ID|SPT_HOME)=' > "$OUT/child-env.txt" 2>/dev/null
echo "perch-vars-in-driver-env: $(wc -l < "$OUT/child-env.txt")" >> "$OUT/census.txt"

run_leg() {  # name, filter
  local n=$1; shift
  cargo nextest run -p spt -E "$1" --no-fail-fast > "$OUT/$n.raw" 2>&1
  echo $? > "$OUT/$n.exit"
  local sum run
  sum=$(grep -c 'Summary' "$OUT/$n.raw")
  run=$(grep -oE 'Summary \[[^]]*\] +[0-9]+ tests? run' "$OUT/$n.raw" | grep -oE '[0-9]+ tests? run' | grep -oE '^[0-9]+')
  run=${run:-0}
  local v=OK
  [ "$sum" = 1 ] || v=SUMMARY-$sum
  [ "$run" -gt 0 ] 2>/dev/null || v="VOID-ZERO-RUN(the filter matched nothing; this is NOT a pass)"
  echo "$n Summary=$sum run-count=$run $v" >> "$OUT/census.txt"
}
run_leg gateway 'binary(gateway_e2e)'
run_leg pair 'test(api_poll_emits_whole_self_delimiting_events) + test(published_messaging_quickstart_runs_as_written)'
echo "finished: $(date -u +%FT%TZ)" >> "$OUT/census.txt"
