#! /bin/sh
#
# see: dh_installdeb(1)

set -e

# 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
#
# 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
    configure)
	adduser --group --system --home /var/lib/synchronet --shell /bin/false --no-create-home --group sbbs > /dev/null 2>/dev/null
	update-rc.d synchronet defaults > /dev/null
	if [ ! -d /var/lib/synchronet/ctrl ]; then
		echo -n "Bootstrapping a BBS in /var/lib/synchronet..."
		cd /var/lib/synchronet && tar zxf /usr/share/synchronet/run-sbbs3.tar.gz
		echo " done.";
	else
		echo "Leaving /var/lib/synchronet alone."
	fi

	if [ ! -f /var/lib/synchronet/exec/login.bin ] || [ /usr/lib/synchronet/bin/baja -nt /var/lib/synchronet/exec/login.bin ]; then
		echo -n "Compiling Baja modules in /var/lib/synchronet/exec..."
		cd /var/lib/synchronet/exec && make BAJAPATH=/usr/lib/synchronet/bin/baja >/dev/null
		echo " done.";
	fi
	chown -R sbbs.sbbs /var/lib/synchronet
	perl /usr/lib/synchronet/bin/update-ini.pl postinst
	if [ -z "`grep synchronet /etc/syslog.conf`" ]; then
		FACILITY=`/usr/sbin/syslog-facility set all /var/log/synchronet.log` || true
		FACILITY=`echo $FACILITY | cut -dl -f3`
		perl /usr/lib/synchronet/bin/update-ini.pl setlog $FACILITY
		kill -HUP `cat /var/run/syslogd.pid`
	fi
    ;;

    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.

# Automatically added by dh_installinit
if [ -x "/etc/init.d/synchronet" ]; then
	update-rc.d synchronet defaults >/dev/null
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d synchronet start
	else
		/etc/init.d/synchronet start
	fi
fi
# End automatically added section


exit 0


