#!/bin/sh

xamimepath=$1
rcfile=""


uname -a | grep -q 'FreeBSD'
if [ $? -ne 0 ]; then 
   exit 1
   fi

if [ -e /etc/rc.conf ]; then
	rcfile=/etc/rc.conf

elif [ -e /etc/rc ]; then
	rcfile=/etc/rc
fi



if [ "x$rcfile" = "x" ]; then
   exit 1
   fi

current_flags=`grep sendmail_flags $rcfile | grep " -bd"`


if [ -f /etc/defaults/rc.conf ]; then
	default_flags=`grep sendmail_flags /etc/defaults/rc.conf | grep " -bd"`
	fi

# 
# If there's currently nothing in /etc/rc.conf
#	then add the default flags
#
if [ "x$current_flags" = "x" ]; then
	if [ "x$default_flags" = "x" ]; then
		# Both the current flags and the defaults flags are empty
		# so create your own.
		echo 'sendmail_flags=" -bd -q30m"' >> $rcfile
	else
		echo "$default_flags" >> $rcfile
	fi
fi



#
# Okay, now that we've got some common ground (that is, there are
#	flags in the /etc/rc.conf file) we can start to alter those
#	flags so that they will support the required Xamime startup.
#
if [ -e $rcfile ]; then
	linecount=`grep sendmail_flags $rcfile | grep " -bd" | wc -l`
	if [ $linecount -gt 0 ]; then
		printf "  sendmail options located..."
		if [ $rcfile = "/etc/rc.conf" ]; then
			./replaceit --input=$rcfile "sendmail_flags=\"" "sendmail_flags=\" -C $xamimepath/xamime.cf " "-xamime.cf" "-if" "+-bd" "-echo"
		elif [ $rcfile = "/etc/rc" ]; then
			./replaceit --input=$rcfile "/sendmail " "/sendmail -C $xamimepath/xamime.cf " "-xamime.cf" "-if"
		fi

		linecount=`grep "xamime.cf" $rcfile | wc -l`
		if [ $linecount -gt 0 ]; then
			printf "successful update..."
		else
			printf "Update did not succeed, please add '-C $xamimepath/xamime.cf' manually\n\
to your $rcfile sendmail startup routine..."
		fi
		printf "done\n"
	fi
fi
