#!/bin/sh

# we have to assume that /etc/sysconfig/desktop has two variables, DESKTOP
# and DISPLAYMANAGER because administors may prefer a specific DM regardless
# of desktops.
# DISPLAYMANAGER is referenced by this script, and DESKTOP is referenced
# as system-wide default by /etc/X11/Xsession script only when X-session
# is opened by "startx" command. 
# when DMs open an X-session, they send DESKTOP, which is in this case
# directly selected by users, as a commandline argument of /etc/X11/Xsession.
# actually Xsession script is only able to know by existance of its first
# argument whether it is called by DM or "startx". see the logic
# in /etc/X11/Xsession.
# If DISPLAYMANAGER is not defined, then assume that it is the same as DESKTOP
dm_service=
if [ -f /etc/sysconfig/desktop ]; then
	. /etc/sysconfig/desktop >/dev/null 2>&1
	[ -z "$DISPLAYMANAGER" ] && DISPLAYMANAGER=$DESKTOP
	if [ "$DISPLAYMANAGER" = "GDM" -o "$DISPLAYMANAGER" = "gdm" -o "$DISPLAYMANAGER" = "GNOME" -o "$DISPLAYMANAGER" = "gnome" -o "$DISPLAYMANAGER" = "Gnome" ]; then
	    dm_service=gdm
	elif [ "$DISPLAYMANAGER" = "KDM" -o "$DISPLAYMANAGER" = "kdm" ]; then
	    dm_service=kdm
	elif [ "$DISPLAYMANAGER" = "XDM" -o "$DISPLAYMANAGER" = "xdm" ] ; then
	    dm_service=xdm
	elif [ "$DISPLAYMANAGER" = "SLiM" -o "$DISPLAYMANAGER" = "slim" ] ; then
	    dm_service=slim
	elif [ "$DISPLAYMANAGER" = "LIGHTDM" -o "$DISPLAYMANAGER" = "lightdm" -o "$DISPLAYMANAGER" = "LightDM" ] ; then
	    dm_service=lightdm
	else
		exit 0
	fi
fi

#disable display-mananger.service symlink and enable preffered dm. If dm = null exit

rm -f /etc/systemd/system/display-manager.service
/bin/systemctl enable $dm_service.service

if [ "$dm_service" = "gdm" ]; then
	/usr/sbin/update-alternatives --set gdmflexiserver /usr/lib/gdm/gdmflexiserver
fi

if [ "$dm_service" = "lightdm" ]; then
	/usr/sbin/update-alternatives --set gdmflexiserver /usr/lib/lightdm/gdmflexiserver
fi

exit 1
