#!/usr/bin/env bash
# lock_gate.sh -- THE CARD LOCK, read FIRST (house rule D-20260928-025: "the body checks its OWN lock first,
# structurally, and a gate test never has a live payload behind the gate"). The house's gate script
# (bench/ollama-vs-vllm-2026-09-28/dtt/tools/ovv_dtt_lock_gate.sh) with the lock word `dv-speed`.
#
# What this file is, for a reader who opened it cold. Every DV-5 arm runs on benchbox as a user unit whose body
# (unit_speed.sh) runs this gate before anything reads or touches the card. It exits 0 only when the lock file exists
# and the first word of its first line is `dv-speed`; otherwise it prints why and exits 3, before any card read. It
# reads one file and writes nothing.
#
# The lock file is /workshop/CARD-LOCK. DV_LOCK_FILE names another file for the gate's own tests only (a test never writes a
# fake /workshop/CARD-LOCK, which other lanes read); the live unit never sets it.
set -u
LOCK="${DV_LOCK_FILE:-$HOME/CARD-LOCK}"
WANT="dv-speed"
if [ ! -f "$LOCK" ]; then
  echo "REFUSED: no lock file at $LOCK; this body runs only under the $WANT lock" >&2
  exit 3
fi
first="$(awk 'NR == 1 { print $1; exit }' "$LOCK" 2>/dev/null)"
if [ "$first" != "$WANT" ]; then
  echo "REFUSED: $LOCK names '${first:-nothing}', not '$WANT'; the card is not this lane's" >&2
  exit 3
fi
echo "lock ok: $(head -n 1 "$LOCK")"
exit 0
