#!/usr/bin/env bash
# extract-logos.sh — estrae loghi insanitylab dal PDF Figma Homepage
# Pipeline: extract-fig-images.sh → apply-asset-map.mjs → optimize-assets.sh → extract-logos.sh
# Idempotente: stessi file ad ogni esecuzione, coordinate fisse.
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
PDF="$ROOT/../FigmaDoc/Homepage.pdf"
OUT="$ROOT/public/img"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT

if [[ ! -f "$PDF" ]]; then
  echo "ERRORE: PDF non trovato: $PDF" >&2
  exit 1
fi

mkdir -p "$OUT"

echo "Estrazione loghi da $PDF..."

# ── Logo DARK (header, su fondo bianco) ─────────────────────────────────────
# Zona: x=104, y=41, W=895, H=250 px a 300dpi (pt: x≈25, y≈10, W≈215, H≈60)
pdftoppm -png -r 300 -f 1 -l 1 \
  -x 104 -y 41 -W 895 -H 250 \
  "$PDF" "$WORK/hdr"

convert "$WORK/hdr-1.png" \
  -fuzz 8% -transparent white \
  -trim +repage \
  -resize 600x \
  -strip \
  "$OUT/logo-dark.png"

# ── Logo LIGHT (footer, su fondo scuro #3A2929) ─────────────────────────────
# Zona: x=0, y=35375, W=1800, H=833 px a 300dpi (pt: x≈0, y≈8490, W≈432, H≈200)
# poi crop interno: x=130, y=60, W=850, H=660 per includere solo icona+testo
pdftoppm -png -r 300 -f 1 -l 1 \
  -x 0 -y 35375 -W 1800 -H 833 \
  "$PDF" "$WORK/ftr"

convert "$WORK/ftr-1.png" \
  -crop 850x660+130+60 +repage \
  -fuzz 12% -transparent 'srgb(58,41,41)' \
  -trim +repage \
  -resize 600x \
  -strip \
  "$OUT/logo-light.png"

echo "Logo dark:  $OUT/logo-dark.png  $(identify -format '%wx%h' "$OUT/logo-dark.png")"
echo "Logo light: $OUT/logo-light.png $(identify -format '%wx%h' "$OUT/logo-light.png")"
echo "Done."
