#!/usr/bin/env bash
# Build a IIIF Level-0 static tree from the gold JPGs already sitting next to this script.
# Run from the directory that contains 00013.jpg .. 00021.jpg (i.e. /gluck_1694/gold/).
# Creates ./iiif/<page>/full/full/0/default.jpg and ./iiif/<page>/info.json for each page.
# Idempotent: re-running just refreshes the copies and info.json files.

set -euo pipefail

PAGES=(00013 00014 00015 00016 00017 00018 00019 00020 00021)
OUT=iiif

# Image dimensions, captured server-side once (see build_iiif.sh generator).
# Format: "<page> <width> <height>"
DIMS=(
  "00013 805 4056"
  "00014 814 4848"
  "00015 781 4786"
  "00016 791 4672"
  "00017 775 4808"
  "00018 818 4800"
  "00019 806 4824"
  "00020 787 4864"
  "00021 800 4784"
)

# Sanity check: are we in the right directory?
for p in "${PAGES[@]}"; do
  if [[ ! -f "${p}.jpg" ]]; then
    echo "ERROR: ${p}.jpg not found in $(pwd)" >&2
    echo "Run this from the directory that contains 00013.jpg .. 00021.jpg" >&2
    exit 1
  fi
done

mkdir -p "$OUT"

for entry in "${DIMS[@]}"; do
  read -r page w h <<< "$entry"
  src="${page}.jpg"
  dest_dir="${OUT}/${page}/full/full/0"
  info_path="${OUT}/${page}/info.json"

  mkdir -p "$dest_dir"
  cp -f "$src" "${dest_dir}/default.jpg"

  cat > "$info_path" <<EOF
{
  "@context": "http://iiif.io/api/image/2/context.json",
  "@id": "https://t.noit.pro/gluck_1694/gold/${OUT}/${page}",
  "@type": "iiif:Image",
  "protocol": "http://iiif.io/api/image",
  "width": ${w},
  "height": ${h},
  "profile": [
    "http://iiif.io/api/image/2/level0.json",
    {
      "formats": ["jpg"],
      "qualities": ["default"]
    }
  ],
  "sizes": [
    {"width": ${w}, "height": ${h}}
  ]
}
EOF

  echo "  ${page}  ${w}x${h}  ?  ${dest_dir}/default.jpg"
done

echo
echo "Done. Now upload manifest.json to ./${OUT}/manifest.json on this box."
echo "Then point eScriptorium at: https://t.noit.pro/gluck_1694/gold/${OUT}/manifest.json"