#!/usr/bin/env bash
set -euo pipefail
ROOT=/home/agent/family/Lernen/Pruefungen_Emilia_4_Klasse
RAW="$ROOT/_raw_google_drive"
WORK=/home/agent/tmp/emilia_pruefungen_import
PRE="$WORK/preprocessed_jpg"
OUT="$WORK/docling_text"
FALL="$WORK/tesseract_text"
mkdir -p "$PRE" "$OUT" "$FALL"
cd "$WORK"
. .docling_venv/bin/activate
count=0
for src in $(find "$RAW" -maxdepth 1 -type f | sort -V); do
  base=$(basename "$src")
  stem="${base%.*}"
  jpg="$PRE/${stem}.jpg"
  txt="$OUT/${stem}.txt"
  ftxt="$FALL/${stem}.txt"
  if [ ! -s "$jpg" ]; then
    convert "$src" -auto-orient -resize 1800x1800 "$jpg"
  fi
  if [ ! -s "$txt" ]; then
    tmpdir="$OUT/.tmp_${stem}"
    rm -rf "$tmpdir"; mkdir -p "$tmpdir"
    if timeout 180 docling convert "$jpg" --to text --ocr-engine tesseract --ocr-lang deu,eng --output "$tmpdir" >/tmp/docling_${stem}.log 2>&1; then
      found=$(find "$tmpdir" -type f -name '*.txt' -print -quit || true)
      if [ -n "$found" ]; then cp "$found" "$txt"; fi
    fi
    rm -rf "$tmpdir"
  fi
  if [ ! -s "$ftxt" ]; then
    tesseract "$jpg" stdout -l deu+eng --psm 6 > "$ftxt" 2>/tmp/tess_${stem}.err || true
  fi
  count=$((count+1))
  if [ $((count % 10)) -eq 0 ]; then echo "processed $count"; fi
done
python3 - <<'PY'
import json, re
from pathlib import Path
work=Path('/home/agent/tmp/emilia_pruefungen_import')
raw=Path('/home/agent/family/Lernen/Pruefungen_Emilia_4_Klasse/_raw_google_drive')
rows=[]
for p in sorted(raw.iterdir(), key=lambda x: int(re.search(r'IMG_(\d+)', x.name).group(1))):
    stem=p.stem
    d=(work/'docling_text'/f'{stem}.txt').read_text(errors='ignore') if (work/'docling_text'/f'{stem}.txt').exists() else ''
    t=(work/'tesseract_text'/f'{stem}.txt').read_text(errors='ignore') if (work/'tesseract_text'/f'{stem}.txt').exists() else ''
    rows.append({'file':p.name,'num':int(re.search(r'IMG_(\d+)',p.name).group(1)),'docling_chars':len(d.strip()),'tesseract_chars':len(t.strip()),'docling_head':' '.join(d.split())[:500],'tesseract_head':' '.join(t.split())[:500]})
(work/'ocr_batch_summary.json').write_text(json.dumps(rows,ensure_ascii=False,indent=2),encoding='utf-8')
print('pages',len(rows),'docling_done',sum(r['docling_chars']>0 for r in rows),'tesseract_done',sum(r['tesseract_chars']>0 for r in rows))
PY
