#!/usr/bin/env bash
set -euo pipefail
# Usage: bash run-evidence.sh CHECKOUT base|head stalled|healthy OUTPUT_ROOT
# Build each checkout independently first: vp i; vp run build:desktop
# Fetch its Electron runtime: (cd apps/desktop && node node_modules/electron/install.js)
CHECKOUT=$(realpath "$1")
SIDE=$2
CONDITION=$3
OUT=$(realpath -m "$4")/"$SIDE-$CONDITION-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$OUT/data/dbus-1/services"
if [ "$CONDITION" = healthy ]; then
  cat > "$OUT/dunstrc" <<'DUNST'
[global]
origin = top-left
offset = 950x90
width = 400
height = 100
font = Sans 12
frame_width = 2
format = "<b>%s</b>\n%b"
[urgency_normal]
timeout = 120
background = "#ffffff"
foreground = "#111827"
frame_color = "#2563eb"
DUNST
  service_exec="/usr/bin/dunst -conf $OUT/dunstrc"
  OBSERVE_MS=15000
else
  service_exec='/bin/sleep 600'
  OBSERVE_MS=115000
fi
cat > "$OUT/data/dbus-1/services/org.freedesktop.Notifications.service" <<SERVICE
[D-BUS Service]
Name=org.freedesktop.Notifications
Exec=$service_exec
SERVICE
export CHECKOUT SIDE CONDITION OUT OBSERVE_MS
export T3CODE_DEV_SEED=0 XDG_DATA_HOME="$OUT/data"
headless-desktop node "$(dirname "$0")/driver.cjs" > "$OUT/launch.log" 2>&1
printf 'Evidence written to %s\n' "$OUT"
