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

set -e

# Source the debconf library.
. /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 /usr/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

case "$1" in
	install)
	;;

	upgrade)
	;;

	configure)
# Create these directories if they don't exist
		if [ ! -d /var/lib/printbill ] ; then
			db_get printbill/use_web_interface
		
			if [ "$RET" = "true" ] ; then
				GROUP=printbil
				addgroup $GROUP || true
				adduser www-data $GROUP || true
				adduser daemon $GROUP || true
			else
				GROUP=daemon
			fi

			install -d -m 0755 -o daemon -g daemon /var/lib/printbill || true
			install -d -m 0770 -o daemon -g $GROUP /var/lib/printbill/tmp || true
			install -d -m 0775 -o daemon -g $GROUP /var/lib/printbill/users || true
			install -d -m 0775 -o daemon -g $GROUP /var/lib/printbill/printers || true
			install -d -m 0755 -o daemon -g daemon /var/lib/printbill/stats || true
			install -d -m 0700 -o www-data -g www-data /var/lib/printbill/cookies || true
		fi
		
# Because printbilld leaves STDOUT/STDERR/STDIN open (on /dev/null...)
	
		db_stop	
		
		#DEBHELPER#
	;;
	abort-upgrade)

	;;
	abort-remove)
		exit 0
	;;

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

exit 0
