#!/bin/sh
# postrm script for eprints
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <r>overwrit>r> <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    purge)
        # Remove the eprints user and group.
        deluser --system --quiet eprints || true
        delgroup --system --quiet eprints || true

        # Remove the Apache config files.
        rm -f /etc/apache2/sites-available/eprints3

        # Remove the EPrints databases and MySQL users.
        if [ -d /opt/eprints3/archives ]; then
            # Note that databases created directly using epadmin will (should) have names
            # of the form "archivename", whereas those created using the webmin tool will
            # have names like "eprints_archivename". We just try to drop both and allow
            # things to continue regardless of what happens. Note that we drop each one
            # separately so that if the first one fails, the other still runs. We'll get
            # a slew of errors regardless, so redirect stderr to reduce the noise.
            ( find /opt/eprints3/archives -mindepth 1 -maxdepth 1 -type d \
                -printf "%f\neprints_%f\n" \
                | xargs --max-args=1 --no-run-if-empty mysqladmin -u root -f drop \
            ) 2> /dev/null || true

            # We need to do the same sort of thing with the users as well.
            ( find /opt/eprints3/archives -mindepth 1 -maxdepth 1 -type d \
                -printf "\"drop user '%f'@'localhost';\"\n" \
                -printf "\"drop user 'eprints_%f'@'localhost';\"\n" \
                | xargs --max-args=1 --no-run-if-empty mysql -u root -D mysql -f -e \
            ) 2> /dev/null || true
        fi

        # Remove the /opt/eprints3 directory and all content.
        rm -rf /opt/eprints3
    ;;
    remove|failed-upgrade|abort-install|abort-upgrade|disappear)
        # Disable EPrints Apache config.
        /usr/sbin/a2dissite eprints3

        # Restart Apache to expunge the EPrints archives and ensure that
        # nothing is left cached.
        if [ -x "/etc/init.d/apache2" ]; then
            invoke-rc.d apache2 restart
        fi
    ;;
    upgrade)
        # Disable EPrints Apache config.
        /usr/sbin/a2dissite eprints3

        # No need to restart Apache as the new version of the package will
        # (should!) do it anyway.
    ;;

    *)
        echo "postrm 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
