#!/bin/sh

# $Id: xdm.init 996 2006-01-02 19:27:34Z ender $

# Copyright 1998-2002, 2004, 2005 Branden Robinson <branden@debian.org>.
# Copyright 2006 Eugene Konev <ejka@imfi.kspu.ru>
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License with
# the Debian operating system, in /usr/share/common-licenses/GPL;  if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA

set -e

# To start xdm even if it is not the default display manager, change
# HEED_DEFAULT_DISPLAY_MANAGER to "false."
# Also overridable from command line like:
# HEED_DEFAULT_DISPLAY_MANAGER=false /etc/init.d/xdm start
[ -z "$HEED_DEFAULT_DISPLAY_MANAGER" ] && HEED_DEFAULT_DISPLAY_MANAGER=true

DEFAULT_DISPLAY_MANAGER_FILE=/etc/X11/default-display-manager

PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/bin/X11/xdm
PIDFILE=/var/run/xdm.pid

test -x $DAEMON || exit 0

# If we have upgraded the daemon since we last started it, we can't use the
# --exec argument to start-stop-daemon, because the daemon's inode will have
# changed.  The risk here is that in a situation where the daemon died, its
# pidfile was not cleaned up, we've upgraded it, *and* some other process is now
# running under that pid, start-stop-daemon will send signals to an innocent
# process.  However, this seems like a corner case.  C'est la vie!
# Update: --name should prevent signalling innocent processes.
SSD_START_ARGS="--pidfile $PIDFILE --name $(basename $DAEMON) --startas $DAEMON"
SSD_STOP_ARGS="--pidfile $PIDFILE --name $(basename $DAEMON) --retry TERM/5/TERM/5"
SSD_RELOAD_ARGS="--pidfile $PIDFILE --name $(basename $DAEMON) --signal 1"

case "$1" in
  start)
    if [ "$HEED_DEFAULT_DISPLAY_MANAGER" = "true" ] &&
       [ -e $DEFAULT_DISPLAY_MANAGER_FILE ] &&
       [ "$(cat $DEFAULT_DISPLAY_MANAGER_FILE)" != "$DAEMON" ]; then
      echo "Not starting X display manager (xdm); it is not the default" \
        "display manager."
    else
      echo -n "Starting X display manager: xdm"
      start-stop-daemon --start --quiet $SSD_START_ARGS \
        || echo -n " already running"
      echo "."
    fi
  ;;

  restart)
    /etc/init.d/xdm stop
    [ -f $PIDFILE ] && exit 1
    /etc/init.d/xdm start
  ;;

  reload)
    echo -n "Reloading X display manager configuration..."
    if start-stop-daemon --stop --quiet $SSD_RELOAD_ARGS; then
      echo "done."
    else
      echo "xdm not running."
    fi
  ;;

  force-reload)
    /etc/init.d/xdm reload
  ;;

  stop)
    echo -n "Stopping X display manager: xdm"
    if ! [ -f $PIDFILE ]; then
      echo -n " not running ($PIDFILE not found)"
    else
      start-stop-daemon --stop --quiet $SSD_STOP_ARGS
      SSD_RES=$?
      if [ $SSD_RES -eq 1 ]; then
        echo -n " not running"
      fi
      if [ $SSD_RES -eq 2 ]; then
        echo -n " not responding to TERM signals"
      else
	if [ -f $PIDFILE ]; then
	  echo -n " (removing stale $PIDFILE)"
	  rm $PIDFILE
	fi
      fi
    fi
    echo "."
  ;;

  *)
    echo "Usage: /etc/init.d/xdm {start|stop|restart|reload|force-reload}"
    exit 1
    ;;
esac

exit 0

# vim:set ai et sts=2 sw=2 tw=80:
