#!/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
qmaildir=/var/indimail
qsysconfdir=/etc/indimail
shareddir=${prefix}/share/indimail
mandir=${prefix}/share/man
servicedir=/service
logdir=/var/log/svc
rm=/bin/rm
rmdir=/bin/rmdir
####################################
default_domain=indimail.org

log_it() {
  if [ -x ${prefix}/bin/tai64n -a -x ${prefix}/bin/tai64nlocal ] ; then
    echo "$(echo  ""|tai64n|tai64nlocal)$*"
  else
    echo $*
  fi
}

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

# we are doing upgrade or removal
case "$1" in
    failed-upgrade|abort-install|abort-upgrade|disappear)
	exit 0
    ;;
esac
#
# Debian silliness
#
if [ " $1" = " purge" ] ; then
	(
	nscd_up=`ps -ef |grep nscd |grep -v grep|wc -l`
	if [ $nscd_up -ge 1 ] ; then
		if [ -x /etc/init.d/nscd ] ; then
			/etc/init.d/nscd stop
		fi
	fi
	log_it "removing user qmaill"
	/usr/bin/getent passwd qmaill > /dev/null && /usr/sbin/userdel qmaill >/dev/null || true
	log_it "removing group nofiles"
	/usr/bin/getent group nofiles > /dev/null && /usr/sbin/groupdel nofiles  >/dev/null || true
	if [ $nscd_up -ge 1 ] ; then
		if [ -x /etc/init.d/nscd ] ; then
			/etc/init.d/nscd start
		fi
	fi
	log_it "removing logs"
	if [ -h ${logdir} ] ; then
		log_dir=`/bin/ls -ld ${logdir} | /usr/bin/awk '{print $10}'`
	else
		log_dir=${logdir}
	fi
	[ "$log_dir" != "/" ] && $rm -fr $log_dir
	) >> /var/log/daemontools-setup.log 2>&1
fi
exit 0
