#! /bin/sh
### BEGIN INIT INFO
# Provides:          lightdm
# Should-Start:      console-screen kbd acpid dbus hal consolekit
# Required-Start:    $local_fs $remote_fs x11-common
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     5
# Default-Stop:      0 1 6
# Short-Description: Light Display Manager
# Description:       Debian init script for the Light Display Manager
### END INIT INFO
#
# Author:       Yves-Alexis Perez <corsac@debian.org> using gdm script from 
#               Ryan Murray <rmurray@debian.org>
#
set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/lightdm

test -x $DAEMON || exit 0

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

. /lib/lsb/init-functions

# To start lightdm 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

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# don't run in systemd
[ /proc/1/exe -ef /usr/lib/systemd/systemd ] &&  exit 0

#start systemd-logind if exists
#don't start if elogind exists, presuming elogind will be started by init.d/elogind
if [ ! -x /usr/libexec/elogind ]; then
	if ! pgrep -x systemd-logind >/dev/null; then  
    	if [ -x /usr/lib/systemd/systemd-logind ]; then
        	mkdir -p /run/systemd/seats  2>/dev/null || :
        	export SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1
        	/usr/lib/systemd/systemd-logind 2>/dev/null & 
    	fi
	fi
fi
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

case "$1" in
  start)
        CONFIGURED_DAEMON=$(basename "$(cat $DEFAULT_DISPLAY_MANAGER_FILE 2> /dev/null)")
        if grep -wqs text /proc/cmdline; then
            log_warning_msg "Not starting Light Display Manager (lightdm); found 'text' in kernel commandline."
        elif [ -e "$DEFAULT_DISPLAY_MANAGER_FILE" ] && \
           [ "$HEED_DEFAULT_DISPLAY_MANAGER" = "true" ] && \
           [ "$CONFIGURED_DAEMON" != lightdm ] ; then
                log_action_msg "Not starting Light Display Manager; it is not the default display manager"
        else
                log_daemon_msg "Starting Light Display Manager" "lightdm"
                start-stop-daemon --start --quiet --pidfile /var/run/lightdm.pid --name lightdm --exec $DAEMON -b|| echo -n " already running"
                log_end_msg $?
        fi
  ;;
  stop)
  #check to see if lightdm is running, filter out grep search.  if not, don't do anything
  if [ -n "$(ps -aux |grep  "/usr/sbin/lightd[m]")" ]; then
  	log_daemon_msg "Stopping Light Display Manager" "lightdm"
        set +e
	start-stop-daemon --stop --quiet --pidfile /var/run/lightdm.pid \
               --name lightdm --retry 5
        set -e
        log_end_msg $?
     tsplash="/live/bin/tsplash"
     if [ -x "$tsplash" -a -d /live/config/tsplash ]; then
         case $(runlevel) in
                 *0) openvt -s -c10 $tsplash shutdown 
				      exit 0
				      ;;
                 *6) openvt -s -c10 $tsplash reboot  
                      exit 0 
                      ;;
         esac
     fi
     [ -d /live/linux ] &&  CHVT=1 || CHVT=8
     case $(runlevel) in
                 *0)  chvt $CHVT
                      exit 0
                      ;;
                 *6)  chvt $CHVT
                      exit 0 
                      ;;
     esac
  fi
  exit 0
  ;;
  reload)
        log_daemon_msg "Scheduling reload of Light Display Manager configuration" "lightdm"
        set +e
        start-stop-daemon --stop --signal USR1 --quiet --pidfile \
                /var/run/lightdm.pid --name lightdm
        set -e
        log_end_msg $?
  ;;
  status)
        status_of_proc -p "$PIDFILE" "$DAEMON" lightdm && exit 0 || exit $?
  ;;
  restart|force-reload)
        $0 stop
        sleep 1
        $0 start
  ;;
  *)
        echo "Usage: /etc/init.d/lightdm {start|stop|restart|reload|force-reload|status}"
        exit 1
  ;;
esac

exit 0
