#!/usr/bin/env bash
# Smoke test of Section 2.4 for Linux and macOS: version banner, data checks and base-model fit of the demo project.
#   bash smoke-test.sh                  # uses the nlmx found on the PATH
#   bash smoke-test.sh /path/to/nlmx    # or a specific executable
set -eu
NLMX=${1:-nlmx}
command -v "$NLMX" >/dev/null 2>&1 || [ -x "$NLMX" ] || { echo "nlmx not found: $NLMX" >&2; exit 1; }

echo "== smoke test"
"$NLMX" version --no-color | sed -n 2,6p
WORK=$(mktemp -d); cd "$WORK"
"$NLMX" generate --output demo --no-color > generate.txt 2>&1
grep -F "Example project written to demo" generate.txt
cd demo
"$NLMX" qc-data data/analysis.csv --no-color -o out_qcd > qcd.txt 2>&1
grep -F "99 checks" qcd.txt | head -1
"$NLMX" fit models/run101.ctl --no-color -o out_fit > fit.txt 2>&1
grep -F "OBJECTIVE FUNCTION VALUE (OFV): -2880.2130" fit.txt
echo "smoke test passed in $WORK"
