#! /bin/sh
# postinst script for daemontools
#
# 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 /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
    configure)

    install -d -o root -g root -m 0755 /var/lib/svscan
    chown daemon.adm /var/log/svscan

if [ ! -e /service ]; then
	echo "This package uses /var/lib/svscan as its service directory.  This is"
	echo "in direct conflict with Dan Bernstein's recommended use of /service"
	echo "as the service directory.  If you like, we can create a symlink now"
	echo "from /var/lib/svscan to /service."
	echo ""
	ans=""
	while [ "$ans" != "n" ] && [ "$ans" != "y" ]; do
		echo -n "Create symlink? [y/n] "
		read ans
	done
	
	if [ "$ans" = "y" ]; then
		echo "creating symlink..."
		ln -s /var/lib/svscan /service || :
	else
		echo "not creating symlink."
	fi
fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

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

update-rc.d daemontools defaults >/dev/null

if [ ! `pidof /usr/bin/svscan` ]; then
    	/etc/init.d/daemontools start
fi

exit 0
