#!/bin/sh
#
#    Copyright (c) 1996-2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Shell script run by cron job per minut 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 file to /tmp/spool before processing.
#    4. When process the mail, the file is deleted by this script file.
#    5. Dependency: mail package and its IPC function "sendmail"
#
#
# Testing shell program
#
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/fabos/sbin:/fabos/bin
MQPATH=/tmp/spool/mqueue
#
# main
#
# clean the mail files of sendmail, base on Makarand requirement
rm -f $MQPATH/*

TEMPMAILDIR="/tmp"
if [ -f /tmp/fwdMailList.txt ]; then
	rm /tmp/fwdMailList.txt
	fileList=`ls -1 $TEMPMAILDIR/fwdMail????.txt`
	mv $fileList /tmp/spool
	fileList=`ls -1 $TEMPMAILDIR/spool/fwdMail????.txt`
else
	exit 0
fi

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

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

if [ -f /tmp/fwdMailList.txt ]; then
	rm -f /tmp/fwdMailList.txt
fi
