#!/usr/bin/env bash
# {{PROJECT_NAME}} — Cursor rule hygiene (generated by cursor_gen)
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
CURSOR="${ROOT}/.cursor"
RULES="${CURSOR}/rules"

echo "== cursor_audit (${ROOT}) =="

if [[ ! -d "$CURSOR" ]]; then
  echo "ERROR: missing ${CURSOR}"
  exit 1
fi

if [[ -d "$RULES" ]]; then
  ac="$(grep -R "alwaysApply: true" "$RULES" --include='*.mdc' 2>/dev/null | wc -l | tr -d ' ')"
  echo "alwaysApply: true occurrences in .cursor/rules: ${ac}"
  echo "  (expect a small number — meta/safety; prefer scoped globs for domain rules)"
else
  echo "WARN: no ${RULES}"
fi

if [[ -d "$RULES/features" ]]; then
  shopt -s nullglob
  for f in "$RULES/features"/*.mdc; do
    base="$(basename "$f" .mdc)"
    if [[ ! -d "${ROOT}/lib/features/${base}" ]] && [[ ! -d "${ROOT}/lib/feature_${base}" ]]; then
      echo "WARN: rules/features/${base}.mdc has no obvious lib/features/${base} folder — update features.modules or lib layout"
    fi
  done
  shopt -u nullglob
fi

echo "OK — review warnings above after brief or folder renames"
