#!/usr/bin/env bash
#
# Cross-platform raster pass for the v4 comp sheets, single-asset proofs, and OG.
# Replaces the Windows-only render-*.ps1 scripts. Needs Google Chrome / Chromium
# (headless); no ImageMagick — the one former crop is produced by sizing the
# viewport to the crop region instead.
#
# Usage:  bash systems/v4/comp-sheets/render-comps.sh
#         CHROME=/path/to/chrome bash .../render-comps.sh   # to override detection
#
set -euo pipefail
cd "$(dirname "$0")"   # systems/v4/comp-sheets

# ---- locate a Chrome/Chromium binary ----
CHROME="${CHROME:-}"
if [ -z "$CHROME" ]; then
  for c in \
    "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \
    "/Applications/Chromium.app/Contents/MacOS/Chromium" \
    "$(command -v google-chrome 2>/dev/null || true)" \
    "$(command -v google-chrome-stable 2>/dev/null || true)" \
    "$(command -v chromium 2>/dev/null || true)" \
    "$(command -v chromium-browser 2>/dev/null || true)" \
    "/c/Program Files/Google/Chrome/Application/chrome.exe" ; do
    if [ -n "$c" ] && [ -x "$c" ]; then CHROME="$c"; break; fi
  done
fi
if [ -z "$CHROME" ] || [ ! -x "$CHROME" ]; then
  echo "Chrome/Chromium not found. Set CHROME=/path/to/chrome and retry." >&2
  exit 1
fi
echo "Chrome: $CHROME"

shot() {  # shot <input file relative to cwd> <out.png> <width> <height>
  local in="$1" out="$2" w="$3" h="$4"
  rm -f "$out"
  "$CHROME" --headless=new --disable-gpu --hide-scrollbars --disable-cache \
    --virtual-time-budget=7000 --window-size="${w},${h}" \
    --screenshot="$out" "file://$PWD/$in" >/dev/null 2>&1 || true
  if [ -s "$out" ]; then echo "  rendered $out (${w}x${h})"; else echo "  FAILED $out" >&2; return 1; fi
}

# ---- regenerate the data-driven lockups proof HTML ----
node generate-brand-lockups-proof.mjs

# ---- asset SVGs + OG PNGs (favicon PNGs need ImageMagick; the crucible-C favicon
#      is unchanged, so its existing PNGs stay valid and are left as-is) ----
CHROME_PATH="$CHROME" node ../assets/generate-adopted-variant-assets.mjs >/dev/null 2>&1 || true

# ---- full comp sheets ----
shot brand-system-spec.html           brand-system-spec.png            1600 3600
shot brand-lockups-proof.html         brand-lockups-proof.png          1600 1400
shot brand-static-logo-overview.html  brand-static-logo-overview.png   1800 1300
shot adopted-variant-asset-proof.html adopted-variant-asset-proof.png  1600 3800

# ---- parent wordmark comp = the top 1600x1200 of the system spec (was an
#      ImageMagick crop at +0+0; the top crop is just the viewport at that size) ----
shot brand-system-spec.html           parent-light-logo-wordmark-comp-sheet.png 1600 1200

# ---- single-asset parent lockup renders (ring lockup is 633.855x191) ----
shot ../assets/group/variant-parent/lockups/caldura-parent-light-static.svg parent-lockup-direct-svg.png 634 191
shot parent-lockup-mechanical-proof.html parent-lockup-mechanical-proof.png 634 191

echo "comp raster pass complete."
