#!/usr/bin/env bash
# hermes-display-session <xfce-lite|xfce|wm-only> <browser_autostart 0|1> <browser_url>
# Started by hermes-display-run with DISPLAY / DBUS_SESSION_BUS_ADDRESS / XDG_* already exported.
set -uo pipefail
SESSION="${1:-xfce-lite}"; BROWSER_AUTOSTART="${2:-0}"; BROWSER_URL="${3:-about:blank}"
HERE="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"

# D-Bus-activated services (xfconfd, at-spi) must see this display, not whatever the bus inherited.
dbus-update-activation-environment DISPLAY XDG_CONFIG_HOME XDG_CACHE_HOME XDG_CURRENT_DESKTOP \
  CHROME_CONFIG_HOME ACCESSIBILITY_ENABLED GNOME_ACCESSIBILITY QT_ACCESSIBILITY 2>/dev/null

# AT-SPI: start the accessibility bus now (not lazily) with a11y + screen-reader flags on, so
# GTK/Qt/Chromium apps expose their trees. It also sets AT_SPI_BUS on this display's root window.
/usr/libexec/at-spi-bus-launcher --launch-immediately --a11y=1 --screen-reader=1 &

# No blanking / DPMS on an unattended agent display; plain background (cheap to encode/screenshot).
xset s off s noblank -dpms 2>/dev/null
xsetroot -solid '#101214' 2>/dev/null
# Idempotent per-display migration; applies the wallpaper on every session start.
if [[ "$SESSION" == "xfce-lite" ]]; then
  "$HERE/hermes-display-style" || echo 'Desktop styling failed; continuing the usable session' >&2
fi

respawn() {  # keep a desktop component alive without killing the display when it crashes
  ( while true; do "$@"; rc=$?; echo "hermes-display[$DISPLAY] $1 exited rc=$rc; respawning in 2s"; sleep 2; done ) &
}

case "$SESSION" in
  xfce-lite)
    # Window manager (EWMH: _NET_ACTIVE_WINDOW/_NET_CLIENT_LIST are needed by cua-driver) + settings + a panel
    # with a window list so a human taking over from a phone can switch windows. No xfdesktop, no
    # xfce4-session, no autostarts: ~1/3 of the RAM of a full XFCE session and nothing that can lock the screen.
    respawn xfwm4 --compositor=on
    sleep 0.5
    respawn xfsettingsd --disable-wm-check
    respawn xfce4-panel
    ;;
  xfce)
    # Full XFCE session. Screensaver/locker autostarts are masked in $XDG_CONFIG_HOME/autostart.
    respawn xfce4-session
    ;;
  wm-only)
    respawn xfwm4 --compositor=off
    ;;
  *) echo "unknown session '$SESSION'" >&2; exit 78 ;;
esac

# Per-display cua-driver daemon (standard permission mode). Hermes attaches with
#   computer_use.driver_socket: /run/hermes-display/<N>/cua.sock   (cua-driver mcp --socket ...)
# so apps the agent launches (launch_app) are children of THIS unit: they inherit this display's env
# (DISPLAY, session bus, CHROME_CONFIG_HOME) and count against this unit's MemoryMax/CPUQuota.
if [[ "${HERMES_DISPLAY_CUA_DAEMON:-1}" == "1" ]]; then
  CUA="${HERMES_DISPLAY_CUA_DRIVER:-$HOME/.local/bin/cua-driver}"
  if [[ -x "$CUA" ]]; then
    RUN="$(dirname "${DBUS_SESSION_BUS_ADDRESS#unix:path=}")"
    export CUA_DRIVER_RS_TELEMETRY_ENABLED=0
    export PATH="$HOME/.local/bin:$PATH"
    respawn "$CUA" serve --socket "$RUN/cua.sock" --pid-file "$RUN/cua.pid" --no-overlay
  else
    echo "cua-driver not found at $CUA; per-display daemon disabled" >&2
  fi
fi

if [[ "$BROWSER_AUTOSTART" == "1" ]]; then
  sleep 1
  "$HERE/hermes-display-chrome" "$BROWSER_URL" &
fi
wait
