#!/bin/sh
# prerm script for indimail
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <prerm> `remove'
#        * <old-prerm> `upgrade' <new-version>
#        * <new-prerm> `failed-upgrade' <old-version>
#        * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
#        * <deconfigured's-prerm> `deconfigure' `in-favour'
#          <package-being-installed> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

prefix=/usr
qsysconfdir=/etc/indimail
servicedir=/service
rm=/bin/rm
mv=/bin/mv

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

case "$1" in
    remove|upgrade|deconfigure)
	if [ "$1" != "upgrade" ] ; then
		/bin/rm -f /var/log/daemontools-setup.log
	fi
	(
	set +e
	${prefix}/bin/svok /service/.svscan/log
	if [ $? -eq 0 ] ; then
		log_it "prerm: Giving svscan exactly 5 seconds to exit nicely"
		touch ${servicedir}/.svscan.down
		${prefix}/bin/svps -s
		if test -f /etc/init/svscan.conf
		then
			/sbin/initctl emit qmailstop
		elif test -f /etc/event.d/svscan
		then
			/sbin/initctl emit qmailstop
		elif test -f /etc/systemd/system/multi-user.target.wants/svscan.service
		then
			/bin/systemctl stop svscan
		elif test -x ${prefix}/sbin/initsvc
		then
			if [ -x /etc/init.d/svscan ] ; then
				${prefix}/sbin/initsvc -status || /etc/init.d/svscan stop || true
			else
				${prefix}/sbin/initsvc -status || true
			fi
			${prefix}/sbin/initsvc -off || true
		else
			if [ -x /etc/init.d/svscan ] ; then
				/etc/init.d/svscan stop
			fi
			/bin/grep "^SV:" /etc/inittab |/bin/grep svscan |/bin/grep respawn >/dev/null
			if [ $? -eq 0 ] ; then
				sed -i 's{^SV:{#SV:{' /etc/inittab
				if [ $? -eq 0 ] ; then
					/sbin/init q
				fi
			fi
		fi
		${prefix}/bin/svps -s
		set -e
		sleep 5
	else
		echo "prerm: svscan not running" 1>&2
	fi
	) >> /var/log/daemontools-setup.log 2>&1
    ;;

    failed-upgrade)
    ;;

    *)
    echo "prerm 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
if [ " $1" = " upgrade" -o " $1" = " deconfigure" ] ; then
	exit 0
fi

(
if [ -s ${qsysconfdir}/daemontools-stat.override -a -f /usr/sbin/dpkg-statoverride ] ; then
	log_it "reverting permissions"
	/bin/cat ${qsysconfdir}/daemontools-stat.override | while read line
	do
		if [ -f $line ] ; then
			log_it "reverting permission for $line"
			/usr/sbin/dpkg-statoverride --remove $line || true
		fi
	done
fi
log_it "shutting down and removing .svscan service"
if [ -d ${servicedir}/.svscan -o -L ${servicedir}/.svscan ] ; then
  touch ${servicedir}/.svscan/down
  svc -dx ${servicedir}/.svscan >/dev/null 2<&1 || true
fi
if [ -d ${servicedir}/.svscan/log -o -L ${servicedir}/.svscan/log ] ; then
  touch ${servicedir}/.svscan/log/down
  svc -dx ${servicedir}/.svscan/log >/dev/null 2<&1 || true
fi
if [ -d ${servicedir}/.svscan -o -L ${servicedir}/.svscan ] ; then
  ${rm} -rf ${servicedir}/.svscan
fi
# Remove svscan being started on system boot
log_it "removing svscan startup"
SERVICE_LIST="."
export SERVICE_LIST
$prefix/sbin/minisvc --config=rm-boot >/dev/null || true
${rm} -f ${qsysconfdir}/resolv.conf
) >> /var/log/daemontools-setup.log 2>&1
exit 0
