#!/bin/sh

export DISPLAY="${DISPLAY:-:0}"
export XAUTHORITY="${XAUTHORITY:-/home/armbian/.Xauthority}"

# Optional verified rendering deployment. Parse an allowlist as data; never
# source/eval configuration. Explicit environment overrides the saved defaults.
render_conf="${VDI_RENDER_CONF:-$HOME/.config/vdi-terminal/render.conf}"
if [ -r "$render_conf" ]; then
    while IFS='=' read -r key value || [ -n "$key" ]; do
        case "$key" in
        profile) export VDI_STREAM_PROFILE="${VDI_STREAM_PROFILE:-$value}" ;;
        stats) export VDI_STREAM_STATS="${VDI_STREAM_STATS:-$value}" ;;
        program) export VDI_MOONLIGHT_PROGRAM="${VDI_MOONLIGHT_PROGRAM:-$value}" ;;
        decoder) export VDI_MOONLIGHT_DECODER="${VDI_MOONLIGHT_DECODER:-$value}" ;;
        codec) export VDI_MOONLIGHT_CODEC="${VDI_MOONLIGHT_CODEC:-$value}" ;;
        esac
    done < "$render_conf"
fi

# Use the same idempotent recovery for boot and HDMI hotplug. The user service
# supervises ongoing recovery; a singleton lock prevents competing instances.
"$(dirname "$0")/vdi-display-guard" --once >>/tmp/vdi-display-mode.log 2>&1 || true
if ! systemctl --user start vdi-display-guard.service >>/tmp/vdi-display-mode.log 2>&1; then
    echo 'WARNING: display guard service unavailable; only boot recovery active' \
        >>/tmp/vdi-display-mode.log
fi

exec /home/armbian/vdi-terminal/bin/vdi-kiosk-monitor -- \
    /home/armbian/vdi-terminal/bin/kiosk-shell
