#!/usr/bin/env bash
# Aggregate all dossier markdown into a single aggregated.md at repo root.
# Order: pages/*.md (index.md first, then page-NN.md numerically), then dossier.md.
set -euo pipefail
cd "$(dirname "$0")"

out=aggregated.md
: > "$out"

emit() {
  printf '\n\n<!-- ===== source: %s ===== -->\n\n' "$1" >> "$out"
  cat "$1" >> "$out"
}

# sort -V: 'index.md' sorts before 'page-*', page numbers sort naturally (robust to 3-digit pages)
while IFS= read -r f; do emit "$f"; done < <(ls pages/*.md | sort -V)
emit dossier.md

echo "Wrote $out — $(grep -c 'source:' "$out") sources, $(wc -l < "$out") lines"
