#!/bin/sh
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Shell script run by cron job per minute to process mail files
#    generated by Fabric Watch Daemon.
#    1. The format of file name is fwdMailYxxx.txt
#	fwdMail:	prefix
#	Y:		switch #
#	xxx:		sequence number of mail file.
#	for example:	fwdMail1002.txt - is 2nd mail from switch 1
#			fwdMail0101.txt - is 101 mail from switch 0.
#    2. The file location is at /tmp of the switch.
#    3. The Cronjob will move those files to /tmp/spool before processing.
#	Files will be moved to /tmp/spool only after checking sendmail 
#	process count.
#    4. If the outstanding mail count in /tmp is too high, /tmp/fwdOverflow.txt
#	file will be created to indicate overflow condition to fwd daemon
#    5. After processing the mails, the fwdMailList.txt file will be deleted 
#	by this script
#    6. Dependency: mail package and its IPC function "sendmail"
#
#
# Testing shell program
#
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/fabos/sbin:/fabos/bin
MAILCONFDIR=/etc/mail
MAILSUBMITCONF=${MAILCONFDIR}/submit.cf
MQPATH=/tmp/spool/mqueue
MQPATH_SKYBOLT=/tmp/spool/clientmqueue
#
# main
#
# clean the mail files of sendmail, base on Makarand requirement
rm -f $MQPATH/*
# clean the spool directory in skybolt
if [ -f "$MAILSUBMITCONF" ]; then
    rm -f $MQPATH_SKYBOLT/*
fi

TEMPMAILDIR="/tmp"

DAEMON="sendmail"	# daemon is sendmail
SENDMAIL_MAX=25 	# tune this value to limit the outstanding mails
TEMP_MAIL_MAX=25	# Max no of mails that can be queued to /tmp dir
TEMP_MAIL_MIN=10	# Low water mark for clearing overflow condition
SPOOLED=""		# clear spooled flag

# if there is work to do
if [ -f $TEMPMAILDIR/fwdMailList.txt ]; then

	FILE_LIST=`ls -1tr $TEMPMAILDIR/fwdMail[0-9]???.txt`

	# get the sendmail process count
	SENDMAIL_COUNT=`ps -ef | grep "$DAEMON" | grep -v grep | wc -l`
	MAILQ_COUNT=$SENDMAIL_COUNT

	for FILE in $FILE_LIST; do
		if [ $MAILQ_COUNT -lt $SENDMAIL_MAX ]; then
			mv $FILE $TEMPMAILDIR/spool
			#increment MAILQ_COUNT when queing mails to /tmp/spool
			MAILQ_COUNT=`expr $MAILQ_COUNT + 1`
			SPOOLED=true
		else
			# sendmail process count has exceeded the MAX
			break
		fi
	done
else
	# no work to do, so exit
	exit 0
fi

if [ ! -d $TEMPMAILDIR/spool ]; then
	echo "$0\: $TEMPMAILDIR/spool not a directory"
fi

# Mails will be present in /tmp/spool only if SPOOLED flag is set
if [ $SPOOLED ]; then
	FILE_LIST=`ls -1 $TEMPMAILDIR/spool/fwdMail[0-9]???.txt`

	for FILE in $FILE_LIST; do
		if [ -f $FILE ]; then
			EMAIL_ADDR=`sed -n '2 p' $FILE | cut -d ':' -f 2`
			echo "Email address: " $EMAIL_ADDR
			echo "File Name: " $FILE
			if [ -f /usr/sbin/sendmail ]; then
				sendmail -oe=q -Nnever -t $EMAIL_ADDR  < $FILE &
				STATUS=$?
				if [ $STATUS -ne 0 ]; then
					echo  "Sendmail returned error status=",$STATUS
				fi
			fi
			rm $FILE
		fi
	done
fi

# If /tmp/fwdMail????.txt count is high indicate overflow condition to 
# fwd daemon
TMPDIR_MAIL_COUNT=`ls -1 $TEMPMAILDIR/fwdMail[0-9]???.txt 2>/dev/null | wc -l`
if [ $TMPDIR_MAIL_COUNT -gt $TEMP_MAIL_MAX ]; then
	touch $TEMPMAILDIR/fwdOverflow.txt
elif [ $TMPDIR_MAIL_COUNT -lt $TEMP_MAIL_MIN ]; then
	rm -f $TEMPMAILDIR/fwdOverflow.txt
	if [ $TMPDIR_MAIL_COUNT -eq 0 ]; then
		rm -f $TEMPMAILDIR/fwdMailList.txt
	fi
fi
