#!/usr/bin/env bash
#
# Gate: the tree run-scenario.sh is about to boot must be a TEST_HARNESS
# build -- the probe endpoint and the pool-allocation fault site compile
# out entirely otherwise (plan-testkit-integration.md Ground truth: without
# both TEST_HARNESS=1 and -DNGX_TEST_HARNESS, the module builds and answers,
# but every fault-injection oracle here would pass by testing nothing).
#
# Checked the same way run-scenario.sh/lib.sh resolve the build tree, so this
# gate can never SKIP a tree the engine would boot fine or pass one it would
# reject -- see consumer-zstd/requires's identical reasoning for why the
# derivation must match lib.sh exactly.
set -euo pipefail

FLAVOR="${2:-nginx}"
VERSION="${3:-1.31.3}"
BUILD="${PROBER_BUILD:-${PROBER_ROOT:-$(cd ../.. && pwd)}/.build/${FLAVOR}-${VERSION}}"
SO="$BUILD/objs/ngx_http_zstd_filter_module.so"

if [ ! -f "$SO" ]; then
    echo "ngx_http_zstd_filter_module.so not found at $SO -- build it first"
    exit 1
fi

PROBE_SYMBOLS="$(nm "$SO" 2>/dev/null | grep -c ' ngx_test_probe_' || true)"
if [ "$PROBE_SYMBOLS" -eq 0 ]; then
    echo "$SO carries no ngx_test_probe_* symbols -- this is a packaged (non-harness) build; configure with TEST_HARNESS=1 CFLAGS=\"\$CFLAGS -DNGX_TEST_HARNESS\" first (see plan-testkit-integration.md Ground truth)"
    exit 1
fi

exit 0
