#!/bin/ksh

#############################################################################
##
## iMS monitoring and alarm script
##
## This script is part an example framework of what can be done.
## There are MANY ways that you can take this from here.
##
## File: $Rsendreport
##       A smtp client script to send a file to a list of recipients.
##
#############################################################################


if [ -z $HEALTHMON_TMPDIR ]; then
     . $_CURR_WDIR/../etc/alarms.cfg
fi

SENDER=""
RECIPIENT=""
SUBJECT=""
FILE=""
TIMEOUT="90"
SENDSMTP=$IMMONITOR_ACCESS


Usage()
{
    echo "sendreport -f sender -r recipient -s subject -f file"
    exit 0
}

while [ $# -gt 0 ]
do
    case "$1" in
	-o)	shift; SENDER=$1;;
	-r)	shift; RECIPIENT=$1;;
	-s)	shift; SUBJECT=$1;;
	-f)	shift; FILE=$1;;
	-t)	shift; TIMEOUT=$1;;

	-help)	Usage;;
    esac
    shift
done


if [ ! $SENDER ] || [ ! $RECIPIENT ] || [ ! "$SUBJECT" ] || [ ! $FILE ]; then
	Usage
fi

LIST=`echo $RECIPIENT | sed 's/,/ /g'`

for RCPT in $LIST ; do
    DOMAIN=`echo $RCPT | $AWK '{FS="@"
                             {print$2}}'`

    if [ ! -z "$ROUTER" ]; then
	MXLIST=$ROUTER
    else
    	MXLIST=`nslookup -type=mx $DOMAIN | grep "mail exchanger" | \
            $AWK '{ {printf("%s%s\n", $4, $8)}}' | sed 's/,/ /' | \
            sort -n | $AWK '{ print $2 }'`
    fi 

    for MX in $MXLIST ; do
        $IMMONITOR_ACCESS -S $MX:25=$TIMEOUT -f $SENDER -u $RCPT -k "'$SUBJECT'" -m $FILE 
	RESULT=$?
	if [ $RESULT -eq 0 ]; then
	    break
	fi
    done
done

## end of script
