#!/bin/sh
# preinst script for freenet
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <new-preinst> `install'
#        * <new-preinst> `install' <old-version>
#        * <new-preinst> `upgrade' <old-version>
#        * <old-preinst> `abort-upgrade' <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    install|upgrade)
       # If the package has default file it could be sourced, so that
       # the local $SERVER_GROUPin can overwrite the defaults

       [ -f "/etc/default/freenet" ] && . /etc/default/freenet

       # Sane defaults:

       [ -z "$SERVER_HOME" ] && SERVER_HOME="/var/lib/freenet"
       [ -z "$SERVER_USER" ] && SERVER_USER=freenet
       [ -z "$SERVER_GROUP" ] && SERVER_GROUP=freenet
       [ -z "$SERVER_NAME" ] && SERVER_NAME="Freenet REference Daemon"
       [ -z "$SERVER_LOG" ] && SERVER_LOG="/var/log/freenet"
       [ -z "$SERVER_ETC" ] && SERVER_ETC="/etc/freenet"

       # Groups that the user will be added to, if undefined, then none.
       ADDGROUP="freenet"

       # create user to avoid running server as root
       # 1. create group if not existing
       if ! getent group | grep -q "^$SERVER_GROUP:" ; then
           echo -n "Adding group $SERVER_GROUP.."
          addgroup --quiet --system $SERVER_GROUP 2>/dev/null ||true
          echo "..done"
       fi
       # 2. create homedir if not existing
       test -d $SERVER_HOME || mkdir $SERVER_HOME
       test -d $SERVER_LOG || mkdir $SERVER_LOG
       test -d $SERVER_ETC || mkdir $SERVER_ETC
       # 3. create user if not existing
       if ! getent passwd | grep -q "^$SERVER_USER:"; then
           echo -n "Adding system user $SERVER_USER.."
           adduser --quiet \
               --system \
               --shell /bin/sh \
               --ingroup $SERVER_GROUP \
               --no-create-home \
               --home /nonexistent \
               --disabled-password \
               $SERVER_USER 2>/dev/null || true
           echo "..done"
       fi
       # 4. adjust passwd entry
       usermod -c "$SERVER_NAME" \
           -d $SERVER_HOME   \
           -g $SERVER_GROUP  \
           $SERVER_USER
       # 5. adjust file and directory permissions
       if ! dpkg-statoverride --list $SERVER_HOME >/dev/null
       then
           chown -f $SERVER_USER:$SERVER_GROUP $SERVER_HOME
           chmod -f u=rwx,g=rxs,o= $SERVER_HOME
           chown -f $SERVER_USER:$SERVER_GROUP $SERVER_LOG
           chmod -f u=rwx,g=rxs,o= $SERVER_LOG
           chown -f $SERVER_USER:$SERVER_GROUP $SERVER_ETC
           chmod -f u=rwx,g=rxs,o= $SERVER_ETC

       fi
       # 6. Add the user to the ADDGROUP group
       if test -n $ADDGROUP
       then
           if ! groups $SERVER_USER | cut -d: -f2 | \
               grep -qw $ADDGROUP; then
               adduser $SERVER_USER $ADDGROUP
           fi
       fi
    ;;

    abort-upgrade)
    ;;

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

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

#DEBHELPER#

exit 0
