#!/bin/sh
# Debian kdm package pre-removal script
# stolen from the xdm scripts
# Copyright 1998-2001 Branden Robinson.
# Licensed under the GNU General Public License, version 2.  See the file
# /usr/share/common-licenses/GPL or <http://www.gnu.org/copyleft/gpl.txt>.
# Acknowlegements to Stephen Early, Mark Eichin, and Manoj Srivastava.

set -e

message () {
  # pretty-print messages of arbitrary length
  echo "$*" | fold -s -w ${COLUMNS:-80} >&2;
}

message_nonl () {
  # pretty-print messages of arbitrary length (no trailing newline)
  echo -n "$*" | fold -s -w ${COLUMNS:-80} >&2;
}

errormsg () {
  # exit script with error
  message "$*"
  exit 1;
}

parseans () {
  if [ ! $1 ]; then
    echo $DEFAULT
  else
    echo $1 | cut -c1 | tr '[A-Z]' '[a-z]';
  fi;
}


THIS_PACKAGE=kdm

case "$1" in
  # we NEVER want to stop kdm without doing some checks first, see below
  upgrade|failed-upgrade)
    REMOVING=
    ;;
  remove|deconfigure)
    REMOVING=yes
    ;;
esac

STOP=
# are we supposed to restart on upgrade?  if REMOVING kdm, we don't care
if grep -qs ^restart-on-upgrade /etc/kde3/debian/kdm.options || [ "$REMOVING" ]; then
  # is there an kdm process running?
  if start-stop-daemon --stop --quiet --signal 0 --exec /usr/bin/kdm; then
    # any children?
    PARENTS=$(pidof /usr/bin/kdm || true)
    CHILDREN=
    for PROCESS in $PARENTS; do
      # make sure we got numbers back
      if ! [ $PROCESS -eq $PROCESS ] 2> /dev/null; then
        # freak out
        errormsg "ERROR: pidof returned non-numeric value!"
      fi
      # we could use grep -q here if ps would ignore SIGPIPE :-P
      if (ps axj | grep "^ *$PROCESS" > /dev/null 2>&1); then
        CHILDREN=yes
      fi
    done
    if [ "$CHILDREN" ]; then
      message "WARNING: Preparing to stop K display manager (kdm) daemon," \
              "and it appears to be managing at least one running X" \
              "session.  If kdm is stopped now, any X sessions it manages" \
              "will be terminated.  Otherwise you may leave kdm running," \
              "and the new version will take effect the next time kdm is" \
              "restarted."
      message
      DEFAULT=n
      VALID_ANSWER=
      while [ ! "$VALID_ANSWER" ]; do
        message_nonl "Do you wish to stop the kdm daemon? (y/n) [$DEFAULT] "
        read ANSWER
        ANSWER=$(parseans $ANSWER)
        case "$ANSWER" in
          y) VALID_ANSWER=true; STOP=yes ;;
          n) VALID_ANSWER=true ;;
          *) message "\"$ANSWER\" not understood." ;;
        esac
      done
    else
      STOP=yes
    fi
  fi
fi

if [ "$STOP" ]; then
  /etc/init.d/kdm stop || true
else
  if [ "$1" = "upgrade" -o "$1" = "failed-upgrade" ]; then
    touch /var/run/kdm.upgrade
  fi
fi

if [ "$1" = "remove" -o "$1" = "deconfigure" ]; then
  if [ -e /usr/share/debconf/confmodule ]; then
    . /usr/share/debconf/confmodule
    # disown this question
    db_unregister shared/default-x-display-manager
    # does the question still exist?
    if db_get shared/default-x-display-manager; then
      db_metaget shared/default-x-display-manager owners
      db_subst shared/default-x-display-manager choices "$RET"
      db_metaget shared/default-x-display-manager value
      if [ "$THIS_PACKAGE" = "$RET" ]; then
        db_fset shared/default-x-display-manager isdefault true
        db_input critical shared/default-x-display-manager || true
        db_go
        db_get shared/default-x-display-manager
        message "Please be sure to run \"dpkg-reconfigure $RET\"."
      fi
    fi
  fi
fi

#DEBHELPER#

exit 0

