#!/usr/bin/env bash
# Syntax-check the scripts, dry-run the installer, and run the test suite when
# the runtime is present. No network, no system mutation.
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

fail() { echo "health: $1" >&2; exit 1; }

while IFS= read -r -d '' script; do
  bash -n "$script" || fail "bash -n failed: $script"
done < <(find "$ROOT" -name '*.sh' -type f -not -path "$ROOT/venv/*" -print0)

bash -n "${ROOT}/bin/gptbridge" || fail "bash -n failed: ${ROOT}/bin/gptbridge"
"${ROOT}/install.sh" --dry-run >/dev/null || fail "install.sh --dry-run failed"

if [ -x "${ROOT}/venv/bin/python" ]; then
  "${ROOT}/venv/bin/python" -m pytest "${ROOT}/tests" -q || fail "test suite failed"
else
  echo "health: runtime not installed; skipped the test suite (run ${ROOT}/install.sh)"
fi

exit 0
