#!/usr/bin/env bash
# misc_panic_main — confirms `NUC_PANIC_AT=main` fires the panic
# trigger at the very TOP of `fn main` (before clap parses and
# before install_panic_hook runs). The default Rust hook prints
# `panicked at src/lib.rs:<line>` + the message; that's all we
# need here (no backtrace, no diagnostic context — that's gated
# behind `-d` which can't take effect because the panic fires
# before clap parses).
#
# Gates against `strip = true` regression in [profile.release]:
# the `src/lib.rs:<line>` token in the panic header requires the
# build to keep line-tables.
set -euo pipefail

cat > "$NUCTEST_TMPDIR/x.dzn" <<'EOF'
interface I { in void e(); behavior { on e: {} } }
component C { provides I p; behavior { on p.e(): {} } }
EOF

set +e
NUC_PANIC_AT=main "$NUCLEOS" parse "$NUCTEST_TMPDIR/x.dzn" \
    > "$NUCTEST_TMPDIR/out.txt" 2> "$NUCTEST_TMPDIR/err.txt"
exit_code=$?
set -e

if [ "$exit_code" = 0 ]; then
    echo "FAIL: expected panic exit, got 0" >&2
    exit 1
fi
if ! grep -q "panicked at" "$NUCTEST_TMPDIR/err.txt"; then
    echo "FAIL: stderr missing 'panicked at'" >&2
    cat "$NUCTEST_TMPDIR/err.txt" >&2
    exit 1
fi
if ! grep -q "src/" "$NUCTEST_TMPDIR/err.txt"; then
    echo "FAIL: stderr missing 'src/' file location (debuginfo stripped?)" >&2
    cat "$NUCTEST_TMPDIR/err.txt" >&2
    exit 1
fi
