#!/bin/sh
# This script allows to choose the display manager in Slint among those
# shipped, and to have speech or not at login in lightdm or text mode.
# Please note that to have speech or not in graphical environments each
# user should use either the orca-on or the orca-off command.
# To have speech on the console use espeakup, fenrir or speechd-up."
# They can be enabled with the switch-on utility.
# Didier Spaier didieratslintdotfr November 2017
# Last updated on 13 February 2026: add nodm.
export TEXTDOMAIN=slint-scripts
if [ "$(id -u)" -ne 0 ]; then
	gettext "This script should be run as root."; echo
	exit
fi
# Just in case someone removes or adds a display manager
[ ! -f /etc/rc.d/rc.4.local ] && echo "#/etc/rc.d/rc.4.local" > /etc/rc.d/rc.4.local
liste="text "
[ -x /usr/sbin/lightdm ] && liste="${liste}lightdm "
[ -x /usr/sbin/nodm ] && liste="${liste}nodm "
[ -x /usr/bin/sddm ] && liste="${liste}sddm "
listew=" $liste"
usage() {
	echo
	gettext "Usage: login-chooser <login manager>"; echo
	gettext "Available login managers:"; echo
	echo "$listew"
	gettext "Short descriptions:"; echo
	printf '%s' "text:    "
	gettext "Login in text mode, in a console. From there, you"; echo
	printf '%s' "         "
	gettext "can type \"startx\" to start a graphical session."; echo
	[ -x /usr/sbin/lightdm ] && gettext "lightdm: Light Display Manager, accessible with speech."; echo
	if [ -x /usr/sbin/nodm ]; then
		printf '%s' "nodm:    "
		gettext "Automatic login display manager."; echo
	fi
	if [ -x /usr/bin/sddm ]; then
		printf '%s' "sddm:    "
		gettext "Simple Desktop Display Manager."; echo
	fi
	echo
	if grep -q id:3:initdefault: /etc/inittab; then
		gettext "Login is currently set to text mode."; echo
	else
		for dm in $liste; do
			if grep -q "^#$dm$" /etc/rc.d/rc.4.local  && \
			[ -x /etc/rc.d/rc.4.local ]; then
				gettext "The display manager" 
				printf '%s' " ${dm} "
				gettext "is currently set."; echo
				break
			fi
		done
	fi
exit
}
restartornot() {
	gettext "To reboot immediatly press R then Enter, else just press Enter."; echo
	read -r answer
	if [ "$answer" = "r" ] || [ "$answer" = "R" ]; then
		shutdown -r now
	fi
}
nodm_configured() {
	nodmConfigFile="/etc/nodm.conf"
	[ ! -f "$nodmConfigFile" ] && return 1
	grep -q "{user}" "$nodmConfigFile" && return 1
	nodmXsession=$(nodm_xsession_path) || return 1
	[ ! -f "$nodmXsession" ] && return 1
	return 0
}
nodm_xsession_path() {
	nodmConfigFile="/etc/nodm.conf"
	[ ! -f "$nodmConfigFile" ] && return 1
	nodmXsession=$(
		# shellcheck disable=SC1090
		. "$nodmConfigFile" >/dev/null 2>&1 &&
		printf '%s' "$NODM_XSESSION"
	) || return 1
	[ -z "$nodmXsession" ] && return 1
	printf '%s\n' "$nodmXsession"
	return 0
}
nodm_make_xsession_executable() {
	nodmXsession=$(nodm_xsession_path) || return 1
	chmod +x "$nodmXsession"
}
configure_nodm_user() {
	nodmConfigFile="/etc/nodm.conf"
	if [ ! -f "$nodmConfigFile" ]; then
		gettext "Missing /etc/nodm.conf. nodm was not enabled."; echo
		return 1
	fi

	userList=$(awk -F: '
	$6 ~ /^\/home\// && $7 !~ /(false|nologin)$/ {
		print $1 ":" $6
	}' /etc/passwd)
	if [ -z "$userList" ]; then
		gettext "No suitable users were found under /home. nodm was not enabled."; echo
		return 1
	fi

	gettext "Choose a user for automatic login:"; echo
	oldIfs=$IFS
	IFS='
'
	userIndex=1
	for userEntry in $userList; do
		userName=$(printf '%s' "$userEntry" | cut -d: -f1)
		printf '%s) %s\n' "$userIndex" "$userName"
		userIndex=$((userIndex+1))
	done
	IFS=$oldIfs

	gettext "Enter a number, or press Enter to abort."; echo
	read -r userChoice
	[ -z "$userChoice" ] && return 1
	case "$userChoice" in
	*[!0-9]*)
		gettext "Invalid selection. nodm was not enabled."; echo
		return 1
		;;
	esac

	selectedEntry=$(printf '%s\n' "$userList" | sed -n "${userChoice}p")
	if [ -z "$selectedEntry" ]; then
		gettext "Invalid selection. nodm was not enabled."; echo
		return 1
	fi

	selectedUser=$(printf '%s' "$selectedEntry" | cut -d: -f1)
	selectedHome=$(printf '%s' "$selectedEntry" | cut -d: -f2-)
	selectedXsession="${selectedHome}/.xinitrc"
	if [ ! -f "$selectedXsession" ]; then
		gettext "The selected user has no ~/.xinitrc file. nodm was not enabled."; echo
		return 1
	fi
	if ! chmod +x "$selectedXsession"; then
		gettext "Could not mark ~/.xinitrc executable for the selected user. nodm was not enabled."; echo
		return 1
	fi
	tmpFile=$(mktemp) || return 1
	sed \
		-e "s@^NODM_USER=.*@NODM_USER='${selectedUser}'@" \
		-e "s@^NODM_XSESSION=.*@NODM_XSESSION='${selectedXsession}'@" \
		"$nodmConfigFile" > "$tmpFile" || {
		rm -f "$tmpFile"
		return 1
	}
	cat "$tmpFile" > "$nodmConfigFile" || {
		rm -f "$tmpFile"
		return 1
	}
	rm -f "$tmpFile"
	gettext "Configured nodm user:"
	printf ' %s\n' "$selectedUser"
	return 0
}
speak() {
	gettext "If you want speech at login press S then Enter, else just press Enter."; echo
	read -r sound
	if [ "$sound" = "s" ] || [ "$sound" = "S" ]; then
		echo "yes" > /etc/dm-speak
		chmod 755 /etc/rc.d/rc.espeakup
		if [ -f /etc/lightdm/lightdm-gtk-greeter.conf ]; then
			(
				cd /etc/lightdm || exit 1
				if ! grep -wq orca lightdm-gtk-greeter.conf; then
					echo "reader = orca" >> lightdm-gtk-greeter.conf
				fi
				if ! grep -wq a11y-states lightdm-gtk-greeter.conf ; then
					echo "a11y-states = +reader" >> lightdm-gtk-greeter.conf
				elif ! grep -wq +reader lightdm-gtk-greeter.conf; then
					sed "/^a11y-states/s/.*/&;+reader/" lightdm-gtk-greeter.conf > dummy
					mv dummy lightdm-gtk-greeter.conf
				fi
			)
		fi
		if [ -f /etc/lightdm/lightdm.conf.d/50-slick-greeter.conf ]; then
			touch /etc/lightdm/slick-greeter.conf
			sed /screen-reader/d /etc/lightdm/slick-greeter.conf > dummy
			echo "screen-reader=true" >> dummy
			mv dummy /etc/lightdm/slick-greeter.conf
			(
				# Unset DISPLAY so gsettings/dconf don't try to connect to Xorg:
				unset DISPLAY
				su -s /bin/bash -l -c "XDG_CONFIG_HOME=/var/lib/gdm/.config \
				dbus-launch gsettings set x.dm.slick-greeter screen-reader true" gdm
			)
			kill -1 "$(ps -C dbus-launch -u gdm --no-headers -o pid|tail -n 1)"
		fi
		gettext "You will have speech at login."; echo
	else
		rm -f /etc/dm-speak
		chmod 644 /etc/rc.d/rc.espeakup
		if [ -f /etc/lightdm/lightdm-gtk-greeter.conf ]; then
			(
				cd /etc/lightdm || exit 1
				sed /orca/d	lightdm-gtk-greeter.conf > dummy
				mv dummy lightdm-gtk-greeter.conf
			)
		fi
		if [ -f /etc/lightdm/lightdm.conf.d/50-slick-greeter.conf ]; then
			touch /etc/lightdm/slick-greeter.conf
			sed /screen-reader/d /etc/lightdm/slick-greeter.conf > dummy
			echo "screen-reader=false" >> dummy
			mv dummy /etc/lightdm/slick-greeter.conf
			(
				# Unset DISPLAY so gsettings/dconf don't try to connect to Xorg:
				unset DISPLAY
				su -s /bin/bash -l -c "XDG_CONFIG_HOME=/var/lib/gdm/.config \
				dbus-launch gsettings set x.dm.slick-greeter screen-reader false" gdm
			)
			kill -1 "$(ps -C dbus-launch -u gdm --no-headers -o pid|tail -n 1)"
		fi
		gettext "You won't have speech at login."; echo
	fi
}
[ $# -ne 1 ] && usage
choice=$(echo "$1"|tr "[:upper:]" "[:lower:]")
if [ "$(echo "$listew"|grep " $choice ")" = "" ]; then
	printf '%s ' "$1 "
	gettext "is not among the available login managers:"; echo
	echo "$listew"
	exit
fi
case $choice in
	text)
		cp /etc/inittab /etc/inittab.save
		sed "/:initdefault:/s/.*/id:3:initdefault:/" /etc/inittab.save > /etc/inittab
		gettext "Login will occur in text mode on next boot."; echo
		gettext "If you need speech type one of these commands before rebooting:"; echo
		echo "speak-with espeakup"
		echo "speak-with fenrir"
		echo "speak-with speechd-up"
		restartornot
		;;
	lightdm)
		cp /etc/inittab /etc/inittab.save
		sed "/:initdefault:/s/.*/id:4:initdefault:/" /etc/inittab.save > /etc/inittab
		cat <<-"EOF" > /etc/rc.d/rc.4.local
		#lightdm
		if [ -x /usr/sbin/lightdm ]; then
			if [ -f /var/log/lightdm/startlightdm.log ]; then
				mv /var/log/lightdm/startlightdm.log /var/log/lightdm/startlightdm.log.old
			fi
			exec /usr/sbin/lightdm  2>/var/log/lightdm/startlightdm.log
		fi
		EOF
		chmod 755 /etc/rc.d/rc.4.local
		gettext "The display manager"
		printf '%s ' " $choice "
		gettext "will be in use on next boot."; echo
		speak
		restartornot
		;;
	nodm)
		if ! nodm_configured; then
			gettext "nodm needs a user configured in /etc/nodm.conf."; echo
			gettext "Configure nodm now? [y/N]"; echo
			read -r configureNodm
			case "$configureNodm" in
			y|Y)
				configure_nodm_user || {
					gettext "nodm was not enabled."; echo
					exit 1
				}
				;;
			*)
				gettext "nodm was not enabled."; echo
				exit 1
				;;
			esac
			if ! nodm_configured; then
				gettext "nodm configuration is incomplete. nodm was not enabled."; echo
				exit 1
			fi
		fi
		if ! nodm_make_xsession_executable; then
			gettext "Could not mark the nodm session file executable. nodm was not enabled."; echo
			exit 1
		fi
		cp /etc/inittab /etc/inittab.save
		sed "/:initdefault:/s/.*/id:4:initdefault:/" /etc/inittab.save > /etc/inittab
		cat <<-"EOF" > /etc/rc.d/rc.4.local
		#nodm
		if [ -x /etc/rc.d/rc.nodm ]; then
			exec /etc/rc.d/rc.nodm start
		fi
		if [ -x /usr/sbin/nodm ]; then
			if [ -r /etc/nodm.conf ]; then
				set -a
				. /etc/nodm.conf
				set +a
			fi
			mkdir -p /var/log/nodm
			if [ -f /var/log/nodm/startnodm.log ]; then
				mv /var/log/nodm/startnodm.log /var/log/nodm/startnodm.log.old
			fi
			exec /usr/sbin/nodm --verbose --stderr --no-syslog \
				>> /var/log/nodm/startnodm.log 2>&1
		fi
		EOF
		chmod 755 /etc/rc.d/rc.4.local
		gettext "The display manager"
		printf '%s ' " $choice "
		gettext "will be in use on next boot."; echo
		restartornot
		;;
	lxdm)
		cp /etc/inittab /etc/inittab.save
		sed "/:initdefault:/s/.*/id:4:initdefault:/" /etc/inittab.save > /etc/inittab
		cat <<-"EOF" > /etc/rc.d/rc.4.local
		#lxdm
		if [ -x /usr/sbin/lxdm ]; then
		    exec /usr/sbin/lxdm -nodaemon
		fi
		EOF
		chmod 755 /etc/rc.d/rc.4.local
		gettext "The display manager"
		printf '%s ' " $choice "
		gettext "will be in use on next boot."; echo
		restartornot
		;;
	kdm)
		cp /etc/inittab /etc/inittab.save
		sed "/:initdefault:/s/.*/id:4:initdefault:/" /etc/inittab.save > /etc/inittab
		cat <<-"EOF" > /etc/rc.d/rc.4.local
		#kdm
		if [ -x /usr/bin/kdm ]; then
		    exec /usr/bin/kdm -nodaemon
		fi
		EOF
		chmod 755 /etc/rc.d/rc.4.local
		gettext "The display manager"
		printf '%s ' " $choice "
		gettext "will be in use on next boot."; echo
		restartornot
		;;
	sddm)
		cp /etc/inittab /etc/inittab.save
		sed "/:initdefault:/s/.*/id:4:initdefault:/" /etc/inittab.save > /etc/inittab
		cat <<-"EOF" > /etc/rc.d/rc.4.local
		#sddm
		if [ -x /usr/bin/sddm ]; then
		    exec /usr/bin/sddm
		fi
		EOF
		chmod 755 /etc/rc.d/rc.4.local
		gettext "The display manager"
		printf '%s ' " $choice "
		gettext "will be in use on next boot."; echo
		restartornot
		;;
	gdm)
		cp /etc/inittab /etc/inittab.save
		sed "/:initdefault:/s/.*/id:4:initdefault:/" /etc/inittab.save > /etc/inittab
		cat <<-"EOF" > /etc/rc.d/rc.4.local
		#gdm
		if [ -x /usr/sbin/gdm ]; then
		    exec /usr/sbin/gdm -nodaemon
		fi
		EOF
		chmod 755 /etc/rc.d/rc.4.local
		gettext "The display manager"
		printf '%s ' " $choice "
		gettext "will be in use on next boot."; echo
		speak
		restartornot
		;;
	*)
		gettext "Unknown choice. Check your spelling."; echo
		;;
esac
