#!/bin/ksh
#
#ident  "@(#)dd.stf 1.15     05/06/15 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# THIS IS THE HARD DISK 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
diskslc=$1			# input device
maxwarn=50			# max number of nonfatal warnings to tolerate.
dls=				# data list
dfoutp=				# df output
fsys=				# file system
mpt=				# mount point
disk_No=0

# argument parsing
if [ $# -gt 2 ]
then
        echo "!ERROR: Invalid argument."
        echo "Usage: $0 [raw device] | --disk_specified disk_no"
	exit 1
fi

# functions

# clean up output file
clean_up()
{
echo "dd Ending!"
if [ -f /tmp/drives ]
then
	rm -f /tmp/drives
fi
}


# list harddrive info
if [ -z "$diskslc" ] 
then
	disk_No=1
elif [ "$diskslc" == "--disk_specified" ]
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'"
        slc=`$prgdir/sliceinfo | awk '{print \$1}' | eval ${sed_cmd}`
        if [ -z "$slc" ]
        then
                echo "!ERROR: The target drive doesn't exist!"
                exit 1
	elif [ "$slc" == "reserved" ]
	then
		slc=`$prgdir/sliceinfo | awk '{print $1}' | sed -n '4p'`
                echo "!ERROR: Can't run dd on reserved slice!"
                exit 1
	else
		((disk_No = ${disk_No} + 3))
        	sed_cmd="sed -n '"$disk_No"p'"
        	slc=`$prgdir/sliceinfo | awk '{print \$1}' | eval ${sed_cmd}`
		diskslc=$slc
        fi
fi

diskslc=`echo $diskslc | sed -e "s/\/dev\/[r]*dsk\///"`

if [ ! -c "/dev/rdsk/$diskslc" ]
then
	echo "!ERROR: $diskslc is not a character device."
	exit 1
fi

echo "<B><U>Harddrive</U></B>:"
echo "Hardrive slice = $diskslc"
### MAIN ###

# check args
if [ -z "$diskslc" ]
then
	echo "!ERROR: No hard drives were found."
	exit 1

fi

# 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}"


echo "Starting dd test..."

cd dd/bin
./runtests $diskslc

# remove output
clean_up

