#!/bin/sh
# postinst script for polynum
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)
        # create user to avoid running server as root

        # code borrowed from
        # http://www.debian.org/doc/manuals/securing-debian-howto/ch9.en.html#s-bpp-lower-privs

        # If the package has default file it could be sourced, so that
        # the local admin can overwrite the defaults
        [ -f "/etc/default/polynum" ] && . /etc/default/polynum

        # Sane defaults:
        [ -z "$POLYNUM_USER" ] && POLYNUM_USER=polynum
        [ -z "$POLYNUM_NAME" ] && POLYNUM_NAME="PolyNum daemon user"
        [ -z "$POLYNUM_HOME" ] && POLYNUM_HOME=/opt/polynum
        [ -z "$POLYNUM_GROUP" ] && POLYNUM_GROUP=polynum
        [ -z "$POLYNUM_VAR" ] && POLYNUM_VAR=/var/lib/polynum
        [ -z "$POLYNUM_ETC" ] && POLYNUM_ETC=/etc/polynum
        [ -z "$PIDFILE" ] && PIDFILE=/var/run/polynum/polynum.pid

        # 1. create group if not existing
        if ! getent group | grep -q "^$POLYNUM_GROUP:" ; then
            echo -n "Adding group $POLYNUM_GROUP.."
            addgroup --quiet --system $POLYNUM_GROUP 2>/dev/null ||true
            echo "..done"
        fi

        # 2. create homedir if not existing
        # test -d $POLYNUM_HOME || mkdir $POLYNUM_HOME

        # 3. create user if not existing
        if ! getent passwd | grep -q "^$POLYNUM_USER:"; then
            echo -n "Adding system user $POLYNUM_USER.."
            adduser --quiet \
                   --system \
                   --ingroup $POLYNUM_GROUP \
                   --no-create-home \
                   --disabled-password \
                   $POLYNUM_USER 2>/dev/null || true
            echo "..done"
        fi

        # 4. adjust passwd entry
        usermod -c "$POLYNUM_NAME" \
                -d $POLYNUM_HOME   \
                -g $POLYNUM_GROUP  \
                $POLYNUM_USER 2>/dev/null || true

        # 5. create default config 
        #    and adjust some files and directories permissions
        if [ ! -f $POLYNUM_ETC/local_settings.py ]
        then
            if ! dpkg-statoverride --list $POLYNUM_ETC/local_settings.py >/dev/null
            then
                echo -n "Installing default $POLYNUM_ETC/local_settings.py .."
                cp /usr/share/doc/polynum/local_settings.py.example $POLYNUM_ETC/local_settings.py
                chown $POLYNUM_USER:$POLYNUM_GROUP $POLYNUM_ETC/local_settings.py
                chmod u=rw,g=,o= $POLYNUM_ETC/local_settings.py
                echo "..done"
            fi
        fi
        if ! dpkg-statoverride --list $POLYNUM_VAR/media >/dev/null
        then
            echo -n "Creating $POLYNUM_VAR/media .."
            mkdir -p $POLYNUM_VAR/media 2> /dev/null || true
            chown -R $POLYNUM_USER:$POLYNUM_GROUP $POLYNUM_VAR/media
            chmod u=rwx,g=rwxs,o= $POLYNUM_VAR/media
            echo "..done"
        fi
        PIDDIR=$(dirname ${PIDFILE})
        if ! dpkg-statoverride --list $PIDDIR >/dev/null
        then
            echo -n "Creating $PIDDIR .."
            mkdir -p $PIDDIR 2> /dev/null || true
            chown -R $POLYNUM_USER:$POLYNUM_GROUP $PIDDIR
            chmod u=rwx,g=rwxs,o= $PIDDIR
            echo "..done"
        fi
        echo -n "Creating .pyc files [ "
        for pydir in polynum virtualenv
        do
            echo -n "$POLYNUM_HOME/$pydir "
            python -m compileall -f -q $POLYNUM_HOME/$pydir
        done
        echo "] done"
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst 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
