#!/bin/sh
# Debian wdm package post-installation script
# Copyright 2001 Branden Robinson.
# Copyright 2005 Vladimir Shakhov.
# 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>.

set -e

# source debconf library
. /usr/share/debconf/confmodule

THIS_PACKAGE=wdm
THIS_DISPLAY_MANAGER=/usr/bin/wdm
DEFAULT_DISPLAY_MANAGER_FILE=/etc/X11/default-display-manager

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;
}

# debconf is not a registry, so we only fiddle with the default file if it
# does not exist
DEFAULT_DISPLAY_MANAGER_FILE=/etc/X11/default-display-manager
if [ ! -e "$DEFAULT_DISPLAY_MANAGER_FILE" ]; then
  DEFAULT_DISPLAY_MANAGER=
  if db_get shared/default-x-display-manager; then
    DEFAULT_DISPLAY_MANAGER="$RET"
  fi
  if [ -n "$DEFAULT_DISPLAY_MANAGER" ]; then
    DAEMON_NAME=
    if db_get "$DEFAULT_DISPLAY_MANAGER"/daemon_name; then
      DAEMON_NAME="$RET"
    fi
    if [ ! -n "$DAEMON_NAME" ]; then
      # if we were unable to determine the name of the selected daemon (for
      # instance, if the selected default display manager doesn't provide a
      # daemon_name question), guess
      DAEMON_NAME=$(which "$DEFAULT_DISPLAY_MANAGER" 2>/dev/null)
      if [ ! -n "$DAEMON_NAME" ]; then
        echo .
        # FIXME - redo this part uses shell-lib.sh from xfree86
        #warn "unable to determine path to default X display manager" \
        #     "$DEFAULT_DISPLAY_MANAGER; not updating" \
        #     "$DEFAULT_DISPLAY_MANAGER_FILE"
      fi
    fi
    if [ -n "$DAEMON_NAME" ]; then
      # FIXME - redo this part uses shell-lib.sh from xfree86
      #observe "committing change of default X display manager"
      echo "$DAEMON_NAME" > "$DEFAULT_DISPLAY_MANAGER_FILE"
    fi
  fi
fi

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

if [ -n "$WDM_RUNNING" ]; then
  if [ -d /var/state/wdm ]; then
    message "Note: obsolete directory /var/state/wdm cannot be removed" \
            "because wdm is still running.  Reinstall the wdm package" \
            "(or remove the directory manually) when wdm is not running."
  fi
else
  if [ -d /var/state/wdm ]; then
    rm -r /var/state/wdm
  fi
fi

update_wdm_wmlist
update-rc.d wdm defaults 99 01 > /dev/null 2>&1

# redirect stdout to /dev/tty so debconf doesn't choke on it
[ -n "$NOSTART" ] || if which invoke-rc.d >/dev/null 2>&1; then
    invoke-rc.d package start
  else
    /etc/init.d/package start
fi  > /dev/tty || true

#DEBHELPER#

exit 0

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