#!/usr/bin/env bash
set -u
cd /home/mande/repo/quber
LOG=experiments/que259/out/batch.log
: > "$LOG"
for pdf in experiments/que259/inputs/*.pdf; do
  id=$(basename "$pdf" .pdf)
  if [[ -f experiments/que259/tables/$id.tables.json && -f experiments/que259/docling/$id.docling.json ]]; then
    echo "[$id] already done, skip" | tee -a "$LOG"; continue
  fi
  echo "[$id] START $(date +%T) size=$(du -h "$pdf" | cut -f1)" | tee -a "$LOG"
  if [[ ! -f experiments/que259/docling/$id.docling.json ]]; then
    uv run quber document --format json -o experiments/que259/docling "$pdf" >>"$LOG" 2>&1
    echo "[$id] docling rc=$? $(date +%T)" | tee -a "$LOG"
  fi
  if [[ ! -f experiments/que259/tables/$id.tables.json ]]; then
    uv run quber table -o experiments/que259/tables "$pdf" >>"$LOG" 2>&1
    echo "[$id] table rc=$? $(date +%T)" | tee -a "$LOG"
  fi
done
echo "BATCH_COMPLETE $(date +%T)" | tee -a "$LOG"
