#!/bin/sh

test $DEBIAN_SCRIPT_DEBUG && set -v -x

export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin

# Gruppe und User gitwebhook anlegen, falls es sie nicht gibt.
# Schamlos von moritz' sms-rest gestohlen, der es bei memcached.postinst gestohlen, das es wiederum von
# gearman-job-server hat

USER="gitwebhook"
GROUP="gitwebhook"
HOSTNAME=$(hostname)
DATADIR="/var/spool/gitwebhook"

case "$1" in
configure)
    # creating group if it isn't already there
    if ! getent group $GROUP >/dev/null ; then
            # Adding system group
            addgroup --system $GROUP >/dev/null
    fi

    # creating user if it isn't already there
    if ! getent passwd $USER >/dev/null ; then
            # Adding system user
            adduser \
              --system \
              --disabled-login \
              --ingroup $GROUP \
              --home $DATADIR \
              --shell /bin/false \
              $USER  >/dev/null
    fi

    # creating ssh-key if it isn't already there
    if ! test -r $DATADIR/.ssh/id_rsa; then
        mkdir -p $DATADIR/.ssh
        ssh-keygen -q -N "" -f $DATADIR/.ssh/id_rsa
        chown $USER.$GROUP $DATADIR -R
    fi

    # enable service
    update-rc.d gitwebhook defaults >/dev/null
    invoke-rc.d gitwebhook start || exit $?

    ;;
esac

