#!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
HOME=/root
export HOME

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

# trap SIGTERM to be able to kill autologin
killed () {
    kill -15 $!
    exit
}

trap killed 15

# Run preferred X display manager

# Try autologin first, if wanted...
if [ -f /etc/sysconfig/autologin -a -x /usr/sbin/autologin ]; then
	/usr/sbin/autologin &
	wait $!
fi

preferred=
if [ -f /etc/sysconfig/desktop ]; then
	. /etc/sysconfig/desktop >/dev/null 2>&1
	[ -n "$DISPLAYMANAGER" ] && DESKTOP=$DISPLAYMANAGER
	if [ "$DESKTOP" = "GNOME" -o "$DESKTOP" = "Gnome" ]; then
		preferred=gdm
	elif [ "$DESKTOP" = "KDE" -o "$DESKTOP" = "KDE1" -o "$DESKTOP" = "KDE2" ]; then
		preferred=/usr/bin/kdm
	elif [ "$DESKTOP" = "AnotherLevel" ] ; then
                preferred=/usr/X11R6/bin/xdm
	fi
fi
# xdm-like program are launched from the console, however, the locale-setting
# can be done in a way that console is not localize, while X11 is.
# That is handled by the lang.sh script, depending on the existance of
# $DISPLAY or $DESKTOP variable. Now that $DESKTOP is defined resource lang.sh
[ -z "$DESKTOP" ] && DESKTOP=dummy_DESKTOP_variable
. /etc/profile.d/lang.sh
[ "$DESKTOP" = "dummy_DESKTOP_variable" ] && unset DESKTOP


if [ -n "$preferred" ] && which $preferred >/dev/null 2>&1; then 
	exec `which $preferred` $* >/dev/null 2>&1
fi
if which kdm >/dev/null 2>&1; then
	exec `which kdm` $* >/dev/null 2>&1
elif which gdm >/dev/null 2>&1; then
	exec `which gdm` $* >/dev/null 2>&1
elif which xdm >/dev/null 2>&1; then
	exec `which xdm` $* >/dev/null 2>&1
fi
exit 1
