#!/usr/bin/env bash
# scripts/pcb-route-bench — route the PCB benchmark set and write one table
# (docs/backlog/pcb-vector-router.md slice 0; the boards and how each is
# measured: tests/fixtures/pcb/route_bench.py).
#
# Usage:
#   scripts/pcb-route-bench [OUT.md]          # one run (default
#                                             # .scratch/pcb-route-bench/bench.md)
#   scripts/pcb-route-bench --twice [OUT.md]  # two runs; exits 1 unless every
#                                             # count matches (wall time aside)
#
# taz15-blank joins the set when its gitignored snapshot exists:
#   scripts/pcb-snapshot export-prod taz15-blank .scratch/pcb-snapshot/taz15-blank.json
# (PRECIS_PCB_BENCH_TAZ15=<repo-relative path> picks another copy.)
#
# Runs through scripts/test (container, RAM test DB). Paths are repo-relative:
# the container mounts this worktree at /app.
set -euo pipefail

twice=0
if [[ "${1:-}" == "--twice" ]]; then
    twice=1
    shift
fi
out="${1:-.scratch/pcb-route-bench/bench.md}"
here="$(cd "$(dirname "$0")/.." && pwd)"
cd "$here"

run() {
    PRECIS_PCB_BENCH_OUT="$1" scripts/test -n0 -s \
        tests/test_pcb_route_bench.py::test_route_bench_table
}

run "$out"
echo "table: $out"
if (( twice )); then
    second="${out%.md}.rerun.md"
    run "$second"
    if cmp -s "${out%.md}.json" "${second%.md}.json"; then
        echo "deterministic: both runs give the same counts"
    else
        echo "NOT deterministic: ${out%.md}.json and ${second%.md}.json differ" >&2
        diff "${out%.md}.json" "${second%.md}.json" >&2 || true
        exit 1
    fi
fi
