#!/bin/sh
#
# Postinst script for authentic2
#

set -e

NAME=authentic2
OLD_AUTHENTIC_USER=authentic
OLD_AUTHENTIC_GROUP=authentic
AUTHENTIC_USER=authentic2
AUTHENTIC_GROUP=authentic2
AUTHENTIC_HOME=/var/lib/authentic2
AUTHENTIC_SECRET_KEY="$AUTHENTIC_HOME/secret_key"

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

case "$1" in
    configure)
        if ! getent group $AUTHENTIC_GROUP > /dev/null 2>&1; then
            if getent group $OLD_AUTHENTIC_GROUP >/dev/null 2>&1; then
                echo -n "Renaming group $OLD_AUTHENTIC_GROUP to $AUTHENTIC_GROUP.." >&2
                groupmod -n $AUTHENTIC_GROUP $OLD_AUTHENTIC_GROUP
                echo "..done" >&2
            else
                echo -n "Adding group $AUTHENTIC_GROUP.." >&2
                addgroup --quiet --system $AUTHENTIC_GROUP
                echo "..done" >&2
            fi
        fi
        if ! getent passwd $AUTHENTIC_USER > /dev/null 2>&1; then
            if getent passwd $OLD_AUTHENTIC_USER >/dev/null 2>&1; then
                echo -n "Renaming user $OLD_AUTHENTIC_USER to $AUTHENTIC_USER.." >&2
                usermod -l $AUTHENTIC_USER $OLD_AUTHENTIC_USER
                echo "..done" >&2
            else
                echo -n "Adding user $AUTHENTIC_USER.." >&2
                adduser --quiet --system --gecos "Authentic2 daemon" \
                        --ingroup $AUTHENTIC_GROUP \
                        --no-create-home --home $AUTHENTIC_HOME \
                        $AUTHENTIC_USER
                echo "..done" >&2
                echo "If you currently use a local postgres database with UNIX security, please rename the postgresql user."
            fi
        fi

        if [ ! -f $AUTHENTIC_SECRET_KEY ]; then
            if [ -f /etc/$NAME/secret ]; then
                echo -n "Converting storage of the secret key.." >&2
                . /etc/$NAME/secret
                echo -n "$SECRET_KEY" >"$AUTHENTIC_SECRET_KEY"
                chown $AUTHENTIC_USER:$AUTHENTIC_GROUP $AUTHENTIC_SECRET_KEY
                rm /etc/$NAME/secret
                echo "..done" >&2
            else
                echo -n "Generating a secret key.." >&2
                echo -n "`</dev/urandom tr -dc [:alnum:]-_\!\%\^:\; | head -c70`" > "$AUTHENTIC_SECRET_KEY"
                chmod 0600 $AUTHENTIC_SECRET_KEY
                chown $AUTHENTIC_USER:$AUTHENTIC_GROUP $AUTHENTIC_SECRET_KEY
                echo "..done" >&2
            fi
        fi

        if [ -d $AUTHENTIC_HOME/extra-static ]; then
            if [ -d $AUTHENTIC_HOME/static ]; then
               mv $AUTHENTIC_HOME/static $AUTHENTIC_HOME/static.dpkg_old
            fi
            mkdir -p $AUTHENTIC_HOME/static
            echo -n "Migrate old extra-static/ to static/.." >&2
            mv $AUTHENTIC_HOME/extra-static/* $AUTHENTIC_HOME/static/
            mv $AUTHENTIC_HOME/extra-static $AUTHENTIC_HOME/extra-static.dpkg_old
            echo "..done" >&2
        fi

        if [ -f /etc/authentic2/authentic.conf ]; then
            # Fix old configuration file
            sed -i -e '/^\. \/etc\/authentic2\/secret$/d' \
                -e '/^export *STATIC_ROOT *=/d' \
                -e '/^. \/etc\/authentic2\/db.conf$/d' \
                -e '/^# do not remove this line, it imports/d' \
                /etc/authentic2/authentic.conf >&2
        fi

        if [ ! -f /etc/authentic2/cert.pem -a ! -f /etc/authentic2/key.pem ]; then
            echo -n "Generating key material..." >&2
            openssl genpkey -algorithm rsa -pkeyopt rsa_keygen_bits:2048 -out /etc/authentic2/key.pem >&2
            openssl req -x509 -new -out /etc/authentic2/cert.pem -subj '/CN=whocares' -key /etc/authentic2/key.pem -days 3650 >&2
            chown $AUTHENTIC_USER.$AUTHENTIC_GROUP /etc/authentic2/cert.pem /etc/authentic2/key.pem
            chmod 640 /etc/authentic2/cert.pem /etc/authentic2/key.pem
            echo "..done" >&2
        fi

        chown $AUTHENTIC_USER:$AUTHENTIC_GROUP \
            $AUTHENTIC_HOME \
            $AUTHENTIC_HOME/static \
            $AUTHENTIC_HOME/collectstatic \
            $AUTHENTIC_HOME/locale \
            $AUTHENTIC_HOME/media \
            $AUTHENTIC_HOME/templates \
            $AUTHENTIC_HOME/spooler \
            /var/log/$NAME \
        ;;

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

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

db_stop

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

#DEBHELPER#

exit 0
