#!/bin/sh
#
# De-installation setup of ravsendmail

PATH=/bin:/usr/bin:/sbin:/usr/sbin

# Function: restore sendmail configuration
#
do_restore()
{
	#restore the sendmail configuration files
	if [ -f  /etc/mail/sendmail.cf ]; then
		sendmail_cf=/etc/mail/sendmail.cf
	else
		if [ -f /etc/sendmail.cf ]; then
			sendmail_cf=/etc/sendmail.cf
		else
			sendmail_cf=false
			echo "| Could not locate sendmail.cf."
			echo "| You have to restore manually the sendmail"
			echo "| configuration file. Please read the UNINSTALL"
			echo "| file for more information."
		fi
	fi
	if [ "$sendmail_cf" != "false" ]; then
	
		echo "-> Restoring the sendmail configuration: $sendmail_cf ..."

		sed -e '/^Sparse=0/,/^SParse0/s/\$#ravsendmail \$@\$1 \$:\$1/\$: \$>Parse1 \$1/' \
			-e '/^S0/,/^SParse0/s/\$#ravsendmail \$@\$1 \$:\$1/\$: \$>Parse1 \$1/' \
			-e '/^#rav-begin/,/^#rav-end/d' < $sendmail_cf > $sendmail_cf.sed   \
			&& mv -f $sendmail_cf.sed $sendmail_cf

		rm -f ${sendmail_cf}.orig

		echo "-> Reloading the sendmail configuration ..."
		killall -HUP sendmail 2>/dev/null

		echo "-> Stoping the second sendmail executable ..."
		killall -TERM sendmail.orig 2>/dev/null
		rm -f /usr/sbin/sendmail.orig
	fi
}

# verify proper execution
#
if [ $# -ne 2 ]; then
	echo "usage: $0 distname { DEINSTALL | POST-DEINSTALL }" >&2
	exit 1
fi

# Verify/process the command
#
case $2 in
	DEINSTALL)
	do_restore
	echo ""
	;;
	POST-DEINSTALL)
	;;
	*)
	echo "Usage: `basename $0` distname DEINSTALL | POST-DEINSTALL" >&2
	exit 1
	;;
esac

exit 0
