#!/usr/bin/env bash
set -u
export PATH="$HOME/.cargo/bin:$HOME/.local/bin:$PATH"
cd /home/reavus/spt-core-gate-w2 || exit 90
out=/home/reavus/gate-294-hertz/keeper-head9
mkdir -p "$out" || exit 90
run_leg() {
  local name=$1; shift
  printf '%q ' "$@" > "$out/$name.command"
  printf '\n' >> "$out/$name.command"
  "$@" > "$out/$name.log" 2>&1
  local rc=$?
  printf '%s\n' "$rc" > "$out/$name.exit"
  printf 'HERTZ_LEG %s EXIT %s\n' "$name" "$rc"
  return "$rc"
}
run_leg fetch git fetch /home/reavus/hertz-287-head9.bundle fix/287-keeper-before-bind:refs/heads/hertz-287-head9-gate || exit $?
run_leg checkout git checkout hertz-287-head9-gate || exit $?
sha=$(git rev-parse HEAD)
printf '%s\n' "$sha" > "$out/subject.sha"
[[ "$sha" == a2eb40c4ed4e4bce2022a5d103474cda2b03f834 ]] || exit 91
run_leg claim cargo run -p xtask -- pool-claim --pool /home/reavus/spt-core-gate-w2/target --label hertz-287-head9 || exit $?
run_leg fixture cargo build -p spt --bin translate_proof_fixture || exit $?
run_leg touched cargo nextest run -p spt --test attach_link_push_e2e --test activity_link_push_e2e --test drive_e2e --no-fail-fast
rc=$?
python3 -c 'import pathlib,re,sys; p=pathlib.Path(sys.argv[1]); n=len(re.findall(r"^\s*Summary\s", (p/"touched.log").read_text(), re.M)); (p/"touched.summary-count").write_text(str(n)+"\n"); sys.exit(0 if n==1 else 92)' "$out" || exit $?
[[ "$rc" == 0 ]] || exit "$rc"
run_leg traceability traceable-reqs check || exit $?
printf 'HERTZ_LINUX_HEAD9_GATE_COMPLETE\n'
