#!/usr/bin/env sh
# PDG Regression Suite — one-command runner
# Canonical home: https://physics.magflowmeters.com/
#
# Runs the regression engine over the six frozen CSVs in ./data and emits the
# seven outputs to ./out. STDLIB-ONLY: no pip install required. The engine
# FAILS CLOSED — a non-zero exit means a required field / unit / uncertainty /
# claim-class / comparison value was missing, or an un-waived FAIL was found.
#
# Usage:
#   ./run_validation.sh                 # uses defaults below
#   ./run_validation.sh --out /tmp/out  # pass extra args straight through
set -eu

# Resolve this script's own directory so it runs from anywhere.
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "$SCRIPT_DIR"

# Pick an interpreter: prefer python3, fall back to python.
if command -v python3 >/dev/null 2>&1; then
  PY=python3
elif command -v python >/dev/null 2>&1; then
  PY=python
else
  echo "ERROR: no python3 / python on PATH." >&2
  exit 127
fi

DATA="${DATA:-data}"
OUT="${OUT:-out}"
CONFIG="${CONFIG:-validation_config.yaml}"

echo "PDG regression suite — home: https://physics.magflowmeters.com/"
echo "Running: $PY pdg_regression.py --data $DATA --out $OUT --config $CONFIG"

# Extra args ("$@") pass straight through to the engine.
"$PY" pdg_regression.py --data "$DATA" --out "$OUT" --config "$CONFIG" "$@"
status=$?

if [ "$status" -eq 0 ]; then
  echo "PASS (exit 0): all seven outputs written to $OUT/ — no un-waived FAILs."
else
  echo "FAIL-CLOSED (exit $status): see $OUT/failed_claims.csv and $OUT/validation_report.md" >&2
fi
exit "$status"
