#!/bin/ksh
#
#ident	"@(#)Fs_stress.stf	1.17	05/06/08 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This is the Fs_stress test module.
#
#########################################################################
# NOTES ON OPERATION AND ERROR HANDLING:
# This script is called by staf.eng, which is the main test harness.
# Standard output gets recorded in the test module log file by staf.eng.
#
# Variable maxwarn is checked by staf.eng in the "chk_err()" monitoring process,# by grep'ing the log file of this test module.
# If maxwarn is exceeded, the staf.eng script will kill this test module.
# There are two formats for the error echos to the log files;
#
# 1.   echo "!ERROR: blah blah foo bar";
#      which will terminate the test.  If the test harness detects
#      a string match with "!ERROR", then all testing terminates.
#
# 2.   echo "!WARNING: blah blah foo bar";
#       The above format "!WARNING:" will make the log file entry,
#       but all testing will continue, no termination of tests,
#       UNLESS maxwarnings variable is exceeded.
#
# DANGER!!:  Even if you do not intend to use WARNING messages, variable
# maxwarn *MUST* be defined and the "maxwarnings=" line *MUST* be
# echo'ed to the logfile.  This syntax is mandatory.
#########################################################################


# variables
prgdir=../../prgs		# program directory
maxwarn=50		# max number of nonfatal warnings to tolerate.
args=$*			# arguments
disk_No=0
set -A array
FSTR_BIN=`isainfo -b`

# functions

clean_up()
{
	echo "Fs_stress Ending!"
	if [ -f /tmp/drives ]
	then
		rm -f /tmp/drives
	fi
}

### MAIN ###

# set trap
trap 'clean_up; exit' 17

# SET LOGFILE FLAG FOR MAX WARNING COUNT
# This is checked by staf.eng, and if exceeded, staf.eng will kill this module.
# ( This only makes sense if this script echos warning messages, but
#   inclusion of this variable in the log file is *MANDATORY* in all cases. )
#echo "maxwarnings=${maxwarn}"

if [ -z "$args" ]
then
	disk_No=1
elif [ "$1" == "--disk_specified" ] && [ $# -eq 2 ]
then
	disk_No=$2
fi

if [ $disk_No -ne 0 ]
then
	((disk_No = (${disk_No} - 1) * 4 + 1))
	sed_cmd="sed -n '"$disk_No"p'"
	targdev=`$prgdir/sliceinfo | awk '{print \$1}' | eval ${sed_cmd}`
	if [ -z "$targdev" ]
	then
		echo "!ERROR: The target drive doesn't exist!"
		exit 1
	elif [ "$targdev" == "reserved" ]
	then
		((disk_No = ${disk_No} + 1))
		sed_cmd="sed -n '"$disk_No"p'"
		targdev=`$prgdir/sliceinfo | awk '{print \$1}' | eval ${sed_cmd}`
		args="-F ufs -D $targdev -r -n 30 -f 128k:32m -t 1k:256k -s"
		array[0]=$args
	else
		args="-F ufs -D $targdev -r -n 30 -f 128k:32m -t 1k:256k -s"
		array[0]=$args
		((disk_No = ${disk_No} + 1))
		sed_cmd="sed -n '"$disk_No"p'"
		targdev=`$prgdir/sliceinfo | awk '{print \$1}' | eval ${sed_cmd}`
		if [ -z "$targdev" ]
		then
			echo "!ERROR: The target drive doesn't exist!"
			exit 1
		fi
	        args="-d -D $targdev -r -n 30 -f 512b:2m -t 512b:256k -s"
		array[1]=$args
	fi
else
	array[0]=$args
fi

if [ $FSTR_BIN = 64 ]
then
	RUNTIME=`expr $RUNTIME / 2`
fi

echo "Starting Fs_stress test $FSTR_BIN..."
cd Fs_stress/bin
HCTS_ENV=yes
export HCTS_ENV
export FSTR_BIN
((RUNTIME=${RUNTIME}/${#array[@]}))
export RUNTIME

i=0
while [ $i -lt ${#array[@]} ]
do
	./runtest.sh ${array[$i]}
	let "i = $i + 1"
done

if [ $FSTR_BIN = 64 ]
then
	FSTR_BIN=32
	echo "Starting Fs_stress test $FSTR_BIN..."
	export FSTR_BIN
	
	i=0
	while [ $i -lt ${#array[@]} ]
	do
		./runtest.sh ${array[$i]}
		let "i = $i + 1"
	done
fi

clean_up
