| 1 | #!/usr/bin/env bash |
| 2 | # check-readme-locales.sh — verify README locale link symmetry. |
| 3 | # |
| 4 | # Checks: |
| 5 | # 1. Every locale file referenced in the main README exists. |
| 6 | # 2. Every existing README.<locale>.md has a corresponding link in |
| 7 | # the main README (no orphaned locale READMEs). |
| 8 | # 3. Standard README header symmetry (at minimum). |
| 9 | # |
| 10 | # Exits non-zero if any check fails. |
| 11 | |
| 12 | set -euo pipefail |
| 13 | |
| 14 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 15 | MAIN="$ROOT/README.md" |
| 16 | |
| 17 | if [[ ! -f "$MAIN" ]]; then |
| 18 | echo "[check-readme-locales] ERROR: $MAIN not found" |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | echo "[check-readme-locales] Main: $MAIN" |
| 23 | |
| 24 | # ---- Extract locale links from main README ------------------------- |
| 25 | |
| 26 | # Translations live in docs/i18n/. Match the whole link target so a link that |
| 27 | # points outside that directory is caught rather than read as a bare filename. |
| 28 | LOCALES_DIR="docs/i18n" |
| 29 | linked_locales=() |
| 30 | while IFS= read -r link; do |
| 31 | file=$(echo "$link" | sed -n 's/.*\](\([^)]*README\.[^)]*\.md\)).*/\1/p') |
| 32 | if [[ -n "$file" && "$file" != "README.md" ]]; then |
| 33 | linked_locales+=("$file") |
| 34 | fi |
| 35 | done < <(grep -oE '\[[^]]*\]\([^)]*README\.[^)]+\.md\)' "$MAIN" 2>/dev/null || true) |
| 36 | |
| 37 | echo "[check-readme-locales] Linked from main README: ${linked_locales[*]:-(none)}" |
| 38 | |
| 39 | # ---- Check 1: every linked file exists --------------------------------- |
| 40 | |
| 41 | missing=() |
| 42 | for file in "${linked_locales[@]}"; do |
| 43 | if [[ ! -f "$ROOT/$file" ]]; then |
| 44 | missing+=("$file") |
| 45 | fi |
| 46 | done |
| 47 | |
| 48 | if [[ ${#missing[@]} -gt 0 ]]; then |
| 49 | echo "[check-readme-locales] FAIL — main README links to missing files: ${missing[*]}" |
| 50 | exit 1 |
| 51 | fi |
| 52 | echo "[check-readme-locales] OK — all linked locale READMEs exist" |
| 53 | |
| 54 | # ---- Check 2: no orphaned locale READMEs ---------------------------- |
| 55 | |
| 56 | # Find all README.<locale>.md files |
| 57 | existing_locales=() |
| 58 | while IFS= read -r f; do |
| 59 | existing_locales+=("$(basename "$f")") |
| 60 | done < <(find "$ROOT/$LOCALES_DIR" -maxdepth 1 -name 'README.*.md' 2>/dev/null | sort) |
| 61 | |
| 62 | echo "[check-readme-locales] Existing locale READMEs: ${existing_locales[*]:-(none)}" |
| 63 | |
| 64 | orphans=() |
| 65 | for file in "${existing_locales[@]}"; do |
| 66 | found=0 |
| 67 | for linked in "${linked_locales[@]}"; do |
| 68 | if [[ "$(basename "$linked")" == "$file" ]]; then |
| 69 | found=1 |
| 70 | break |
| 71 | fi |
| 72 | done |
| 73 | if [[ $found -eq 0 ]]; then |
| 74 | orphans+=("$file") |
| 75 | fi |
| 76 | done |
| 77 | |
| 78 | if [[ ${#orphans[@]} -gt 0 ]]; then |
| 79 | echo "[check-readme-locales] FAIL — locale READMEs not linked from main README: ${orphans[*]}" |
| 80 | echo "[check-readme-locales] Add links to these files in the main README.md header." |
| 81 | exit 1 |
| 82 | fi |
| 83 | echo "[check-readme-locales] OK — no orphaned locale READMEs" |
| 84 | |
| 85 | # ---- Check 3: symmetry count ------------------------------------------ |
| 86 | |
| 87 | linked_count=${#linked_locales[@]} |
| 88 | existing_count=${#existing_locales[@]} |
| 89 | |
| 90 | if [[ $linked_count -ne $existing_count ]]; then |
| 91 | echo "[check-readme-locales] WARNING — linked ($linked_count) ≠ existing ($existing_count)" |
| 92 | echo "[check-readme-locales] This is informational; checks 1 and 2 handle actual breakage." |
| 93 | fi |
| 94 | |
| 95 | echo "[check-readme-locales] PASS" |
| 96 |