#! /bin/bash 

set -e

. /usr/share/debconf/confmodule

#     * 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 overwriter overwriter-version

case "$1" in
  purge)
    rm -f /etc/alternc/local.sh /etc/alternc/my_mail.cnf /etc/alternc/bureau.conf
    rm -f /var/lib/alternc/backups/etc-installed.tar.gz
    rm -f /etc/incron.d/alternc

    # FIXME : remove following commented lines if we are sure
    # to not drop database and remove files (except /etc/alternc)
    # when Alternc is purged

    # Maybe tell to user how to do if he want to drop db and/or remove files ?

    # Purge database?
    #db_input high alternc/postrm_remove_databases || true
    #db_go
    #db_get alternc/postrm_remove_databases || true
    #if [ "$RET" = "true" ]; then
    #  /usr/bin/mysql --defaults-file=/etc/alternc/my.cnf -Bs -e "DROP DATABASE $ALTERNC_DB"
    #fi

    # Purge datafiles
    #db_input high alternc/postrm_remove_datafiles || true
    #db_go
    #db_get alternc/postrm_remove_datafiles || true
    #if [ "$RET" = "true" ]; then
    #  rm -rf /var/lib/alternc/backups /var/alternc/dns /var/alternc/html /var/alternc/redir
    #fi

    # Purge bind zones
    #db_input high alternc/postrm_remove_bind || true
    #db_go
    #db_get alternc/postrm_remove_bind || true
    #if [ "$RET" = "true" ]; then
    #  rm -rf /var/alternc/bind /etc/bind/templates
    #fi

    # Purge mailboxes
    #db_input high alternc/postrm_remove_mailboxes || true
    #db_go
    #db_get alternc/postrm_remove_mailboxes || true
    #if [ "$RET" = "true" ]; then
    #  rm -rf /var/alternc/mail
    #fi

    #rm -rf /var/alternc/apacheconf /var/alternc/cgi-bin /var/alternc/bureau /var/alternc/exec.usr /var/alternc/mla /var/alternc/redir /var/alternc/tmp /var/log/alternc
    rm -f /etc/apache*/conf.d/override_php.conf /etc/apache*/conf.d/alternc-ssl.conf /etc/apache*/conf.d/alternc.conf
    rm -f /etc/php*/conf.d/alternc.ini

    # Restore orignals conf files which have been replaced by Alternc
    # With the oldest tar found
    BACKUP=$(ls /var/lib/alternc/backups/etc-original*.tar.gz | head -n 1)
    tar -C / -xf "$BACKUP" 2>/dev/null || true
    
    # Reload services which use these files
    for service in bind9 postfix proftpd dovecot apache2; do
	invoke-rc.d $service force-reload || true
    done

    ;;
  remove|upgrade|failed-upgrade|abort-install|abort-upgrade)
    ;;
  disappear)
    ;;
  *)
    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#

# important: postrm freezes withtout that:
db_stop

exit 0

# vim: et sw=4