#!/bin/sh

# Crustace session launcher — invoked by the display manager.
# Runs forge directly so the compositor stays inside the user logind
# session (seat-bound, class=user). Going through `systemctl --user start`
# would put forge inside systemd-user@.service's manager-class session,
# which libseat's logind backend refuses to grant DRM/input access to.

if [ -n "$SHELL" ] \
	&& grep -q "$SHELL" /etc/shells 2>/dev/null \
	&& ! (echo "$SHELL" | grep -q "false") \
	&& ! (echo "$SHELL" | grep -q "nologin")
then
	if [ "$1" != '-l' ]; then
		exec "$SHELL" -l -c "$0 -l $*"
	else
		shift
	fi
fi

export XDG_CURRENT_DESKTOP=crustace:GNOME
export XDG_SESSION_DESKTOP=crustace
export GDK_BACKEND=wayland
export QT_QPA_PLATFORM=wayland
export MOZ_ENABLE_WAYLAND=1

systemctl --user reset-failed
systemctl --user import-environment \
	XDG_CURRENT_DESKTOP XDG_SESSION_TYPE XDG_SESSION_DESKTOP \
	GDK_BACKEND QT_QPA_PLATFORM MOZ_ENABLE_WAYLAND \
	XKB_DEFAULT_LAYOUT XKB_DEFAULT_MODEL XKB_DEFAULT_VARIANT XKB_DEFAULT_OPTIONS

if command -v dbus-update-activation-environment >/dev/null 2>&1; then
	dbus-update-activation-environment --systemd \
		XDG_CURRENT_DESKTOP XDG_SESSION_TYPE XDG_SESSION_DESKTOP \
		GDK_BACKEND QT_QPA_PLATFORM MOZ_ENABLE_WAYLAND \
		XKB_DEFAULT_LAYOUT XKB_DEFAULT_MODEL XKB_DEFAULT_VARIANT XKB_DEFAULT_OPTIONS
fi

# crustace.service is a no-op stub (Type=oneshot, ExecStart=/bin/true) whose
# only job is to activate graphical-session.target via BindsTo, so portals
# and other gated user services (xdg-desktop-portal-gnome, etc.) can start.
# forge itself stays outside systemd-user so libseat grants DRM/input.
systemctl --user start --no-block crustace.service 2>/dev/null || true

exec /usr/bin/forge --exec /usr/share/crustace/session-init
