#!/usr/bin/env bash
# Preview tanin-greet from the repo in a nested cage window (no logout, no real
# auth) and screenshot it. Uses the mock greetd socket so the UI fully renders.
#   ./dev/preview.sh [wallpaper.jpg]
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SOCK=/tmp/mock-greetd.sock
SHOT=/tmp/tanin-greet.png

export GREETD_SOCK="$SOCK"
export TANIN_GREET_CSS="$HERE/tanin-greet.css"
export TANIN_GREET_BG="${1:-$HOME/Downloads/karen-z-OW5F_DMfb0E-unsplash.jpg}"

python3 "$HERE/dev/mockgreetd.py" "$SOCK" >/tmp/mockgreetd.log 2>&1 &
MOCK=$!
trap 'kill "$MOCK" 2>/dev/null; pkill -x cage 2>/dev/null' EXIT
sleep 0.4

timeout 8 cage -s -- python3 "$HERE/tanin-greet" >/tmp/tanin-greet.log 2>&1 &
sleep 4.5
grim -o "${GREET_OUTPUT:-DP-3}" "$SHOT" && echo "screenshot: $SHOT"
pkill -x cage 2>/dev/null || true
