#!/bin/sh
# postrm script for indimail
#
# 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' <overwriter>
#          <overwriter-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


prefix=/usr
mandir=/usr/share/man
sysconfdir=/etc/indimail
servicedir=/service
logdir=/var/log/svc
rm=/bin/rm
rmdir=/bin/rmdir
mv=/bin/mv
####################################
nocourierimap=0
nofetchmail=0

ID=`id -u`
if [ $ID -ne 0 ] ; then
	echo "You are not root" >&2
	exit 1
fi

case "$1" in
    purge|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#

# we are doing upgrade or removal
if [ " $1" = " upgrade" ] ; then
	if [ ! "${prefix}" = "/usr" ] ; then
	echo "recreating ld.so cache"
	/sbin/ldconfig
	fi
	exit 0
fi

case "$1" in
    failed-upgrade|abort-install|abort-upgrade|disappear)
	exit 0
    ;;
esac
if [ -x /usr/bin/uname -o -x /bin/uname ] ; then
  default_domain=$(echo $([ -n "$HOSTNAME" ] && echo "$HOSTNAME" || uname -n) | sed 's/^\([^\.]*\)\.\([^\.]*\)\./\2\./')
else
  defaultDomain=@defaultdomain@
  default_domain=$(echo $([ -n "$HOSTNAME" ] && echo "$HOSTNAME" || echo ${defaultDomain}) | sed 's/^\([^\.]*\)\.\([^\.]*\)\./\2\./')
fi
#
# Debian silliness
#
if [ $nocourierimap -eq 0 ] ; then
	echo "removing startup services"
	for i in qmail-imapd.143 qmail-imapd-ssl.993 \
		qmail-pop3d.110 qmail-pop3d-ssl.995 \
		proxy-imapd.4143 proxy-imapd-ssl.9143 \
		proxy-pop3d.4110 proxy-pop3d-ssl.9110
	do
		if [ -d ${servicedir}/$i -o -L ${servicedir}/$i ] ; then
			touch ${servicedir}/$i/down
			svc -dx ${servicedir}/$i >/dev/null 2>&1 || true
		fi
		if [ -d ${servicedir}/$i/log -o -L ${servicedir}/$i/log ] ; then
			touch ${servicedir}/$i/log/down
			svc -dx ${servicedir}/$i/log >/dev/null 2>&1 || true
		fi
		if [ -d ${servicedir}/$i -o -L ${servicedir}/$i ] ; then
			${rm} -rf ${servicedir}/$i || true
		fi
	done

	echo "removing TCP configuration"
	for i in imap pop3
	do
		${rm} -f ${sysconfdir}/tcp/tcp.$i.cdb ${sysconfdir}/tcp/tcp.$i
	done
	for i in indimail-access-stat.override \
		imapd.cnf imapd.dist imapd-ssl.dist \
		pop3d.cnf pop3d.dist pop3d-ssl.dist \
		quotawarnmsg.example perm_list.imap
	do
		${rm} -f ${sysconfdir}/$i
	done
	${rm} -f ${sysconfdir}/imapd.pem ${sysconfdir}/pop3d.pem
	${rm} -f ${sysconfdir}/imapd.cnf.* ${sysconfdir}/pop3d.cnf.*
fi

if [ $nofetchmail -eq 0 ] ; then
	echo "removing startup services"
	if [ -d ${servicedir}/fetchmail -o -L ${servicedir}/fetchmail ] ; then
		touch ${servicedir}/fetchmail/down
		svc -dx ${servicedir}/fetchmail >/dev/null 2>&1 || true
	fi
	if [ -d ${servicedir}/fetchmail/log -o -L ${servicedir}/fetchmail/log ] ; then
		touch ${servicedir}/fetchmail/log/down
		svc -dx ${servicedir}/fetchmail/log >/dev/null 2>&1 || true
	fi
	if [ -d ${servicedir}/fetchmail -o -L ${servicedir}/fetchmail ] ; then
		${rm} -rf ${servicedir}/fetchmail
	fi
	${rm} -f ${sysconfdir}/fetchmailrc
fi

count=`/bin/ls ${servicedir} 2>/dev/null| /usr/bin/wc -l`
if [ $count -eq 0 ] ; then # ignore disabled services
	${rm} -rf ${servicedir} || true
fi

echo "removing logs"
if [ -h ${logdir} ] ; then
	log_dir=`/bin/ls -ld ${logdir} | /usr/bin/awk '{print $10}'`
else
	log_dir=${logdir}
fi

if [ $nocourierimap -eq 0 ] ; then
	for i in fetchmail imapd.143 imapd-ssl.993 \
		pop3d.110 pop3d-ssl.995 \
		proxyIMAP.4143 proxyIMAP.9143 \
		proxyPOP3.4110 proxyPOP3.9110
	do
		${rm} -rf $log_dir/$i
	done
fi

if [ $nofetchmail -eq 0 ] ; then
	${rm} -rf $log_dir/fetchmail
fi

if [ ! "${prefix}" = "/usr" ] ; then
	echo "recreating ld.so cache"
	/sbin/ldconfig
fi
exit 0
