#!/bin/sh
# Deterministic OMP-native adapter gates. No model or network is required.
set -u
ROOT=$(CDPATH= cd "$(dirname "$0")/.." && pwd)
cd "$ROOT" || exit 2
rc=0

gate() { printf '\n=== GATE: %s ===\n' "$1"; }
fail() { printf 'FAIL: %s\n' "$1"; rc=1; }
run() {
  name=$1
  shift
  gate "$name"
  if "$@"; then printf 'ok  %s\n' "$name"; else fail "$name"; fi
}

gate "shell syntax"
for file in ci/run-gates.sh ci/digest/build.sh ci/digest/digest-proof-int.sh \
  ci/docs/check-docs.sh ci/manifest/check-manifest.sh \
  ci/publish/package-adapter.sh ci/publish/release-acquire-int.sh \
  tests/adapter-archive.sh tests/ci-gates.sh tests/manifest-schema.sh \
  tests/manifest-shortcut.sh tests/native-launch-manifest.sh \
  tests/release-acquire.sh; do
  if sh -n "$file"; then printf 'ok  %s\n' "$file"; else fail "syntax: $file"; fi
done

PY=""
for candidate in python3 python py; do
  command -v "$candidate" >/dev/null 2>&1 || continue
  if "$candidate" -c 'import pathlib' >/dev/null 2>&1; then
    PY=$candidate
    break
  fi
done
if [ -z "$PY" ]; then
  fail "Python is required for deterministic release and docs gates"
else
  gate "Python syntax"
  for file in ci/docs/build-docs.py ci/manifest/validate_manifest.py \
    ci/publish/create-adapter-archive.py ci/publish/unwrap-changelog.py \
    ci/publish/validate-release-binary.py \
    ci/release/check-version-consistency.py \
    ci/release/validate-release-evidence.py tests/docs-gate.py \
    tests/release-evidence.py tests/version-consistency.py; do
    if "$PY" -c 'import pathlib, sys; compile(pathlib.Path(sys.argv[1]).read_bytes(), sys.argv[1], "exec")' "$file"; then
      printf 'ok  %s\n' "$file"
    else
      fail "syntax: $file"
    fi
  done
  # [int->REQ-DIST-VERSION-CONSISTENCY]
  run "version consistency" "$PY" ci/release/check-version-consistency.py
  run "version consistency behavior" "$PY" tests/version-consistency.py
  run "docs drift behavior" "$PY" tests/docs-gate.py
  # [int->REQ-DIST-RELEASE-EVIDENCE]
  run "release evidence validation behavior" "$PY" tests/release-evidence.py
fi

# [int->REQ-DOCS-PUBLISH-GATE]
run "documentation publish gate" sh ci/docs/check-docs.sh
run "packaged OMP skills" bun tests/omp-skills.mjs
run "CI dispatcher contract" sh tests/ci-gates.sh

run "manifest schema" sh ci/manifest/check-manifest.sh
run "manifest schema behavior" sh tests/manifest-schema.sh
run "manifest cutover" sh tests/manifest-shortcut.sh
run "native launch manifest" sh tests/native-launch-manifest.sh
run "archive packaging" sh tests/adapter-archive.sh
run "release acquisition behavior" sh tests/release-acquire.sh
run "OMP helper build and tests" sh ci/digest/build.sh

gate "traceable requirements"
if command -v traceable-reqs >/dev/null 2>&1; then
  if traceable-reqs check; then echo "ok  traceable requirements"; else fail "traceable requirements"; fi
else
  fail "traceable requirements: traceable-reqs not on PATH"
fi

printf '\n=== RESULT: %s ===\n' "$([ "$rc" -eq 0 ] && echo PASS || echo FAIL)"
exit "$rc"
