#!/bin/sh
# postinst script for thruk
#
# 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>
#        * <postinst> `abort-remove'
#        * <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)
        # restore themes, plugins and ssi
        if [ -d /tmp/thruk_update/themes/. ]; then
          rm -f /etc/thruk/themes/themes-enabled/*
          cp -rp /tmp/thruk_update/themes/* /etc/thruk/themes/themes-enabled/
        fi
        if [ -d /tmp/thruk_update/plugins/. ]; then
          rm -f /etc/thruk/plugins/plugins-enabled/*
          cp -rp /tmp/thruk_update/plugins/* /etc/thruk/plugins/plugins-enabled/
        fi
        if [ -d /tmp/thruk_update/ssi/. ]; then
          rm -f /etc/thruk/ssi/*
          cp -rp /tmp/thruk_update/ssi/* /etc/thruk/ssi/
        fi
        rm -rf /tmp/thruk_update

        # enable modules we need
        a2enmod alias
        a2enmod fcgid
        a2enmod auth_basic
        a2enmod rewrite

        # set permissions
        mkdir -p /var/lib/thruk /var/log/thruk /var/cache/thruk/reports
        chown -R www-data: /var/lib/thruk \
                           /var/cache/thruk \
                           /var/log/thruk \
                           /etc/thruk/plugins/plugins-enabled \
                           /etc/thruk/thruk_local.conf
        chown 755 /usr/share/thruk/fcgid_env.sh
        chown 755 /usr/bin/thruk

        # reload new apache config
        if which invoke-rc.d >/dev/null 2>&1; then
            invoke-rc.d apache2 reload
        else
            /etc/init.d/apache2 reload
        fi

        # create empty crontab
        /usr/bin/crontab -l -u www-data 2>/dev/null | /usr/bin/crontab -u www-data -

        # update cron entries
        /usr/bin/thruk -a installcron --local > /dev/null

        echo "Thruk has been configured for http://$(hostname)/thruk/. User and password is 'thrukadmin'."
    ;;

    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
