#!/usr/bin/env bash
set -euo pipefail

# Vault Display — Linux launcher
# Adjust BASE_URL and monitor positions if needed.
BASE_URL="${VAULT_DISPLAY_BASE_URL:-http://localhost/vault-display/public}"

if command -v google-chrome >/dev/null 2>&1; then
  CHROME="google-chrome"
elif command -v chromium-browser >/dev/null 2>&1; then
  CHROME="chromium-browser"
elif command -v chromium >/dev/null 2>&1; then
  CHROME="chromium"
else
  echo "Chrome/Chromium not found."
  exit 1
fi

# Format: key:x:y:width:height
SCREENS=(
  "matrix-1:0:0:1920:1080"
  "matrix-2:1920:0:1920:1080"
  "matrix-3:0:1080:1920:1080"
  "matrix-4:1920:1080:1920:1080"
  "counter:3840:0:1920:1080"
  "tv:5760:0:1920:1080"
  "window:7680:0:1920:1080"
)

# Optional: close old display windows.
# pkill -f "display.php?screen=" || true

for entry in "${SCREENS[@]}"; do
  IFS=":" read -r KEY X Y W H <<< "$entry"
  URL="${BASE_URL}/display.php?screen=${KEY}"

  "$CHROME" \
    --new-window \
    --app="$URL" \
    --window-position="${X},${Y}" \
    --window-size="${W},${H}" \
    --disable-infobars \
    --no-first-run \
    --disable-session-crashed-bubble \
    --autoplay-policy=no-user-gesture-required \
    >/dev/null 2>&1 &

  sleep 0.7
done

echo "Vault Display launched."
