#!/bin/sh
# Original script written by Karen Shepelak
# 	edited by Troy Dawson May 25, 2000
#       Modified by S. Timm 12/16/2002
printHelp() {
echo "Usage:"
echo "bonnie_test <directory> <size> <mailfile> <disk>"
}
# Now the script has four parameters
# Directory, size, mailfile disk
#Check the arguments
if [ $# -lt 4 ]
then
   printHelp
   exit
fi

TESTDIR=$1
SIZE=$2               #test file size in MB
MAILFILE=$3
DISK=$4
FILE1=/root/test/bonnie_passed.$4
FILE2=/root/test/bonnie_failed.$4
FILE3=/root/test/bonnie_current.$4
FILE4=/root/test/bonnie_history.$4
#
#Put start date in bonnie_current
#
if [ ! -e $TESTDIR ] 
then
	echo "Bonnie attempted to start `date` " >> $FILE3
	echo "$TESTDIR does not exist" >> $FILE3
	exit
fi
#
#Check if lock file is there. 

if [ -e $TESTDIR/bonnie.lock ] 
then
    echo "Bonnie attempted to start `date` " >> $FILE3
    echo "$TESTDIR/bonnie.lock present on $DISK, bonnie is already running" >> $FILE3
    exit
else
    if [ -e $TESTDIR/dailyfill.lock ]
    then
	echo "Bonnie attempted to start `date` " >> $FILE3
        echo "$TESTDIR/dailyfill.lock present on $DISK, dailyfill is running" >> $FILE3
    exit
    else
    	echo "Bonnie started `date` " > $FILE3
    	touch $TESTDIR/bonnie.lock
    fi
fi
#For IDE disks only, check to make sure the DMA is on
HD=${DISK:0:2}
if [ "$HD" = "hd" ]
then
    DMALINE="`/sbin/hdparm /dev/${DISK} | grep using_dma`"
    DMATEST="$(echo $DMALINE | cut -d ' ' -f3)"
    if [ $DMATEST -ne 1 ] 
    then
        echo "DMA isn't on" >> $FILE2
        exit
    fi
fi
#Check free disk space to be sure we have enough:
DFLINE="`df -m $TESTDIR | grep -v Available`"
FREE="$(echo $DFLINE | cut -d ' ' -f4)"
if [ $FREE -lt $SIZE ]
then
    echo "Not enough free space on $DISK" >> $FILE2
    exit
fi

/root/bin/bonnie_large -d $TESTDIR -s $SIZE > /tmp/hdc.$$ 2>&1

#
#check integrity of /tmp/hdc.$$
#
TESTLINE="`head -1 /tmp/hdc.$$`"
TESTFILE=${TESTLINE:0:4}
if [ "$TESTFILE" != "File" ]
then
    date >> $FILE2
    echo "File /tmp/hdc.$$ corrupted" >> $FILE2
    cat /tmp/hdc.$$ >> $FILE2
    exit
fi
if [ `tail -1 /tmp/hdc.$$ | awk '{print $2}'` -ge 10000 ]; then
       echo "Bonnie finished `date`" >> $FILE1
       cat /tmp/hdc.$$ >> $FILE1
       echo "${1} PASSED" >> $FILE1
       echo " "             >> $FILE1
else
       echo "Bonnie finished `date`" >> $FILE2
       cat /tmp/hdc.$$ >> $FILE2
       echo "${1} FAILED" >> $FILE2
       echo ""              >> $FILE2
fi
if [ -e /tmp/hdc.$$ ]
then
	rm /tmp/hdc.$$
fi

if [ -e $FILE1 ] ; then 
	cat $FILE1 >> $FILE3
	rm $FILE1
fi
if [ -e $FILE2 ] ; then 
	cat $FILE2 >> $FILE3
	cat $FILE2 >> $MAILFILE
	rm $FILE2
fi
if [ -e $FILE3 ] 
then
        cat $FILE3 >> $FILE4
fi
if [ -e $TESTDIR/bonnie.lock ]
then
	rm $TESTDIR/bonnie.lock
fi

