#!/bin/sh

# change to 0 to turn off a specific output
__logtofile="1"
__sendemail="1"
__addressfile="/cfg/emailaddress"
__mailserverfile="/cfg/mailserver"




###########################################################
if [ ! -f $__addressfile ]; then
    echo "Inter-MC check can not take place. Please specify a /cfg/emailaddress file containing the destination email address."
fi
if [ ! -f $__mailserverfile ]; then
    echo "Inter-MC check can not take place. Please specify a /cfg/mailserver file containing the mail server IP address."
fi
__address=`cat $__addressfile`
__mailserver=`cat $__mailserverfile`
__ipaddress=`ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://'`
__filesize=5000
__logfile="/cfg/intermccheck.log"
notify()
{
    echo "to: $__address" >/mnt/ramdisk/temp.mail
    echo "from: $__ipaddress@dothill.com" >>/mnt/ramdisk/temp.mail
    echo "subject: $__ipaddress $1" >>/mnt/ramdisk/temp.mail
    echo "" >>/mnt/ramdisk/temp.mail
    echo "Test Started/Completed: $STARTTEST/$ENDTEST" >>/mnt/ramdisk/temp.mail
    /usr/sbin/sendmail -S $__mailserver -t </mnt/ramdisk/temp.mail
    rm /mnt/ramdisk/temp.mail
}
writelog() 
{
    if [ "$__logtofile" -eq "1" ]; then
        if [ !-f "$__logfile" ]; then
            touch "$__logfile"
        fi

	    echo "$1" >> "$__logfile"
    fi
}

STARTTEST=`date "+%Y-%m-%d %H:%M:%S"`
cd /mnt/ramdisk
dd if=/dev/zero of=bigfile bs=1024 count=$__filesize
tar c * | pfuc othermcinner
ENDTEST=`date "+%Y-%m-%d %H:%M:%S"`

sleep 10
result=`ping -c 1 -W 20 othermcinner | grep "1 packets received" | wc -l`

if [ "$result" -eq "0" ]
then
    notify "Inter-MC has a wedgie!!"
    writelog ""
	writelog "*************ERROR!*************************"
	writelog "$STARTTEST/$ENDTEST: Inter-mc is wedgied!!"
	writelog "************/ERROR!*************************"
    writelog ""
else
    notify "Inter-MC link check passed"
	writelog "$STARTTEST/$ENDTEST: Inter-MC check A-OK"
    rpcsys othermcinner "delayedreboot 5"
    delayedreboot 10
fi
