#!/bin/sh
# Original script written by Karen Shepelak
# 	edited by Troy Dawson May 25, 2000
#       Modified by S. Timm 8/2/2002
printHelp() {
echo "Usage:"
echo "bonnie_test <directory> <size> <mailfile> <disk>"
}
# Now the script has three parameters
# Directory, size, mailfile
#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
#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 $TESTDIR | grep -v Available`"
FREE="$(echo $DFLINE | cut -d ' ' -f4)"
if [ $FREE -lt 1048576 ]
then
    echo "Not enough free space on $DISK" >> $FILE2
    exit
fi

/root/bin/bonnie -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
       date >> $FILE1
       cat /tmp/hdc.$$ >> $FILE1
       echo "${1} PASSED" >> $FILE1
       echo " "             >> $FILE1
else
       date >> $FILE2
       cat /tmp/hdc.$$ >> $FILE2
       echo "${1} FAILED" >> $FILE2
       echo ""              >> $FILE2
fi

rm /tmp/hdc.$$

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
