| 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 | # Match patterns like: [简体中文 README](README.zh-CN.md) |
| 27 | linked_locales=() |
| 28 | while IFS= read -r link; do |
| 29 | # Extract the filename from the markdown link |
| 30 | file=$(echo "$link" | sed -n 's/.*\](\(README\.[^)]*\.md\)).*/\1/p') |
| 31 | if [[ -n "$file" && "$file" != "README.md" ]]; then |
| 32 | linked_locales+=("$file") |
| 33 | fi |
| 34 | done < <(grep -oE '\[[^]]*\]\(README\.[^)]+\.md\)' "$MAIN" 2>/dev/null || true) |
| 35 | |
| 36 | echo "[check-readme-locales] Linked from main README: ${linked_locales[*]:-(none)}" |
| 37 | |
| 38 | # ---- Check 1: every linked file exists --------------------------------- |
| 39 | |
| 40 | missing=() |
| 41 | for file in "${linked_locales[@]}"; do |
| 42 | if [[ ! -f "$ROOT/$file" ]]; then |
| 43 | missing+=("$file") |
| 44 | fi |
| 45 | done |
| 46 | |
| 47 | if [[ ${#missing[@]} -gt 0 ]]; then |
| 48 | echo "[check-readme-locales] FAIL — main README links to missing files: ${missing[*]}" |
| 49 | exit 1 |
| 50 | fi |
| 51 | echo "[check-readme-locales] OK — all linked locale READMEs exist" |
| 52 | |
| 53 | # ---- Check 2: no orphaned locale READMEs ---------------------------- |
| 54 | |
| 55 | # Find all README.<locale>.md files |
| 56 | existing_locales=() |
| 57 | while IFS= read -r f; do |
| 58 | existing_locales+=("$(basename "$f")") |
| 59 | done < <(find "$ROOT" -maxdepth 1 -name 'README.*.md' -not -name 'README.md' 2>/dev/null | sort) |
| 60 | |
| 61 | echo "[check-readme-locales] Existing locale READMEs: ${existing_locales[*]:-(none)}" |
| 62 | |
| 63 | orphans=() |
| 64 | for file in "${existing_locales[@]}"; do |
| 65 | found=0 |
| 66 | for linked in "${linked_locales[@]}"; do |
| 67 | if [[ "$linked" == "$file" ]]; then |
| 68 | found=1 |
| 69 | break |
| 70 | fi |
| 71 | done |
| 72 | if [[ $found -eq 0 ]]; then |
| 73 | orphans+=("$file") |
| 74 | fi |
| 75 | done |
| 76 | |
| 77 | if [[ ${#orphans[@]} -gt 0 ]]; then |
| 78 | echo "[check-readme-locales] FAIL — locale READMEs not linked from main README: ${orphans[*]}" |
| 79 | echo "[check-readme-locales] Add links to these files in the main README.md header." |
| 80 | exit 1 |
| 81 | fi |
| 82 | echo "[check-readme-locales] OK — no orphaned locale READMEs" |
| 83 | |
| 84 | # ---- Check 3: symmetry count ------------------------------------------ |
| 85 | |
| 86 | linked_count=${#linked_locales[@]} |
| 87 | existing_count=${#existing_locales[@]} |
| 88 | |
| 89 | if [[ $linked_count -ne $existing_count ]]; then |
| 90 | echo "[check-readme-locales] WARNING — linked ($linked_count) ≠ existing ($existing_count)" |
| 91 | echo "[check-readme-locales] This is informational; checks 1 and 2 handle actual breakage." |
| 92 | fi |
| 93 | |
| 94 | echo "[check-readme-locales] PASS" |
| 95 |