#!/bin/bash

# Wait for DWS to be fully started
while (systemctl -q is-enabled duetwebserver.service) && !(systemctl -q is-active duetwebserver.service); do
	sleep 1
done

# Find the Chromium binary (named chromium-browser on some images, chromium on others)
chromium_bin=
for c in chromium-browser chromium; do
	if command -v "$c" >/dev/null; then
		chromium_bin="$c"
		break
	fi
done

# Older releases (X11 / Wayfire) show DWC fullscreen. On trixie and later the
# labwc on-screen keyboard cannot draw over a fullscreen window, so show a
# maximized window there instead and hide the taskbar while DWC is up
. /etc/os-release
case "$VERSION_CODENAME" in
	buster | bullseye | bookworm)
		window_arg=--start-fullscreen
		;;
	*)
		window_arg=--start-maximized
		hide_panel=1
		;;
esac

# Launch DWC in Chromium, or the default browser if Chromium is not installed
if [ -n "$chromium_bin" ]; then
	# Clear restore prompt info in case the SBC was powered off
	if (! pgrep chromium > /dev/null); then
		rm -f ~/.config/chromium/SingletonLock
		sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' ~/.config/chromium/'Local State'
		sed -i 's/"exited_cleanly":false/"exited_cleanly":true/; s/"exit_type":"[^"]\+"/"exit_type":"Normal"/' ~/.config/chromium/Default/Preferences
	fi

	# wf-panel-pi reserves a strip at the screen edge, so hide it (and its
	# respawn wrapper) while DWC fills the screen, and bring it back on exit
	if [ "$hide_panel" = 1 ] && pgrep -x wf-panel-pi >/dev/null; then
		pkill -f 'lwrespawn.*wf-panel-pi'
		pkill -x wf-panel-pi
		trap '/usr/bin/lwrespawn /usr/bin/wf-panel-pi &' EXIT
	fi

	"$chromium_bin" --app=http://localhost --app-auto-launched "$window_arg"
else
	xdg-open http://localhost
fi

