#!/bin/bash
# Refresh FFXI library art from Steam CDN (app 230330 Seekers Edition store assets).
set -euo pipefail
OUT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/art"
mkdir -p "$OUT"
APP=230330
BASE="https://shared.akamai.steamstatic.com/store_item_assets/steam/apps/${APP}"

echo "Fetching Steam store assets for app $APP into $OUT"
curl -fsSL -o "$OUT/portrait.png" "$BASE/portrait.png"
curl -fsSL -o "$OUT/header.jpg" "$BASE/header.jpg"
curl -fsSL -o "$OUT/capsule_616x353.jpg" "$BASE/capsule_616x353.jpg"
curl -fsSL -o "$OUT/wide.jpg" "$OUT/header.jpg"

# Hero from first screenshot via store API
python3 - "$OUT" <<'PY'
import json, urllib.request, subprocess, sys
from pathlib import Path
out = Path(sys.argv[1])
req = urllib.request.Request(
    "https://store.steampowered.com/api/appdetails?appids=230330",
    headers={"User-Agent": "Mozilla/5.0"},
)
data = json.load(urllib.request.urlopen(req))["230330"]["data"]
ss = (data.get("screenshots") or [{}])[0].get("path_full")
if ss:
    dest = out / "ss0.jpg"
    urllib.request.urlretrieve(ss, dest)
    # crop to ~1920x620 if ffmpeg exists
    hero = out / "hero.jpg"
    try:
        subprocess.run(
            ["ffmpeg", "-y", "-i", str(dest), "-vf", "crop=1920:620:0:230", str(hero)],
            check=True, capture_output=True,
        )
        print("hero", hero.stat().st_size)
    except Exception:
        urllib.request.urlretrieve(ss, hero)
        print("hero (full screenshot fallback)")
print("done")
PY

# Simple logo strip from header if no logo.png
if [ ! -f "$OUT/logo.png" ] && command -v magick >/dev/null; then
  magick "$OUT/header.jpg" -gravity center -crop 400x80+0-20 +repage -fuzz 15% -transparent '#d0d8e0' "$OUT/logo.png" || true
fi
ls -la "$OUT"
