#!/bin/sh
set -e

#DEBHELPER#

CONFFILE=/etc/default/tomcat8
LR_CONFFILE=/etc/logrotate.d/tomcat8

# Remove cached files and auto-generated catalina.policy
rm -rf /var/cache/tomcat8/*

case "$1" in
    remove)
        # Remove ROOT webapp if not modified
        RWLOC="/var/lib/tomcat8/webapps/ROOT"
        RWFILES="$RWLOC/index.html $RWLOC/META-INF/context.xml"
        if [ "`(cat $RWFILES | md5sum -) 2>/dev/null | cut -d ' ' -f 1`" \
                            = "@ROOT_WEBAPP_MD5SUM@" ] ; then
            rm $RWFILES
            rmdir --ignore-fail-on-non-empty \
                /var/lib/tomcat8/webapps/ROOT/META-INF \
                /var/lib/tomcat8/webapps/ROOT \
                /var/lib/tomcat8/webapps \
                /var/lib/tomcat8 || true
        fi
        if [ -d /var/lib/tomcat8/common ] && [ -z "`(find var/lib/tomcat8/common/classes -type f)`" ] ; then
            rmdir --ignore-fail-on-non-empty \
                /var/lib/tomcat8/common/classes \
                /var/lib/tomcat8/common || true
        fi
        if [ -d /var/lib/tomcat8/server ] && [ -z "`(find var/lib/tomcat8/server/classes -type f)`" ] ; then
            rmdir --ignore-fail-on-non-empty \
                /var/lib/tomcat8/server/classes \
                /var/lib/tomcat8/server || true
        fi
        if [ -d /var/lib/tomcat8/shared ] && [ -z "`(find var/lib/tomcat8/shared/classes -type f)`" ] ; then
            rmdir --ignore-fail-on-non-empty \
                /var/lib/tomcat8/shared/classes \
                /var/lib/tomcat8/shared || true
        fi
        if [ -d /var/lib/tomcat8/lib ] && [ -z "`(find var/lib/tomcat8/lib/classes -type f)`" ] ; then
            rmdir --ignore-fail-on-non-empty \
                /var/lib/tomcat8/lib/classes \
                /var/lib/tomcat8/lib || true
        fi
        if [ -d "/var/cache/tomcat8" ] ; then
            rm -rf /var/cache/tomcat8
        fi
    ;;

    purge)
        # ignore errors during purge
        set +e
        if [ -x "/usr/bin/ucf" ]; then
            ucf --purge $CONFFILE
            ucf --purge $LR_CONFFILE
        fi
        rm -rf /var/log/tomcat8 /var/lib/tomcat8/temp $CONFFILE $LR_CONFFILE
        if [ -d "/var/lib/tomcat8" ] ; then
            rmdir --ignore-fail-on-non-empty /var/lib/tomcat8
        fi
        rmdir --ignore-fail-on-non-empty /etc/tomcat8/policy.d \
          /etc/tomcat8/Catalina/localhost /etc/tomcat8/Catalina /etc/tomcat8
        # clean up /etc/authbind after conffiles have been removed
        rmdir --ignore-fail-on-non-empty /etc/authbind/byuid /etc/authbind
        # Put all files owned by group tomcat8 back into root group before deleting
        # the tomcat8 user and group
        chown -Rhf root:root /etc/tomcat8/ || true
        # Remove user/group and log files (don't remove everything under
        # /var/lib/tomcat8 because there might be user-installed webapps)
        db_get tomcat8/username && TOMCAT8_USER="$RET" || TOMCAT8_USER="tomcat8"
        db_get tomcat8/groupname && TOMCAT8_GROUP="$RET" || TOMCAT8_GROUP="tomcat8"
        deluser $TOMCAT8_USER
        delgroup $TOMCAT8_GROUP

        set -e
    ;;

    upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
        # Nothing to do here
    ;;

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