#!/usr/bin/env bash
set -u
GW=$1; OUT=$GW/.spt-repin
mkdir "$OUT" 2>/dev/null || { echo "REFUSE: $OUT exists" >&2; exit 4; }
cd "$GW" || exit 9
ps_join() { powershell.exe -NoProfile -Command "$1" 2>/dev/null | tr -d '\r\n'; }
holders=$(ps_join "(Get-CimInstance Win32_Process | Where-Object { \$_.ExecutablePath -like '*hertz-repin*' } | Select-Object -ExpandProperty ProcessId) -join ','")
builders=$(ps_join "(Get-CimInstance Win32_Process | Where-Object { \$_.Name -in @('cargo.exe','rustc.exe') -and \$_.CommandLine -like '*hertz-repin*' } | Select-Object -ExpandProperty ProcessId) -join ','")
{ echo "started: $(date -u +%FT%TZ)"; echo "head: $(git rev-parse HEAD)"; echo "holders: [$holders]"; echo "builders: [$builders]"; } > "$OUT/census.txt"
[ -z "$holders" ] && [ -z "$builders" ] || { echo "REFUSE" >&2; exit 5; }
unset OWL_SESSION_ID SPT_AGENT_ID SPT_ENDPOINT_ID SPT_HOME
cargo run -q -p xtask -- pool-claim --pool ./target --label hertz-repin > "$OUT/claim.raw" 2>&1
echo $? > "$OUT/claim.exit"
cargo nextest run -p spt -E 'test(gateway)' --no-fail-fast > "$OUT/gateway.raw" 2>&1
echo $? > "$OUT/gateway.exit"
# The other two golden reds BY NAME. They are todlando's product cause
# (emit.rs splice_typed_msg re-emits `type`), and they are expected to STILL
# red here — printed so nobody reads them as this branch's defect.
cargo nextest run -p spt -E 'test(poll_envelope_e2e) + test(quickstart_e2e)' --no-fail-fast > "$OUT/others.raw" 2>&1
echo $? > "$OUT/others.exit"
cargo clippy --workspace --all-targets -- -D warnings > "$OUT/clippy.raw" 2>&1
echo $? > "$OUT/clippy.exit"
bash -lc 'traceable-reqs check' > "$OUT/treqs.raw" 2>&1
echo $? > "$OUT/treqs.exit"
{ for f in gateway others; do echo "$f Summary=$(grep -c Summary "$OUT/$f.raw")"; done
  echo "finished: $(date -u +%FT%TZ)"; } >> "$OUT/census.txt"
