#!/bin/sh

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

# Match the local output to the 2K Moonlight stream.
# This kernel rejects the monitor's 241.70 MHz timing but accepts the 297 MHz
# PLL rate. Longer vertical blanking produces 2560x1440 at approximately 60 Hz.
# HDMI can appear a few seconds after the desktop autostart entry runs.
: >/tmp/vdi-display-mode.log
mode="2560x1440_60_fixed297"
for _ in $(seq 1 20); do
    if xrandr --query 2>>/tmp/vdi-display-mode.log | grep -q '^HDMI-1 connected'; then
        xrandr --newmode "$mode" 297.00 \
            2560 2608 2640 2720 1440 1443 1448 1820 \
            +HSync -VSync >>/tmp/vdi-display-mode.log 2>&1 || true
        xrandr --addmode HDMI-1 "$mode" >>/tmp/vdi-display-mode.log 2>&1 || true
        if xrandr --output HDMI-1 --mode "$mode" >>/tmp/vdi-display-mode.log 2>&1; then
            break
        fi
        xrandr --output HDMI-1 --mode 1920x1080 --rate 60 \
            >>/tmp/vdi-display-mode.log 2>&1 || true
    fi
    sleep 1
done

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