#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT_DIR"

if ! command -v npx >/dev/null 2>&1; then
  echo "npx not found; install Node.js to run markdown lint" >&2
  exit 2
fi

echo "Running markdown lint on tracked markdown files"

scope_file="docs/standards/lint_scope.txt"
if [[ ! -f "$scope_file" ]]; then
  echo "Missing lint scope file: $scope_file" >&2
  exit 2
fi

mapfile -t scope_paths < <(sed '/^\s*$/d; /^\s*#/d' "$scope_file")
files=()
for p in "${scope_paths[@]}"; do
  [[ -f "$p" ]] && files+=("$p")
done

if [[ "${#files[@]}" -eq 0 ]]; then
  echo "No markdown files to lint"
  exit 0
fi

npx -y markdownlint-cli2 --config .markdownlint.yml "${files[@]}"
