#! /bin/sh
#
# Originally based on:
# Version:	@(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
#
# Modified for gdm, Steve Haslam <steve@arise.dmeon.co.uk> 14mar99
# modified to remove --exec, as it does not work on upgrades. 18jan2000
# modified to use --name, to detect stale PID files 18mar2000
# sleep until gdm dies, then restart it 16jul2000
# get along with other display managers (Branden Robinson, Ryan Murray) 05sep2001
#
# ::copy::
# ::maintainer::
#
# modified to include /etc/default/gdm file for environment custom settings

set -e

# To start gdm even if it is not the default display manager, change
# HEED_DEFAULT_DISPLAY_MANAGER to "false."
HEED_DEFAULT_DISPLAY_MANAGER=true
DEFAULT_DISPLAY_MANAGER_FILE=/etc/X11/default-display-manager
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/gdm
PIDFILE=/var/run/gdm.pid
UPGRADEFILE=/var/run/gdm.upgrade

if [ -e $UPGRADEFILE -a "$1" != "restart" -a "$1" != "force-reload" ]; then
	SSD_ARG="--startas $DAEMON"
	rm -f $UPGRADEFILE
else
	SSD_ARG="--exec $DAEMON"
fi

# Allow cdd to override the config
if [ -f /etc/gdm/gdm-cdd.conf ]; then
	CONFIG_FILE="--config=/etc/gdm/gdm-cdd.conf"
fi

test -x $DAEMON || exit 0

if [ -r /etc/default/locale ]; then
  . /etc/default/locale
  export LANG LANGUAGE
elif [ -r /etc/environment ]; then
  . /etc/environment
  export LANG LANGUAGE
fi

#[ku]
# include environment overrides
[ -r /etc/default/gdm ] && . /etc/default/gdm

. /lib/lsb/init-functions

case "$1" in
  start)
        if grep -wqs text /proc/cmdline; then
		log_warning_msg "Not starting GNOME Display Manager (gdm); found 'text' in kernel commandline."
  	elif [ -e "$DEFAULT_DISPLAY_MANAGER_FILE" -a "$HEED_DEFAULT_DISPLAY_MANAGER" = "true" -a "$(cat $DEFAULT_DISPLAY_MANAGER_FILE 2>/dev/null)" != "$DAEMON" ]; then
		log_warning_msg "Not starting GNOME Display Manager (gdm); it is not the default display manager."
	else
		if [ -z "$SPLASH_ORIG_CONSOLE" ]; then
			log_begin_msg "Starting GNOME Display Manager..."
		fi
		# if usplash is running, make sure to stop it now, yes "start" kills it.
		if [ "$SPLASH_ORIG_CONSOLE" ]; then
			# usplash was already shut down earlier, so don't
			# log success as it will look weird on the console.
			log_end_msg=:
	        elif pidof usplash > /dev/null; then
			SPLASH_ORIG_CONSOLE="$(fgconsole)"
			DO_NOT_SWITCH_VT=yes /etc/init.d/usplash start
			# We've just shut down usplash, so don't log
			# success as it will look weird on the console.
			log_end_msg=:
		else
			log_end_msg=log_end_msg
		fi
		start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --name gdm $SSD_ARG -- $CONFIG_FILE >/dev/null 2>&1 || log_end_msg 1
		$log_end_msg 0

		if [ "$SPLASH_ORIG_CONSOLE" ] && \
		   [ "$SPLASH_ORIG_CONSOLE" != serial ]; then
			# Wait a short while for the active console to
			# change, to try to avoid visible console noise from
			# later init scripts.
			i=0
			while [ "$(fgconsole)" = "$SPLASH_ORIG_CONSOLE" ]; do
				i="$(($i + 1))"
				if [ "$i" -gt 50 ]; then
					break
				fi
				sleep 0.1
			done
		fi
	fi
  ;;
  stop)
	log_begin_msg "Stopping GNOME Display Manager..."
	start-stop-daemon --stop  --quiet --oknodo --pidfile $PIDFILE --name gdm $SSD_ARG --retry 30 >/dev/null 2>&1
	log_end_msg 0
  ;;
  reload)
	log_begin_msg "Reloading GNOME Display Manager configuration..."
	log_warning_msg "Changes will take effect when all current X sessions have ended."
	start-stop-daemon --stop --signal USR1 --quiet --pidfile \
		$PIDFILE --name gdm $SSD_ARG >/dev/null 2>&1
	log_end_msg 0
  ;;
  restart|force-reload)
	$0 stop || true
	$0 start
  ;;
  status)
	status_of_proc -p "$PIDFILE" "$DAEMON" gdm && exit 0 || exit $?
  ;;
  *)
	log_success_msg "Usage: /etc/init.d/gdm {start|stop|restart|reload|force-reload|status}"
	exit 1
  ;;
esac

exit 0
