#!/bin/sh -e

if ! id -u maradns >/dev/null 2>&1 ; then

    # the account doesn't exist... time to create it
    echo "creating MaraDNS system user..."
    adduser --quiet --system --group --home /etc/maradns maradns

    if [ "$1" = "upgrade" ] && dpkg --compare-versions "$2" lt 1.0.04-1; then
        echo
        echo "NOTE: Starting from 1.0.04-1 MaraDNS package installation"
        echo "creates separate user for itself to use."
        echo "However upgrade will not touch existing configuration files,"
        echo "so please modify maradns_uid and maradns_gid in mararc to match "
        echo "the numeric uid and gid of newly created maradns user."
        echo -n "Press RETURN to continue"
        read foo
    fi
fi

if [ "$1" = "install" ] && [ -f /etc/maradns/mararc ] ; then #were doing fresh install
    if id -u maradns >/dev/null 2>&1 ; then #that uid was really created
    IDNUM=`id -u maradns`
    GIDNUM=`id -g maradns`
    sed -e "s/^maradns_uid = .*$/maradns_uid = $IDNUM/" \
        -e "s/^# maradns_gid = .*$/maradns_gid = $IDNUM/" < /etc/maradns/mararc > /etc/maradns/mararc.tmp
    mv -f /etc/maradns/mararc.tmp /etc/maradns/mararc
    fi
fi

# If an old style (single server) pid file
# stop the server if it's running and delete the file
if [ -f /var/run/maradns.pid ] ; then
    start-stop-daemon --oknodo --stop -m --quiet --pidfile /var/run/maradns.pid /usr/sbin/maradns
    rm -f /var/run/maradns.pid
fi

#DEBHELPER#

exit 0
