#!/bin/sh
# Rigenera le icone PNG/ICO da assets/icon.svg e src/app/icon.svg (serve Chrome e ImageMagick)
set -e
cd "$(dirname "$0")/.."
CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
TMP=$(mktemp -d)

render() { # svg, size, out
  printf '<html><body style="margin:0"><img src="file://%s" style="width:%spx;height:%spx;display:block"></body></html>' "$PWD/$1" "$2" "$2" > "$TMP/page.html"
  "$CHROME" --headless=new --disable-gpu --hide-scrollbars --force-device-scale-factor=1 \
    --window-size="$2,$2" --screenshot="$3" "file://$TMP/page.html" >/dev/null 2>&1
}

render assets/icon.svg 180 src/app/apple-icon.png
render assets/icon.svg 192 public/icons/icon-192.png
render assets/icon.svg 512 public/icons/icon-512.png
render src/app/icon.svg 64 "$TMP/fav64.png"
# angoli trasparenti al posto del bianco dello screenshot
magick "$TMP/fav64.png" -alpha set -fuzz 8% -fill none \
  -draw "color 0,0 floodfill" -draw "color 63,0 floodfill" -draw "color 0,63 floodfill" -draw "color 63,63 floodfill" \
  -define icon:auto-resize=48,32,16 src/app/favicon.ico

# Schermate di avvio iPhone: larghezza, altezza (punti CSS), densità
for spec in 430x932x3 393x852x3 402x874x3 440x956x3 390x844x3 428x926x3 375x812x3 414x896x2 375x667x2; do
  w=${spec%%x*}; rest=${spec#*x}; h=${rest%%x*}; d=${rest#*x}
  "$CHROME" --headless=new --disable-gpu --hide-scrollbars --force-device-scale-factor=1 \
    --window-size="$((w * d)),$((h * d))" --virtual-time-budget=3000 --screenshot="public/splash/splash-${w}x${h}@${d}x.png" \
    "file://$PWD/assets/splash.html?z=$d" >/dev/null 2>&1
done
rm -rf "$TMP"
echo "Icone rigenerate"
