#!/bin/sh -e

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

check_and_create_group() {
	if [ ! "`getent group nut`" ]; then
		addgroup --quiet --system nut
#	else
#		printf "error: user \"nut\" already exists... see /usr/share/doc/nut/README.Debian\n"  &&  exit 1
	fi
}

check_and_create_user() {
	if [ ! "`getent passwd nut`" ]; then
		adduser --quiet --system --ingroup nut --home /var/lib/nut --no-create-home nut
#	else
#		printf "error: user \"nut\" already exists... see /usr/share/doc/nut/README.Debian\n"  &&  exit 1
	fi
}

case "$1" in

  install)
    # Check for existing nut system account, and create it if needed
    check_and_create_group
    check_and_create_user
    ;;

  upgrade)
    # Check for existing nut system account, and create it if needed
    check_and_create_group
    check_and_create_user

    # remove the obsolete config file (upgrading from nut < 0.44.0)
    db_get nut/remove_debian_conf
    if [ "$RET" = "true" ] ; then
      rm -f /etc/nut/debian.conf
    fi

    # modify the nut user's home dir (upgrading from nut < 0.44.0)
    if dpkg --compare-versions "$2" lt-nl "0.44.0" ; then
      usermod -d /var/lib/nut -m nut
    fi

    # modify the nut user again (upgrading from nut < 0.44.1-3)
    if dpkg --compare-versions "$2" lt-nl "0.44.1-3" ; then
      # remove the nut user for the dialout group
      usermod -G "" nut
      # try to make a nut group with gid equal to uid of the nut user
      if ! [ `addgroup --gid \`id -u nut\` nut` ] ; then
        # oh well, gid and uid will not be the same
        addgroup nut
      fi
      # change the nut user's primary grou
      usermod -g nut nut
    fi

    # stop the daemon (upgrading from nut < 1.1.11)
    if dpkg --compare-versions "$2" lt-nl "1.1.11" ; then
      [ -x /etc/init.d/nut ] && /etc/init.d/nut stop
    fi

    # remove the obsolete udev file
    [ -f /etc/udev/rules.d/025_nut-usbups.rules ] && rm -f /etc/udev/rules.d/025_nut-usbups.rules
    ;;

  abort-upgrade)
    # do nothing
    ;;

  *)
    echo "$0: incorrect arguments: $*" >&2
    exit 1
    ;;
esac

#DEBHELPER#
db_stop


syntax highlighted by Code2HTML, v. 0.9.1