#! /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:     2 3 4 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

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)
        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 $?
  ;;
  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
