#!/usr/bin/env bash
# Check that all relative markdown links under docs/components resolve.
set -uo pipefail
cd "$(dirname "$0")/.."
for f in docs/components/*.md; do
  [ -e "$f" ] || continue
  # extract ](target) links, ignore http(s):, anchors, and mailto
  grep -oE '\]\([^)]+\)' "$f" | sed -E 's/^\]\(//; s/\)$//' | while read -r link; do
    case "$link" in
      http://*|https://*|mailto:*|\#*) continue ;;
    esac
    target="${link%%#*}"                 # strip #anchor
    [ -z "$target" ] && continue
    resolved="$(cd "$(dirname "$f")" && cd "$(dirname "$target")" 2>/dev/null && pwd)/$(basename "$target")"
    if [ ! -e "$resolved" ]; then
      echo "BROKEN: $f -> $link"
    fi
  done
done
echo "link check done"
