#!/usr/bin/env bash # batch-index.sh — run scan-notes-index on every transcribed PDF in a folder. # # usage: batch-index.sh [scan-notes-index options...] # # PDFs are taken newest first (reverse filename order, which is date order # for date-prefixed names). A PDF without a .md transcript is skipped. # Pages already indexed are kept, so the script can simply be rerun after an # interruption. A failed PDF is reported and the batch moves on. # # Long runs belong in tmux: tmux new -s index 'batch-index.sh ~/notebooks' set -uo pipefail if [[ $# -lt 1 || ! -d "$1" ]]; then echo "usage: $0 [scan-notes-index options...]" >&2 exit 64 fi dir="$1"; shift if command -v scan-notes-index >/dev/null 2>&1; then run() { scan-notes-index "$@"; } else : "${PDF_TOOLS_DIR:?set PDF_TOOLS_DIR or install scan-notes-index}" run() { (cd "$PDF_TOOLS_DIR" && uv run --quiet scan-notes-index "$@"); } fi pdfs=() while IFS= read -r -d '' f; do pdfs+=("$f"); done \ < <(find "$dir" -type f -iname '*.pdf' -not -name '.*' -print0 | sort -rz) total=${#pdfs[@]} done_n=0; skipped=0; failed=() start=$(date +%s) for i in "${!pdfs[@]}"; do pdf="${pdfs[$i]}" echo "=== [$((i + 1))/$total] ${pdf#"$dir"/} ($(date '+%H:%M'))" >&2 if [[ ! -e "${pdf%.*}.md" ]]; then echo "no transcript; skipping" >&2 skipped=$((skipped + 1)) continue fi if run "$@" "$pdf"; then done_n=$((done_n + 1)) else failed+=("${pdf#"$dir"/}") fi done mins=$(( ($(date +%s) - start) / 60 )) echo "=== finished in ${mins} min: $done_n indexed, $skipped skipped, ${#failed[@]} failed" >&2 for f in ${failed[@]+"${failed[@]}"}; do echo "failed: $f" >&2; done [[ ${#failed[@]} -eq 0 ]]