#!/bin/sh
# Original script written by Karen Shepelak
# 	edited by Troy Dawson May 25, 2000
#       Modified by S. Timm 12/16/2002
#      9/3/2003--special version to use with the "dailyfill" feature
#      If there is a bonnie.lock from a currently running bonnie
#      sleep 3 minutes and try again.  Then our dailyfill lock
#      will make sure no other bonnies are running at the moment.
#
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/dailyfill_passed.$4
FILE2=/root/test/dailyfill_failed.$4
FILE3=/root/test/dailyfill_current.$4
FILE4=/root/test/dailyfill_history.$4
#
#Put start date in dailyfill_current
#
if [ ! -d $TESTDIR ]
then
	echo "Dailyfill attempted to start `date` " >> $FILE3
	echo "$TESTDIR does not exist " >> $FILE3
fi
#
#Check if lock file is there. 
if [ -e $TESTDIR/dailyfill.lock ] 
then
	echo "Dailyfill attempted to start `date` " >> $FILE3
	echo "$TESTDIR/dailyfill.lock present on $DISK, dailyfill is already running" 
	exit
fi
while [ -e $TESTDIR/bonnie.lock ]
do 
    echo "Dailyfill attempted to start `date` " >> $FILE3
    echo "$TESTDIR/bonnie.lock present on $DISK, bonnie is already running" >> $FILE3
    sleep 180
done
    echo "Dailyfill started `date` " > $FILE3
    touch $TESTDIR/dailyfill.lock
#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/dailyfill.lock ]
then
	rm $TESTDIR/dailyfill.lock
fi

