#!/bin/sh

set -e

. /usr/share/debconf/confmodule

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

# creating gdm group if he isn't already there
if ! getent group gdm >/dev/null; then
        addgroup --system gdm
fi

# creating gdm user if he isn't already there
if ! getent passwd gdm >/dev/null; then
        adduser --system --ingroup gdm --home /var/lib/gdm gdm
        usermod -c "Gnome Display Manager" gdm
        usermod -d "/var/lib/gdm"          gdm
        usermod -g "gdm"                   gdm
        usermod -s "/bin/false"            gdm
fi

if [ -d /var/lib/gdm ]; then
  chown gdm:gdm /var/lib/gdm
  chown -R gdm:gdm /var/lib/gdm/.gconf*
  chmod 0750 /var/lib/gdm
fi

# 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
  if db_get shared/default-x-display-manager; then
    # workaround debconf passthru bug (#379198)
    if [ -z "$RET" ]; then
      $RET="$THIS_PACKAGE"
    fi
    if [ "$THIS_PACKAGE" != "$RET" ]; then
      echo "Please be sure to run \"dpkg --configure $RET\"."
    fi
    if db_get "$RET"/daemon_name; then
      echo "$RET" > $DEFAULT_DISPLAY_MANAGER_FILE
    fi
  fi
fi
# debconf hangs if gdm gets started below without this
db_stop || true

PID=$(status "gdm" 2>/dev/null | awk '/[0-9]$/ { print $NF }')
[ -z "$PID" ] || kill -HUP $PID

#DEBHELPER#

exit 0
