#!/bin/ksh
#
#ident	"@(#)flp.stf	1.12	05/06/07 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Floppy Disk Test Routine.
###########################
#
#################################################################
# 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
err=			# floppy error return code
diskin=			# is media inserted?
prgdir=../../prgs	#program directory
maxwarn=2    	  	# max number of nonfatal warnings to tolerate.
warnings=0		# number of warnings so far

checkfloppy=/tmp/probe_cd_flp

# functions
#
clean_up()
{
	echo "Floppy-disk test ending..."
	
}


### 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 any case. )
echo "maxwarnings=${maxwarn}"


echo "Starting Floppy disk test..."

if [ -f $checkfloppy ]
then
	if [ -z `grep floppy $checkfloppy` ]
	then
		echo "!NOTE: Floppy device not found. Ignoring floppy test."
		echo "Floppy-disk test ending..."
		exit 1
	fi
fi

while :
do
	starttime=`$prgdir/time_sec`
	echo "Attempting fdformat..."

	err=`fdformat -Ufqv 2>&1`
	# Options above are U= umount any file systems and then format.
	#		f= Force.   Do  not  ask  for  confirmation.
	#		q= Quiet; do not print status messages.
	#		v= Verify each block of the diskette after the format.

	# CHECK FOR ERRORS
	if [ $? -ne 0 ]
	then
		#echo "!ERROR: Floppy test returned error code/message:\n $err"
		echo "!WARNING: Floppy test returned error code/message:\n $err"
		warnings=`$warnings + 1`
	else
		endtime=`$prgdir/time_sec`
		ltime=`expr $endtime - $starttime`
		if [ $ltime -gt 300 ]
		then
			echo LOOPTIME: $ltime
		fi

		break;
	fi

	# If no diskette media in drive, abort HCT test run...
	#
	# Vold says "No such volume..." and no vold running says,
	# "fdformat: no diskette"
	# variable "err" was filled in with the result of the fdformat command.
	#
	diskin=` echo $err | awk ' /^No such volume \(or no media/ { print "no-media" } /^fdformat: no diskette/        { print "no-media"}' `        

	if [ "${diskin}" = "no-media" ]                                
	then
       		echo "!ERROR:  No Floppy Media Inserted!"
       		echo "!ERROR:  ABORTING HCTS RUN..."
       		exit 1
	fi

	if [ "$warnings" -ge "$maxwarn" ]
	then
		echo "!ERROR: Received $maxwarn !WARNING messages. Terminating test."
		exit 1
	fi

	endtime=`$prgdir/time_sec`
	ltime=`expr $endtime - $starttime`
	if [ $ltime -gt 300 ]
	then
		echo LOOPTIME: $ltime
	fi
done

echo "!PASS: No errors detected in floppy test."

	
clean_up



# sleep if arg
#if [ -n "$1" ]
#then
#	sleep $1
#fi
