#!/bin/sh
#
# prefdm:         Starts the preferred Display Manager
#
# chkconfig: 5 26 74
# description:  This is a daemon which handles user autentication 
# processname: /etc/X11/prefdm
# config: /etc/sysconfig/desktop
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin

. /etc/init.d/functions

# shut down any graphical boot that might exist
if [ -x /usr/bin/rhgb-client ]; then
    /usr/bin/rhgb-client -quit
fi

# We need to source this so that the login screens get translated
[ -f /etc/profile.d/lang.sh ] && . /etc/profile.d/lang.sh

# Run preferred X display manager
preferred=
if [ -f /etc/sysconfig/desktop ]; then
	. /etc/sysconfig/desktop
	if [ "$DISPLAYMANAGER" = GNOME ]; then
		preferred=gdm
	elif [ "$DISPLAYMANAGER" = KDE ]; then
		preferred=kdm
	elif [ "$DISPLAYMANAGER" = XDM ]; then
	        preferred=xdm
	fi
fi

if [ -z "$preferred" ]; then
	if which gdm >/dev/null 2>&1; then
		preferred=gdm
	elif which kdm >/dev/null 2>&1; then
		preferred=kdm
	elif which xdm >/dev/null 2>&1; then
		preferred=xdm
	fi
fi

start() {
	echo -n $"Starting $preferred: "
	if [ -n "$preferred" ] && which $preferred >/dev/null 2>&1; then 
		exec `which $preferred` $* >/dev/null 2>&1
	fi
	if which gdm >/dev/null 2>&1; then
		exec `which gdm` $* >/dev/null 2>&1
	elif which kdm >/dev/null 2>&1; then
		exec `which kdm` $* >/dev/null 2>&1
	elif which xdm >/dev/null 2>&1; then
		exec `which xdm` $* >/dev/null 2>&1
	fi
	RETVAL=$?
	echo
    	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/prefdm
    	return $RETVAL
}

stop () {
	echo -n $"Stopping $prog: "
	( killproc `which gdm` || killproc `which kdm` || killproc `which xdm` )
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
        	rm -f /var/lock/subsys/prefdm
        	success $"$prog shutdown"
    	else
        	failure $"$prog shutdown"
    	fi
    	echo
    	return $RETVAL
}

restart() {
    stop
    start
}

case "$1" in
    start)
        start
        RETVAL=$?
        ;;
    stop)
        stop
        RETVAL=$?
        ;;
    status)
        status prefdm 
        RETVAL=$?
        ;;
    restart)
        restart
        RETVAL=$?
        ;;
    condrestart)
        [ -e /var/lock/subsys/prefdm ] && restart
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        RETVAL=1
        ;;
esac
exit $RETVAL
