#!/usr/bin/env bash
# Builds Physarum for the simulator and captures App Store baseline screenshots
# (the rendered player hero) for each required device size.
#
# Output: docs/appstore/screenshots/<device>/01-hero.png
# Requires: Xcode + simulators for a 6.9" iPhone Pro Max, a 13" iPad Pro, and Apple TV 4K
# (device names are matched by regex pattern, so any recent generation works).
set -euo pipefail
cd "$(dirname "$0")/.."
SCHEME="Physarum"
PROJ="apple/PhysarumApp/Physarum.xcodeproj"
BUNDLE_ID="de.jkaindl.physarum"
OUT="docs/appstore/screenshots"
( cd apple/PhysarumApp && xcodegen generate )
shoot() {
local sim="$1" dir="$2" sdk="$3"
echo "==> $sim"
local udid
# $sim is an extended-regex device-name pattern (version-robust across Xcode releases).
# `|| true` so a no-match grep under `set -euo pipefail` yields an empty udid (graceful skip)
# rather than aborting the whole script.
udid=$(xcrun simctl list devices available | grep -E "$sim" | grep -oiE '[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}' | head -1 || true)
[ -n "$udid" ] || { echo "skip: no simulator matching /$sim/"; return 0; }
xcrun simctl boot "$udid" 2>/dev/null || true
xcrun simctl bootstatus "$udid" -b
# Build the app for this simulator (into a temp DerivedData so the repo stays clean) and locate the .app
local dd="${TMPDIR:-/tmp}/physarum-dd-$dir"
xcodebuild -project "$PROJ" -scheme "$SCHEME" -destination "id=$udid" -derivedDataPath "$dd" build >/dev/null
local app
app=$(find "$dd/Build/Products" -name 'Physarum.app' -path "*$sdk*" | head -1)
[ -n "$app" ] || { echo "skip: no .app built for $sim"; return 0; }
xcrun simctl install "$udid" "$app"
xcrun simctl launch "$udid" "$BUNDLE_ID"
sleep 8 # let the Metal renderer warm up and the pattern form
mkdir -p "$OUT/$dir"
xcrun simctl io "$udid" screenshot "$OUT/$dir/01-hero.png"
echo "wrote $OUT/$dir/01-hero.png"
}
shoot "iPhone 1[5-9] Pro Max" "iphone-6.9" "iphonesimulator"
shoot "iPad Pro 13-inch" "ipad-13" "iphonesimulator"
shoot "Apple TV 4K" "appletv" "appletvsimulator"
echo "Done. Review docs/appstore/screenshots/*/01-hero.png (replace/add as desired)."