#!/bin/sh
set -e

. /usr/share/debconf/confmodule

LOCALSCANCONF=/etc/exim4/conf.d/main/15_sa-exim_plugin_path
DISABLELOCALSCANTEXT='# sa-exim plugin is disabled, because it was uninstalled'
DISABLELOCALSCANMD5=`echo "${DISABLELOCALSCANTEXT}" | md5sum | cut -d\  -f1`

case "$1" in
    remove)
        # disable local_scan_path directive to exim working
	if [ -e "$LOCALSCANCONF" ] && [ ! -e "${LOCALSCANCONF}.rul" ]; then
	    echo "${DISABLELOCALSCANTEXT}" > ${LOCALSCANCONF}.rul
	fi
	if [ -x /etc/init.d/exim4 ]; then
		if [ -x /usr/sbin/invoke-rc.d ]; then
			invoke-rc.d exim4 reload || true
		else
			/etc/init.d/exim4 reload || true
		fi
	fi
	;;

    purge)
        # clean up temporary file generated by postrm uninstall
	if [ -e "${LOCALSCANCONF}.rul" ] && \
	   [ "`md5sum ${LOCALSCANCONF}.rul | cut -d\  -f1`" = "${DISABLELOCALSCANMD5}" ]; then
	   rm ${LOCALSCANCONF}.rul
	fi
	if [ -x /etc/init.d/exim4 ]; then
		if [ -x /usr/sbin/invoke-rc.d ]; then
			invoke-rc.d exim4 reload || true
		else
			/etc/init.d/exim4 reload || true
		fi
	fi
	if [ -e /var/spool/sa-exim ] && ! rmdir /var/spool/sa-exim 2>/dev/null ; then
		db_version 2.0
		db_input medium sa-exim/purge_spool || true
		db_go || true
		db_get sa-exim/purge_spool
		purge_spool="$RET"
		if [ "x${purge_spool}" = "xtrue" ] ; then
			rm -rf /var/spool/sa-exim
		fi
	fi
	;;
esac

#DEBHELPER#
