#!/bin/sh
#
# Pre/post-installation setup of ravpostfix

PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX="/usr/local"
BIN_PATH="${PREFIX}"
ETC_PATH="${PREFIX}/etc/rav"

RAVPOSTFIX_CONTROL_FILE="${PREFIX}/etc/rc.d/rpostfix.sh"
RAVPOSTFIX_SAMPLE_CONTROL_FILE="${PREFIX}/share/examples/rav/rpostfix.sh"
FILTER_FILE="${PREFIX}/bin/ravpostfix"
FILTER_ARGS="-s 10025 -c 10026"
_USER="ravms"
_GROUP="ravms"

# Function: install ravpostfix control file from the samples
#
do_activate()
{
	echo ""
	if [ -f $RAVPOSTFIX_CONTROL_FILE ]; then
		echo "| The existing $1 control file,"
		echo "| $RAVPOSTFIX_CONTROL_FILE,"
		echo "| has NOT been changed."
		echo "| A new control script sample can be found in"
		echo "| $RAVPOSTFIX_SAMPLE_CONTROL_FILE."
	else
		# Install ravpostfix control script.
		install -c -o root -g wheel -m 755 $RAVPOSTFIX_SAMPLE_CONTROL_FILE $RAVPOSTFIX_CONTROL_FILE
		echo "| You can start/stop ravpostfix using"
		echo "| $RAVPOSTFIX_CONTROL_FILE script."
	fi

main_cf=/etc/postfix/main.cf
master_cf=/etc/postfix/master.cf

	
	if [ -f $main_cf -a -f $master_cf ]; then
		echo "-> Modifying the postfix configuration ..."
cat >> $main_cf <<EOF
#rav-begin: RAV AntiVirus Configuration
content_filter=smtp:[127.0.0.1]:10025
#rav-end
EOF

cat>> $master_cf <<EOF
#rav-begin: RAV AntiVirus Configuration
127.0.0.1:10026 inet n - n - 100 smtpd
                        -o content_filter= -o myhostname=dummy.domain.name
#rav-end
EOF

		echo "-> Reloading postfix configuration ..."
		postfix reload 2>/dev/null
	else
		echo "| You have to modify the postfix configuration files"
		echo "| manually. Please read the INSTALL file for more information."
	fi
	
	echo "-> Starting the ravpostfix daemon ..."
	${FILTER_FILE} ${FILTER_ARGS}
}

# Function: tell the user what s/he needs to do to use the port just installed
#
do_notice()
{
	echo ""
	echo "| Start ravpostfix daemon with:"
	echo "|	${FILTER_FILE} ${FILTER_ARGS}"
	echo "| or"
	echo "|	$RAVPOSTFIX_CONTROL_FILE start"
}

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

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

exit 0
