#!/bin/bash
# Selfhost pipeline v2 → MIR(JSON v1) → hv1 inline PRIMARY → rc=42
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"; if ROOT_GIT=$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null); then ROOT="$ROOT_GIT"; else ROOT="$(cd "$SCRIPT_DIR/../../../../../../../../.." && pwd)"; fi
source "$ROOT/tools/smokes/v2/lib/test_runner.sh"; require_env || exit 2

tmp="/tmp/selfhost_v1_$$.json"
bash "$ROOT/tools/selfhost/gen_v1_from_selfhost_pipeline_min.sh" > "$tmp"
if [ "${HAKO_VERIFY_SHOW_LOGS:-0}" = "1" ]; then
  echo "[debug] hv1 input json at: $tmp" >&2
  head -n 2 "$tmp" >&2 || true
fi

set +e
rc=$(HAKO_PRIMARY_NO_FALLBACK=1 HAKO_VERIFY_PRIMARY=hakovm verify_v1_inline_file "$tmp" || true)
set -e
rm -f "$tmp" || true

if [[ "$rc" =~ ^-?[0-9]+$ ]] && [ "$rc" -eq 42 ]; then
  echo "[PASS] selfhost_v1_primary_rc42_canary_vm"
  exit 0
fi
echo "[FAIL] selfhost_v1_primary_rc42_canary_vm (rc=$rc, expect 42)" >&2
exit 1
