#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Static-analysis gate: run the configured ruff rules over the source and test
# trees. The gated rules are BLE001 and S110; B and SIM are advisory.

set -eu

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
PROJECT_ROOT=$(dirname -- "$SCRIPT_DIR")

cd "$PROJECT_ROOT"

if [ -x "$PROJECT_ROOT/.venv/bin/ruff" ]; then
    RUFF="$PROJECT_ROOT/.venv/bin/ruff"
elif command -v ruff >/dev/null 2>&1; then
    RUFF=$(command -v ruff)
else
    printf '%s\n' "error: ruff is required; install requirements-dev.txt" >&2
    exit 127
fi

# The gated rules are BLE001 (blind except) and S110 (try/except/pass) over the
# source tree: a violation fails the check. B (bugbear) and SIM (simplify) are
# advisory and are reported without failing, so latent issues stay visible
# without gating.
"$RUFF" check --select BLE001,S110 src/orchestrator
"$RUFF" check --select B,SIM --exit-zero src/orchestrator tests
