#!/bin/sh
# postrm script for firebird2.5-superclassic

set -e
if [ -f /usr/share/debconf/confmodule ]; then
    . /usr/share/debconf/confmodule
fi
set -u

# 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>

FB_VER=2.5
DBAPasswordFile="/etc/firebird/$FB_VER/SYSDBA.password"
SECURITY_DB="/var/lib/firebird/$FB_VER/system/security2.fdb"


case "$1" in
    purge)
        if [ -f /usr/share/debconf/confmodule ]; then
            # metaget returns an error "... does not exist" if the last package
            # is purged
            db_metaget shared/firebird/active_version owners || RET=
            if ! (echo "$RET" | grep -q "$FB_VER") ; then
                if [ -f "$DBAPasswordFile" ] || [ -f "$SECURITY_DB" ]; then
                    # clean up security remains?
                    db_subst shared/firebird/purge_security FB_VER $FB_VER
                    db_fset shared/firebird/purge_security seen false
                    db_input critical shared/firebird/purge_security || true
                    # remove databases?
                    db_subst shared/firebird/purge_databases FB_VER $FB_VER
                    db_fset shared/firebird/purge_databases seen false
                    db_input critical shared/firebird/purge_databases || true
                    db_go

                    db_get shared/firebird/purge_security
                    purge_security=$RET
                    db_get shared/firebird/purge_databases
                    purge_databases=$RET


                    if [ "$purge_security" = 'true' ]; then
                        if [ -f "$DBAPasswordFile" ]; then
                            rm "$DBAPasswordFile"
                        fi

                        if [ -f "$SECURITY_DB" ]; then
                            rm "$SECURITY_DB"
                        fi
                    fi


                    if [ "$purge_databases" = 'true' ]; then
                        DB_DIR="/var/lib/firebird/$FB_VER/data"

                        find $DB_DIR -type f \( -name "*.fdb" -o -name "*.fbk" \) \
                        -exec rm '{}' \;
                    fi
                fi
            fi
        else
            echo "debconf unavailable."
            echo "NOT purging password file ($DBAPasswordFile)"
            echo "NOT purging security database ($SECURITY_DB)"
            echo "NOT purging databases in /var/lib/firebird/$FB_VER/data"
        fi
    ;;

esac

#DEBHELPER#

exit 0

