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

installdir=/opt/campcaster
apache_docroot=/var/www
apache_group=www-data

if [ -d /etc/postgresql/8.1 ]; then
    postgresql_dir=/etc/postgresql/8.1/main
    postgresql_init_script=/etc/init.d/postgresql-8.1
elif [ -d /etc/postgresql/8.2 ]; then
    postgresql_dir=/etc/postgresql/8.2/main
    postgresql_init_script=/etc/init.d/postgresql-8.2
elif [ -d /etc/postgresql/8.3 ]; then
    postgresql_dir=/etc/postgresql/8.3/main
    postgresql_init_script=/etc/init.d/postgresql-8.3
else
    echo "no postgresql 8.1, 8.2 or 8.3 found" >&2
    exit 1
fi

case "$1" in
    configure)
        # do post-installation configuration
        $installdir/bin/postInstallStation.sh --directory=$installdir \
                        --apache-group=$apache_group \
                        --postgresql-dir=$postgresql_dir \
                        --postgresql-init-script=$postgresql_init_script

        # remount the NFS share if we have a 2-computer setup
        if [ -f $installdir/storageServer.wasMounted ]; then
            echo "Remounting the remote storage..."
            mount $installdir/var/Campcaster/storageServer
            rm $installdir/storageServer.wasMounted
        fi

        # register and start the Campcaster scheduler daemon
        cp -f $installdir/etc/campcaster-scheduler /etc/init.d
        update-rc.d campcaster-scheduler defaults 92 || true
        /etc/init.d/campcaster-scheduler start || true
    ;;

    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#

exit 0