#!/bin/sh -e

CONFIG=/etc/alterator-setup/config
[ ! -s "$CONFIG" ] || . "$CONFIG"

XORG_DUMMY_CONF=/etc/X11/xorg.conf.d/51-dummy.conf

if [ "${ALTERATOR_SETUP_WAYLAND-}" = 1 ]; then
	if ! { [ "${ALTERATOR_SETUP_VNC-}" = 1 ] && [ -s /usr/bin/wayvnc ]; }; then
		# disable ALTERATOR_SETUP_VNC
		sed -i '/^ALTERATOR_SETUP_VNC=/s/^/#/' "$CONFIG"
	fi
else
if [ "${ALTERATOR_SETUP_VNC-}" = 1 ] && [ -s /usr/bin/x11vnc ] &&
	[ -s /usr/lib/alterator/backend3/vnc ]; then
# disable graphical video for Xorg
cat > "$XORG_DUMMY_CONF" << EOF
Section "Device"
    Identifier "Configured Video Device"
    Driver "dummy"
    VideoRam 16384
EndSection

Section "Monitor"
    Identifier "Configured Monitor"
    HorizSync 5.0 - 1000.0
    VertRefresh 5.0 - 200.0
EndSection

Section "Screen"
    Identifier "Default Screen"
    Monitor "Configured Monitor"
    Device "Configured Video Device"
    DefaultDepth 24
    SubSection "Display"
        Viewport 0 0
        Depth 24
        Virtual 1024 768
    EndSubSection
EndSection
EOF

else
# enable graphical video for Xorg
rm -f "$XORG_DUMMY_CONF"

# disable ALTERATOR_SETUP_VNC
sed -i '/^ALTERATOR_SETUP_VNC=/s/^/#/' "$CONFIG"
fi
fi
