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

set -e

# Substitute in the values from the debconf db.
# Must remove the area-specific envs - debconf chokes on them.
egrep "^[a-zA-Z0-9_]+=" /etc/hobbit/hobbitserver.cfg >/etc/hobbit/hobbitserver.cfg.dbinst
CONFIGFILE=/etc/hobbit/hobbitserver.cfg.dbinst
. /usr/share/debconf/confmodule


# 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>
#        * <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)
        # Setup permissions for the newly created "hobbit" user to write
	# data where he needs to.
	# And for the Apache-run CGI's to generate reports.
        cd /var/log/hobbit; chown hobbit:hobbit .
	cd /var/lib/hobbit; chown hobbit:hobbit . acks data disabled hist histlogs hostdata logs rrd tmp www
	cd /var/lib/hobbit/www; chown hobbit:hobbit . html notes wml rep snap; chgrp www-data rep snap; chmod g+w rep snap
	cd /etc/hobbit; chgrp www-data hobbit-nkview.cfg hobbit-nkview.cfg.bak; chmod g+w hobbit-nkview.cfg hobbit-nkview.cfg.bak
	chown root /usr/lib/hobbit/server/bin/hobbitping; chmod 4755 /usr/lib/hobbit/server/bin/hobbitping
	cd /usr/lib/hobbit/client; chown hobbit:hobbit logs tmp; chmod u+w logs tmp

	# Do the debconf stuff
	db_get hobbit/BBSERVERHOSTNAME
	BBSERVERHOSTNAME="$RET"
	db_get hobbit/BBSERVERIP
	BBSERVERIP="$RET"
	db_get hobbit/BBSERVERWWWNAME
	BBSERVERWWWNAME="$RET"
	db_stop

	# Update configuration file
	cp -a -f $CONFIGFILE $CONFIGFILE.tmp
	sed -e 's/^BBSERVERHOSTNAME=.*/BBSERVERHOSTNAME=\"${BBSERVERHOSTNAME}\"/' \
	    -e 's/^BBSERVERIP=.*/BBSERVERIP=\"${BBSERVERIP}\"/'                   \
	    -e 's/^BBSERVERWWWNAME=.*/BBSERVERWWWNAME=\"${BBSERVERWWWNAME}\"/'    \
	    < $CONFIGFILE > $CONFIGFILE.tmp
	mv -f $CONFIGFILE.tmp $CONFIGFILE
    ;;

    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#

rm -f /etc/hobbit/hobbitserver.cfg.dbinst
exit 0

