#!/sbin/openrc-run

description="Simple Desktop Display Manager"

command="/usr/bin/sddm"
pidfile="/run/sddm.pid"

HEED_DEFAULT_DISPLAY_MANAGER=true
DEFAULT_DISPLAY_MANAGER_FILE=/etc/X11/default-display-manager

depend() {
	need localmount dbus
	use logger
	after rsyslog elogind
	provide xdm display-manager
}

start_pre() {
	[ -x "$command" ] || return 1
	if [ -r /etc/default/locale ]; then
		. /etc/default/locale
		export LANG LANGUAGE
	fi

	if grep -wqs text /proc/cmdline; then
		ewarn "Not starting Simple Desktop Display Manager (sddm); found 'text' in kernel commandline."
		return 1
	fi

	if [ -e "$DEFAULT_DISPLAY_MANAGER_FILE" ] && \
		[ "$HEED_DEFAULT_DISPLAY_MANAGER" = "true" ]; then
		if [ "$(cat "$DEFAULT_DISPLAY_MANAGER_FILE" 2>/dev/null)" != "/usr/bin/sddm" ]; then
			einfo "Not starting Simple Desktop Display Manager; it is not the default display manager"
			return 1
		fi
	fi

	[ -x /bin/plymouth ] && /bin/plymouth quit || true
	mkdir -p /var/lib/sddm
	chown sddm:sddm /var/lib/sddm 2>/dev/null || true
	mkdir -p -m 01777 /tmp/.X11-unix /tmp/.ICE-unix 2>/dev/null || true
}

start() {
	start_pre || return $?
	ebegin "Starting ${RC_SVCNAME}"
	# sddm stays in foreground (QCoreApplication); background via ssd.
	start-stop-daemon -S -b -m -p "$pidfile" -n sddm \
		-x "$command"
	eend $?
}

stop() {
	ebegin "Stopping ${RC_SVCNAME}"
	start-stop-daemon -K -p "$pidfile" -n sddm -R 5
	eend $?
	rm -f "$pidfile"
}
