#!/usr/bin/env bash
# Pretest sanity checks + auto-PATH for the xlsa benchmark.
#
# 1. If xlso/xlsi aren't on PATH but a libtool-wrapped copy lives at the
#    source-tree root (../../xlso, ../../xlsi), prepend that root to PATH
#    so an uninstalled build can run the demo too.
# 2. Verify xlso / xlsi / bpdriver / bpcounter / ionadmin are reachable.
# 3. Warn if XLSA_RATE_BPS disagrees with the contact-plan rate.

set -u

fail() { echo "PRETEST FAIL: $1" >&2; exit 1; }
warn() { echo "PRETEST WARN: $1" >&2; }

# Auto-PATH: if xlso isn't on PATH but is at ../.. (the source tree root
# when this demo lives at demos/bench-ltp-xlsa/), prepend it.
if ! command -v xlso >/dev/null 2>&1; then
    SRC_ROOT="$(cd ../.. 2>/dev/null && pwd)"
    if [ -x "${SRC_ROOT}/xlso" ] && [ -x "${SRC_ROOT}/xlsi" ]; then
        export PATH="${SRC_ROOT}:${PATH}"
        echo "Pretest: prepended ${SRC_ROOT} to PATH for xlso/xlsi."
    fi
fi

for bin in xlso xlsi bpdriver bpcounter ionadmin bpadmin ltpadmin; do
    if ! command -v "$bin" >/dev/null 2>&1; then
        fail "$bin not found on PATH (consider sudo make install, or run from the source tree)"
    fi
done

# Rate-consistency warnings are emitted per-scenario by run_scenario,
# not here -- the harness sets XLSA_RATE_BPS internally for each
# scenario, so an "unset" check at pretest time would always fire.

echo "Pretest checks passed."
