#! /bin/sh
# postinst script for kdm
#
# Mostly stolen from the Debian xdm scripts
# Copyright 1998, 1999 Branden Robinson.  Licensed under the GNU GPL.
# Acknowlegements to Stephen Early, Mark Eichin, and Manoj Srivastava.
#
# see: dh_installdeb(1)

set -e

. /usr/share/debconf/confmodule
 
THIS_PACKAGE=kdm
THIS_DISPLAY_MANAGER=/usr/bin/kdm
DEFAULT_DISPLAY_MANAGER_FILE=/etc/X11/default-display-manager

case "$1" in
    configure)
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

# debconf is not a registry, so we only fiddle with the default file if it
# does not exist
if [ ! -e $DEFAULT_DISPLAY_MANAGER_FILE ]; then
  db_get shared/default-x-display-manager
  if [ "$RET" = "$THIS_PACKAGE" ]; then
    echo "$THIS_DISPLAY_MANAGER" > $DEFAULT_DISPLAY_MANAGER_FILE
  fi
fi

db_stop

nostart=
kdm_running=
# don't start kdm if we are upgrading without stopping it
if [ -e /var/run/kdm.upgrade ]; then
  nostart=yes
fi
# or if we're currently in X on the display it attempts to manage by default
for hostname in "" "localhost" "$(hostname)" "$(hostname -f)"; do
  if echo $DISPLAY | grep -q "^$hostname:0.*"; then
    nostart=yes
  fi
done
# or if it's already running
if start-stop-daemon --stop --quiet --signal 0 --pid /var/run/kdm.pid --exec /usr/bin/kdm; then
  nostart=yes
  kdm_running=yes
fi
# or if the options file says not to
if ! grep -qs ^restart-on-upgrade /etc/kde3/debian/kdm.options; then
  nostart=yes
fi

[ -n "$nostart" ] || /etc/init.d/kdm start > /dev/tty || true

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

