#!/bin/bash
# Canonical monorepo ESLint entrypoint.
# Always runs from the repository root so the config's root-relative `files`
# patterns match; installs the gate toolchain when it is absent.
set -euo pipefail

REPO_ROOT="$(git rev-parse --show-toplevel)"
cd "$REPO_ROOT"

ESLINT_BIN=".dev-config/node_modules/.bin/eslint"

if [ ! -x "$ESLINT_BIN" ]; then
    echo "eslint gate: installing toolchain (npm ci --prefix .dev-config)..." >&2
    npm ci --prefix .dev-config --no-audit --no-fund >&2
fi

if [ ! -x "$ESLINT_BIN" ]; then
    echo "eslint gate FAILED to become runnable: $ESLINT_BIN is still missing after npm ci." >&2
    exit 1
fi

exec "$ESLINT_BIN" --config .dev-config/eslint.config.mjs --no-warn-ignored \
    --no-error-on-unmatched-pattern "$@"
