#!/bin/sh

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

# 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
# We need to source this so that the login screens get translated
. /etc/profile.d/10lang.sh
[ "$DESKTOP" = "dummy_DESKTOP_variable" ] && unset DESKTOP

# Try autologin first, if wanted...
if [ -f /etc/sysconfig/autologin -a -x /usr/sbin/autologin ]; then
	. /etc/sysconfig/autologin
	if [ -n "$USER" -a "$AUTOLOGIN" = yes ]; then
	    # shut down boot splash
	    [ -x /bin/plymouth ] && /bin/plymouth quit
	    # force shut down boot splash if not yet done
	    if [ -n $(/bin/pidof plymouth) ]; then
	    	/bin/pkill -SIGTERM plymouth
	    fi
	    /usr/sbin/autologin &
	    echo -n $$ > /var/lock/subsys/dm
	    wait $!
	fi
fi

# 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=
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=GNOME
	elif [ "$DISPLAYMANAGER" = "SDDM" -o "$DISPLAYMANAGER" = "sddm" -o "$DISPLAYMANAGER" = "PLASMA5" -o "$DISPLAYMANAGER" = "plasma5" ]; then
	    dm=SDDM
	    plymouth_quit=yes
	elif [ "$DISPLAYMANAGER" = "WDM" -o "$DISPLAYMANAGER" = "wdm" ] ; then
	    dm=WDM
	    plymouth_quit=yes
	elif [ "$DISPLAYMANAGER" = "XDM" -o "$DISPLAYMANAGER" = "xdm" ] ; then
	    dm=XDM
	    plymouth_quit=yes
	elif [ "$DISPLAYMANAGER" = "SLiM" -o "$DISPLAYMANAGER" = "slim" ] ; then
	    dm=SLiM
	    plymouth_quit=yes
	elif [ "$DISPLAYMANAGER" = "LXDM" -o "$DISPLAYMANAGER" = "lxdm" ] ; then
	    dm=LXDM
	elif [ -n "$DISPLAYMANAGER" ]; then
	    dm=$DISPLAYMANAGER
	    plymouth_quit=yes
	fi
fi

[ -f /etc/sysconfig/firstboot ] && . /etc/sysconfig/firstboot
[ -f /etc/sysconfig/finish-install ] && . /etc/sysconfig/finish-install
grep -q '\binstall\b' /proc/cmdline
RETVAL=$?
if  [ \( "x$FINISH_INSTALL" = "xyes" -a -x /usr/sbin/finish-install \) -o \( "x$FIRSTBOOT" = "xyes" -a -x /usr/bin/mozilla-firefox \) -o \( $RETVAL = 0 \)  ]; then
  # unconditionally shut down boot splash
  [ -x /bin/plymouth ] && /bin/plymouth quit
  [ -n $(/bin/pidof plymouth) ] && /bin/pkill -SIGTERM plymouth
  # and run first-time actions
  /usr/bin/xinit /etc/X11/xdm/Xsetup_0
else
  # shut down boot splash for DMs that don't handle it themselves
  [ -x /bin/plymouth ] && [ "x$plymouth_quit" = "xyes" ] && /bin/plymouth quit
  [ -n $(/bin/pidof plymouth) ] && [ "x$plymouth_quit" = "xyes" ] && /bin/pkill -SIGTERM plymouth
fi

shopt -s execfail

preferred=`/etc/X11/lookupdm "$dm"`
[ -n "$preferred" ] && exec $preferred "$@" >/dev/null 2>&1 </dev/null

# Fallbacks, in order
exec gdm "$@" >/dev/null 2>&1 </dev/null
exec sddm "$@" >/dev/null 2>&1 </dev/null

# catch all exit error
[ -x /bin/plymouth ] && /bin/plymouth quit
# force shut down boot splash if not yet done
if [ -n $(/bin/pidof plymouth) ]; then
	/bin/pkill -SIGTERM plymouth
fi
exit 1
