#!/bin/sh
###############################################################################
# 	configure x-display-manager's
###############################################################################
setup_gdm3() {
	# autologin + greeter theme
	# timed login required for login after x restart
	sed -i	-e "/^AutomaticLogin\=.*/d" \
		-e "/^AutomaticLoginEnable\=.*/d" \
		-e "/^TimedLoginEnable\=.*/d" \
		-e "/^TimedLogin\=.*/d" \
		-e "/^TimedLoginDelay\=.*/d" \
		-e "s/^\(\[daemon\]$\)/\1\nAutomaticLogin\=${FLL_LIVE_USER}\nAutomaticLoginEnable\=true\n \
			\nTimedLoginEnable\=true\nTimedLogin\=${FLL_LIVE_USER}\nTimedLoginDelay\=1/" \
			/etc/gdm3/daemon.conf

	case "${FLL_XTYPE}" in
		x11)
			sed -i "s/#WaylandEnable=false/WaylandEnable=false/" /etc/gdm3/daemon.conf
			;;
	esac

	# preselect the xsession
	cat > /etc/skel/.dmrc <<EOF
[Desktop]
Session=${FLL_XSESSION}
EOF
}

setup_lightdm() {
	# set autologin user and set timeout to zero
	mkdir -p /etc/lightdm/lightdm.conf.d
	if [ ! -e "/etc/lightdm/lightdm.conf.d/80_${FLL_LIVE_USER}.conf" ]; then
		cat >"/etc/lightdm/lightdm.conf.d/80_${FLL_LIVE_USER}.conf" <<EOF
[SeatDefaults]
greeter-hide-users=false
autologin-user=${FLL_LIVE_USER}
autologin-user-timeout=0
autologin-session=${FLL_XSESSION}
EOF
	fi
}

setup_lxdm() {
	# set autologin user
	if [ -L /etc/lxdm/default.conf ] && [ ! -e "/etc/lxdm/${FLL_LIVE_USER}.conf" ]; then
		rm -f /etc/lxdm/default.conf
		sed "s/^# autologin=.*/&\nautologin=${FLL_LIVE_USER}/" \
			"${1}/etc/lxdm/lxdm.conf" > "/etc/lxdm/${FLL_LIVE_USER}.conf"
		ln -fs "${FLL_LIVE_USER}.conf" /etc/lxdm/default.conf
	fi
}

setup_sddm() {
	mkdir -p /etc/sddm.conf.d
	printf "[Autologin]\nUser=${FLL_LIVE_USER}\nSession=${FLL_XSESSION}\n" \
		>>"/etc/sddm.conf.d/${FLL_LIVE_USER}.conf"
	[ -z "${HOMECTL}" ] && printf "Relogin=true\n" \
		>>"/etc/sddm.conf.d/${FLL_LIVE_USER}.conf"

	case "${FLL_XTYPE}" in
		x11)
			sed -i 's/^DisplayServer=.*/DisplayServer=x11/' /etc/sddm.conf.d/*.conf
			;;
	esac
}

setup_slim() {
	# set autologin for $FLL_LIVE_USER to yes
	sed -i	-e "s/^default_user.*/\#FLL\#&/" \
		-e "s/^\#FLL\#\(default_user[ \t]*${FLL_LIVE_USER}$\)/\1/" \
		-e "s/^auto_login.*/\#FLL\#&/" \
		-e "s/^\#FLL\#\(auto_login[ \t]*yes$\)/\1/" \
			/etc/slim.conf
	grep -q ^default_user /etc/slim.conf || \
		printf "default_user\t${FLL_LIVE_USER}\n" >> /etc/slim.conf
	grep -q ^auto_login /etc/slim.conf || \
		printf "auto_login\tyes\n" >> /etc/slim.conf
}

setup_greetd() {
	unset GREETER_CMD
	unset EXECUTE_CMD

	[ "${FLL_XSESSION}" = "hyprland" ] && EXECUTE_CMD="Hyprland" || EXECUTE_CMD="${FLL_XSESSION}"

	if [ -n "${FLL_XSESSION}" ] && [ -x /usr/bin/tuigreet ]; then
		TUITHEME="border=magenta;text=cyan;prompt=cyan;time=cyan;action=cyan;button=magenta;container=black;input=magenta"
		GREETER_CMD="/usr/bin/tuigreet --greeting 'Welcome to ${FLL_DISTRO_NAME}'"
		GREETER_CMD="${GREETER_CMD} --time --user-menu --user-menu-max-uid 60513"
		GREETER_CMD="${GREETER_CMD} --asterisks --remember --remember-user-session"
		GREETER_CMD="${GREETER_CMD} --power-shutdown 'systemctl -i poweroff'"
		GREETER_CMD="${GREETER_CMD} --power-reboot 'systemctl -i reboot'"
		GREETER_CMD="${GREETER_CMD} --cmd '${EXECUTE_CMD}' --theme '${TUITHEME}'"
	else
		case "${FLL_XSESSION}" in
			sway|hyprland)
				GREETER_CMD="${EXECUTE_CMD} --config /etc/greetd/${FLL_XSESSION}-config"
				GREETER="agreety -c ${EXECUTE_CMD}"
				[ -x /usr/sbin/wlgreet ] && GREETER="wlgreet -c ${EXECUTE_CMD}"
				[ -x /usr/bin/gtkgreet ] && GREETER="gtkgreet -l -c ${EXECUTE_CMD}"
				if [ -x /usr/bin/nwg-hello ]; then
					GREETER_CMD= "${EXECUTE_CMD} -c /etc/nwg-hello/${FLL_XSESSION}-config"
				elif [ "${FLL_XSESSION}" = "sway" ]; then
					cat > /etc/greetd/sway-config <<EOF
exec "${GREETER}; swaymsg exit"

bindsym Mod4+shift+e exec swaynag \
-t warning \
-m 'What do you want to do?' \
-b 'Poweroff' 'systemctl -i poweroff' \
-b 'Reboot' 'systemctl -i reboot'

include /etc/sway/sway-config.d/*
EOF
				elif [ "${FLL_XSESSION}" = "hyprland" ]; then
					cat > /etc/greetd/hyprland.conf <<EOF
exec-once = ${GREETER}; hyprctl dispatch exit
EOF
				fi
				;;
			labwc)
				GREETER="agreety"
				[ -x /usr/sbin/wlgreet ] && GREETER="wlgreet"
				[ -x /usr/bin/gtkgreet ] && GREETER="gtkgreet"
				GREETER_CMD="labwc --startup ${GREETER}"
				;;
		esac
	fi

	if [ -n "${GREETER_CMD}" ]; then
		sed -i "s#^command = ".*"#command = \"${GREETER_CMD}\"#" \
			/etc/greetd/config.toml

		cat >> /etc/greetd/config.toml <<EOF

[initial_session]
command = "${EXECUTE_CMD}"
user = "${FLL_LIVE_USER}"
EOF
	fi
}

setup_desktop() {
	# This function may be called twice for the same desktop if user
	# supplied a desktop= cheatcode - everything below must be imdepotent
	DESKTOP="${1}"

	case "${DESKTOP}" in
		budgie|budgie-desktop)
			FLL_XTYPE="x11"
			FLL_XSESSION="budgie-desktop"
			;;
		cinnamon-wayland)
			FLL_XTYPE="wayland"
			FLL_XSESSION="cinnamon-wayland"
			;;
		cinnamon|cinnamon-x11)
			FLL_XTYPE="x11"
			FLL_XSESSION="cinnamon"
			;;
		flux*)
			FLL_XTYPE="x11"
			FLL_XSESSION="fluxbox"
			;;
		gnome)
			FLL_XTYPE="wayland"
			FLL_XSESSION="gnome"
			;;
		gnome-x11)
			FLL_XTYPE="x11"
			FLL_XSESSION="gnome"
			;;
		hyprland)
			FLL_XTYPE="wayland"
			FLL_XSESSION="hyprland"
			;;
		icewm)
			FLL_XTYPE="x11"
			FLL_XSESSION="IceWM"
			;;
		kde|kde-wayland|plasma)
			FLL_XTYPE="wayland"
			FLL_XSESSION="plasma"
			;;
		kde-x11|plasmax11)
			FLL_XTYPE="x11"
			FLL_XSESSION="plasmax11"
			;;
		kodi|kodi-gbm)
			FLL_XTYPE="wayland"
			FLL_XSESSION="kodi-gbm"
			;;
		kodi-x11)
			FLL_XTYPE="x11"
			FLL_XSESSION="kodi"
			;;
		labwc)
			FLL_XTYPE="wayland"
			FLL_XSESSION="labwc"
			;;
		lxde)
			FLL_XTYPE="x11"
			FLL_XSESSION="LXDE"
			;;
		lxqt-x11|lxqt-openbox)
			FLL_XTYPE="x11"
			FLL_XSESSION="lxqt"
			;;
		lxqt|lxqt-wayland|lxqt-labwc|lxqt-kwin|lxqt-hyprland|lxqt-sway|lxqt-wayfire)
			FLL_XTYPE="wayland"
			FLL_XSESSION="lxqt-wayland"
			;;
		mate)
			FLL_XTYPE="x11"
			FLL_XSESSION="mate"
			;;
		sway)
			FLL_XTYPE="wayland"
			FLL_XSESSION="sway"
			;;
		xfce|xfce-x11)
			FLL_XTYPE="x11"
			FLL_XSESSION="xfce"
			;;
		xfce-wayland)
			FLL_XTYPE="wayland"
			FLL_XSESSION="xfce-wayland"
			;;
		*)
			FLL_XTYPE=""
			FLL_XSESSION=""
			;;
	esac

	case "${FLL_XSESSION}" in
		lxqt)
			# set window_manager=openbox
			mkdir -p /etc/skel/.config/lxqt
			cp /etc/xdg/lxqt/session.conf \
				/etc/skel/.config/lxqt/session.conf
			;;
		lxqt-wayland)
			# Support various compositors of lxqt (default: labwc)
			case "${DESKTOP}" in
				lxqt-labwc)
					[ -x /usr/bin/labwc ] && \
					sed -i 's#^compositor=.*#compositor=/usr/bin/labwc#' \
						/etc/xdg/lxqt/session.conf
					;;
				lxqt-kwin)
					[ -x /usr/bin/kwin_wayland ] && \
					sed -i 's#^compositor=.*#compositor=/usr/bin/kwin_wayland#' \
						/etc/xdg/lxqt/session.conf
					;;
				lxqt-hyprland)
					[ -x /usr/bin/Hyprland ] && \
					sed -i 's#^compositor=.*#compositor=/usr/bin/Hyprland#' \
						/etc/xdg/lxqt/session.conf
					;;
				lxqt-sway)
					[ -x /usr/bin/sway ] && \
					sed -i 's#^compositor=.*#compositor=/usr/bin/sway#' \
						/etc/xdg/lxqt/session.conf
					;;
				lxqt-wayfire)
					[ -x /usr/bin/wayfire ] && \
					sed -i 's#^compositor=.*#compositor=/usr/bin/wayfire#' \
						/etc/xdg/lxqt/session.conf
					;;
			esac
			# Solve a chicken and egg problem: preselect wayland compositor
			# or else startlxqt launches lxqt session preferences dialog on
			# first login
			mkdir -p /etc/skel/.config/lxqt
			cp /etc/xdg/lxqt/session.conf \
				/etc/skel/.config/lxqt/session.conf
			# Hack to set initial dark theme
			mkdir -p /etc/skel/.config/labwc
			sed -e 's/Vent/Vent-dark/' \
				/usr/share/lxqt/wayland/labwc/rc.xml \
				> /etc/skel/.config/labwc/rc.xml
			;;
		labwc)
			. /etc/default/keyboard
			if [ "${XKBVARIANT}" ]; then
				XKB_DEFAULT_LAYOUT="${XKBLAYOUT}(${XKBVARIANT})"
			else
				XKB_DEFAULT_LAYOUT="${XKBLAYOUT}"
			fi
			if ! grep -q '^XKB_DEFAULT_LAYOUT=' /etc/xdg/labwc/environment; then
				cat >> /etc/xdg/labwc/environment <<EOF
XKB_DEFAULT_LAYOUT="${XKB_DEFAULT_LAYOUT}"
EOF
			fi
			;;
		hyprland)
			# Hyprland doesn't appear to read from /etc/xdg
			mkdir -p /etc/skel/.config
			cp -a /usr/share/aptosid-settings-hyprland/hypr /etc/skel/.config/
			. /etc/default/keyboard
			sed -i "s#\#kb_model =#kb_layout = ${XKBLAYOUT}#" /etc/skel/.config/hypr/hyprland.conf
			sed -i "s#\#kb_variant =#kb_variant = ${XKBVARIANT}#" /etc/skel/.config/hypr/hyprland.conf
			sed -i "s#\#kb_model =#kb_model = ${XKBMODEL}#" /etc/skel/.config/hypr/hyprland.conf
			sed -i "s#\#kb_options =#kb_options = ${XKBOPTIONS}#" /etc/skel/.config/hypr/hyprland.conf
			;;
		sway)
			mkdir -p /etc/skel/.config/sway
			cp /usr/share/aptosid-settings-sway/config /etc/skel/.config/sway
			cp /usr/share/aptosid-settings-sway/colorscheme /etc/skel/.config/sway
			. /etc/default/keyboard
			sed -i "s#xkb_layout \"\"#xkb_layout \"${XKBLAYOUT}\"#" /etc/skel/.config/sway/config
			sed -i "s#xkb_variant \"\"#xkb_variant \"${XKBVARIANT}\"#" /etc/skel/.config/sway/config
			sed -i "s#xkb_model \"\"#xkb_model \"${XKBMODEL}\"#" /etc/skel/.config/sway/config
			sed -i "s#xkb_options \"\"#xkb_options \"${XKBOPTIONS}\"#" /etc/skel/.config/sway/config
	esac

	export DESKTOP FLL_XTYPE FLL_XSESSION
}

DESKTOP=
# desktop defaults are checked in reverse order
[ -r /usr/share/wayland-sessions/sway.desktop ] && setup_desktop "sway"
[ -r /usr/share/wayland-sessions/hyprland.desktop ] && setup_desktop "hyprland"
[ -r /usr/share/wayland-sessions/labwc.desktop ] && setup_desktop "labwc"
[ -r /usr/share/xsessions/mate.desktop ] && setup_desktop "mate"
[ -r /usr/share/xsessions/budgie-desktop.desktop ] && setup_desktop "budgie-desktop"
[ -r /usr/share/wayland-sessions/cinnamon-wayland.desktop ] && setup_desktop "cinnamon-wayland"
[ -r /usr/share/xsessions/cinnamon.desktop ] && setup_desktop "cinnamon"
[ -r /usr/share/xsessions/kodi.desktop ] && setup_desktop "kodi-x11"
[ -r /usr/share/wayland-sessions/kodi-gbm.desktop ] && setup_desktop "kodi"
[ -r /usr/share/xsessions/icewm-session.desktop ] && setup_desktop "icewm"
[ -r /usr/share/xsessions/fluxbox.desktop ] && setup_desktop "fluxbox"
[ -r /usr/share/xsessions/LXDE.desktop ] && setup_desktop "lxde"
[ -r /usr/share/xsessions/lxqt.desktop ] && setup_desktop "lxqt"
[ -r /usr/share/wayland-sessions/lxqt-wayland.desktop ] && setup_desktop "lxqt-wayland"
[ -r /usr/share/wayland-sessions/xfce-wayland.desktop ] && setup_desktop "xfce-wayland"
[ -r /usr/share/xsessions/xfce.desktop ] && setup_desktop "xfce"
[ -r /usr/share/xsessions/gnome.desktop ] && setup_desktop "gnome-x11"
[ -r /usr/share/wayland-sessions/gnome.desktop ] && setup_desktop "gnome"
[ -r /usr/share/xsessions/plasmax11.desktop ] && setup_desktop "plasmax11"
[ -r /usr/share/wayland-sessions/plasma.desktop ] && setup_desktop "plasma"

# override default desktop
for opt in $(cat /proc/cmdline); do
	case "${opt}" in
		desktop=*)
			setup_desktop "${opt#desktop=}"
			;;
	esac
done

if [ -x /usr/sbin/greetd ]; then
	echo "Configuring greetd..."
	setup_greetd
fi

if [ -x /usr/bin/slim ]; then
	echo "Configuring slim..."
	setup_slim
fi

if [ -x /usr/sbin/lxdm ]; then
	echo "Configuring lxdm..."
	setup_lxdm
fi

if [ -x /usr/sbin/lightdm ]; then
	echo "Configuring lightdm..."
	setup_lightdm
fi

if [ -x /usr/sbin/gdm3 ]; then
	echo "Configuring gdm3..."
	setup_gdm3
fi

if [ -x /usr/bin/sddm ]; then
	echo "Configuring sddm..."
	setup_sddm
fi
