#!/bin/bash
#
#    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"
EMAIL_MONITOR_LOG_FILE="/tmp/.email_fail_count_log.txt"
EMAIL_LOCK_FILE="/tmp/.email_sent_lock"
EMAIL_ADDR_LIST="/tmp/.email_addr_fail_list.txt"
SCRIPT_EXEC_LOCK="/tmp/.process_email_lock"

# process the error logs in spool directory and send the raslog message
# for every email address. There are 2 types of mail queue files - df* and qf*,
# the qf* are the queue control files and df* are data files. We have to process
# the qf* files for detecting sendmail failure.
#if [ -n "$(ls -A "$MQPATH")" ]; then
SCRIPT_EXEC_LOCK="/tmp/.process_email_lock"
if [ -f "$SCRIPT_EXEC_LOCK" ]; then
	exit 0
fi
if [ -f "$MAILSUBMITCONF" ]; then
	ERROR_LOG=$MQPATH_SKYBOLT
else
	ERROR_LOG=$MQPATH
fi
num_files=`ls -A $ERROR_LOG | wc -l`
if [ $num_files -ne 0 ]; then
    for i in `ls $ERROR_LOG/qf*`
	do
	addr_line=`cat $i| grep To`
	addr=`echo $addr_line | cut -f2 -d :`
	flag_raslog=0
	if [ -n "$addr" ]; then
# send 1 raslog message for every email address configured.
		if [ -f "$EMAIL_LOCK_FILE" ]; then
			raslog_cnt=`cat $EMAIL_ADDR_LIST| wc -l`
			cat $EMAIL_ADDR_LIST| grep $addr
			is_existing_addr=$?
			if [ $is_existing_addr -ne 0 ]; then
				flag_raslog=1
			fi
# If the raslog has been sent for this email address, update the failure
# count and write it to $EMAIL_MONITOR_LOG_FILE
			if [ -f "$EMAIL_MONITOR_LOG_FILE" ]; then
				email_fail_count=`cat $EMAIL_MONITOR_LOG_FILE`
				email_fail_count=`expr $email_fail_count + 1`
				echo $email_fail_count > $EMAIL_MONITOR_LOG_FILE
			fi
		else
# handle first email failure, create the lock file, and initialize
# error failure count file
			flag_raslog=1
			if [ ! -f "$EMAIL_LOCK_FILE" ]; then
				touch $EMAIL_LOCK_FILE
				email_fail_count=1
				echo $email_fail_count > $EMAIL_MONITOR_LOG_FILE
			fi
			touch $EMAIL_ADDR_LIST
		fi
	fi
	if [ $flag_raslog -eq 1 ]; then
		/fabos/cliexec/errlogtest -i MAPS-1206
		echo $addr >> $EMAIL_ADDR_LIST
	fi
	done
# kill the sendmail processes from the previous cron job
	killall sendmail
fi
# clean the mail files of sendmail, base on Makarand requirement
rm -f $ERROR_LOG/*
# 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`

	touch $SCRIPT_EXEC_LOCK
	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
			#FROM_ADDR will have the configured from address
			FROM_ADDR=`sed -n '1 p' $FILE | cut -d ':' -f 2 | cut -d "<" -f 2 | cut -d ">" -f 1`
			if [ -f /usr/sbin/sendmail ]; then
				sendmail -oe=q -Nnever -f $FROM_ADDR -t $EMAIL_ADDR  < $FILE
				STATUS=$?
				if [ $STATUS -ne 0 ]; then
					echo  "Sendmail returned error status=",$STATUS
				fi
			fi
			rm $FILE
		fi
	done
	rm $SCRIPT_EXEC_LOCK
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
