#!/bin/sh
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: To get the display manager of server

# The following is borrowed from Mandrake 9.2
# 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
preferred=
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
	    preferred=gdm
	elif [ "$DISPLAYMANAGER" = "KDE" -o "$DISPLAYMANAGER" = "kde" ]; then
	    preferred=mdkkdm
	elif [ "$DISPLAYMANAGER" = "KDM" -o "$DISPLAYMANAGER" = "kdm" ]; then
	    preferred=kdm
	elif [ "$DISPLAYMANAGER" = "XDM" -o "$DISPLAYMANAGER" = "xdm" ] ; then
	    preferred=xdm
	fi
fi

# if the preferred dm is not found, set the default one
# For Redhat/FC, the default dm is gdm, for Mandrake, the default dm is kdm
if [ -z "$preferred" ]; then
   if [ -n "$(grep "Fedora Core release" /etc/issue)" -o -n "$(grep "Red Hat Linux release" /etc/issue)" ]; then
      # For Redhat/FC, the default dm is gdm, make sure it exists
      which gdm &>/dev/null && preferred="gdm"
   elif [ -n "$(grep "Mandrake Linux release" /etc/issue)" -o -n "$(grep "Mandrakelinux release" /etc/issue)" ]; then
      # For Mandrake, the default dm is kdm or mdkkdm, make sure it exists
      which kdm &>/dev/null && preferred="kdm"
      which mdkkdm &>/dev/null && preferred="mdkkdm"
   else
      echo "Can NOT find gdm or kdm in your system... `basename ${0}` can NOT continue..."
      exit 1
   fi
fi
echo $preferred
exit 0
