#!/bin/zsh
# Print the deck to a true 16:9 PDF with the house export script, then render every page
# to PNG and one contact sheet for the visual check (deck kit Rev 2).
#
# Run from <pack>/deck-build/. The brand export script is found in this order:
#   $IMA_EXPORT, then ../../../brand/export.sh (vault layout reports/<pack>/deck-build),
#   then ../../../export.sh (the kit's own example inside brand/research/deck-kit),
#   then ./brand-export.sh (download https://brand.agentsinmotion.ai/export.sh beside this file).
set -e
HERE="$(cd "$(dirname "$0")" && pwd)"
PACK="$(cd "$HERE/.." && pwd)"
HTML="$(ls "$PACK"/*-DECK.html | head -1)"
PDF="${HTML%.html}.pdf"
BRAND="${IMA_EXPORT:-}"
[[ -z "$BRAND" && -x "$PACK/../../brand/export.sh" ]] && BRAND="$PACK/../../brand/export.sh"
[[ -z "$BRAND" && -x "$PACK/../../export.sh" ]] && BRAND="$PACK/../../export.sh"   # the kit inside brand/research/
[[ -z "$BRAND" && -x "$HERE/brand-export.sh" ]] && BRAND="$HERE/brand-export.sh"
[[ -z "$BRAND" ]] && { echo "brand export.sh not found; set IMA_EXPORT" >&2; exit 1; }
"$BRAND" "$HTML" "$PDF" --deck-16x9
pdfinfo "$PDF" | grep -E "Pages|Page size"
PNG="$(mktemp -d)"
pdftoppm -r 100 -png "$PDF" "$PNG"/p
if command -v montage >/dev/null; then
  montage "$PNG"/p-*.png -tile 3x -geometry 640x360+6+6 "$PNG"/contact-sheet.png 2>/dev/null || true
fi
echo "page images and contact sheet: $PNG"
