#!/usr/bin/env bash
# Run in the checkout bound to the leased Pixel 9 API 36 session.
# First install the cached real dev client with vp run mobile:install android --session "$SESSION".
# Base launcher bitmaps were verified byte-for-byte against base Expo prebuild output.
# For head: Expo prebuild in the head checkout (APP_VARIANT=development),
# assemble-icons.py, zipalign -p -f 4, then apksigner with the generated debug key.
# Only the five monochrome mipmap entries differ between the assembled APK and cached APK.
set -euo pipefail
: "${SESSION:?Set leased agent-device session}"
: "${OUT:?Set capture directory}"
mkdir -p "$OUT/$(date +%Y%m%d-%H%M%S)"
run="$OUT/$(date +%Y%m%d-%H%M%S)"
mkdir -p "$run"
# First side only: drawer -> held drag T3 Code Dev onto home screen.
# agent-device home --session "$SESSION"
# agent-device swipe 540 2000 540 800 --session "$SESSION"
# agent-device gesture drag 'text="T3 Code Dev"' 'text="Google"' 1000 1500 500 --session "$SESSION"
# Toggle from the currently known theme state; inspect switch before each run.
toggle_theme() {
  agent-device home --session "$SESSION"
  agent-device longpress 540 1100 800 --settle --session "$SESSION"
  agent-device press 'text="Wallpaper & style"' --settle --session "$SESSION"
  agent-device press 'text="Themed icons"' --settle --session "$SESSION"
  agent-device home --session "$SESSION"
}
capture() {
  agent-device screenshot "$run/$1-1.png" --session "$SESSION"
  sleep 0.5
  agent-device screenshot "$run/$1.png" --session "$SESSION"
  compare -metric AE "$run/$1-1.png" "$run/$1.png" null:
}
# Actual order: base themed -> base normal -> install head in place -> head normal -> head themed.
# Repeated screenshots if the clock changed during capture.
case "${1:?capture LABEL or toggle}" in
  toggle) toggle_theme ;;
  capture) capture "${2:?label}" ;;
esac
