#! /bin/csh -f  
# @(#)floppy.sh	1.1	8/21/88
#
# simple (for now) test to stress floppy drive
#
#set echo
#set verbose
#
set MODULE = :Floppytest:
set TMP = /tmp
setenv TMPDIR $TMP	# for tmpfile routine	
set DIR = `tmpfile floppy_`
set loop = 1
unset extra_verbose say_done logfile
set DFT_TIME = 300	# 5 minutes between passes 
set DFT_BS = 512	# default block size 
set DFT_RUN_TIME = -1 
@ seconds = $DFT_RUN_TIME
set DFT_SIZE = 524288	# default max size = 512K
set time = $DFT_TIME
set bs = $DFT_BS
set size = $DFT_SIZE
set dev = /dev/rfd0	# default device name

foreach i ($argv)
    if ($i:x == '-h' || $i:x == 'h' ||  $#argv < 1) then
	echo 'usage: tape <options>:'
	echo '  <file>............floppy dev file'
	echo '  <dir>.............temporary directory'
	echo '  -v................set extra_verbose mode'
	echo '  -l<n>.............loop n times'
	echo '  -a<file>..........set log file'
	echo "  -i<secs>..........sleep interval (secs) between loops (default $DFT_TIME)"
	echo '  -t<[[[dd:]hh:]mm:]ss>...run time (default forever)'
	echo "  -s<size>..........set max file size (default $DFT_SIZE)"
	echo '  -z................write to tty on successful exit'
	echo '  -e................eject disk when done
	echo '  -b<size>..........disk block size
	echo '  -h................this message'
	exit
    else if ($i:x =~ /dev/fd*) then
	set dev = $i:x
    else if ($i:x =~ /dev/rfd*) then
	set dev = $i:x
    else  if ($i:x == -v) then
	set extra_verbose
    else  if ($i:x == '-z') then
	set say_done
    else  if ($i:x == '-e') then
	set eject
    else  if ($i:x =~ -l*) then
	set loop = `echo $i:x | sed 's/-l//'`
	if ($loop == '') then
	    echo '***option note: -l<n> requires <n>umber as argument'
	    set loop = 1
	endif
    else  if ($i:x =~ -a*) then
	set logfile = `echo $i:x | sed 's/-a//'`
	if ($logfile == '') then
	    echo '***option note: -a<file> requires <file> as argument'
	    unset logfile
	endif
    else  if ($i:x =~ -b*) then
	set bs = `echo $i:x | sed 's/-b//'`
	if ($bs == '') then
	    echo '***option note: -b<size> requires <size> as argument'
	    set bs = $DFT_BS
	endif
    else  if ($i:x =~ -i*) then
	set time = `echo $i:x | sed 's/-i//'`
	if ($time == '') then
	    echo '***option note: -i<secs> requires <secs> as argument'
	    set time = $DFT_TIME
	endif
    else  if ($i:x =~ -t*) then
	set temp = `echo $i:x | sed 's/-t//'`	# remove "-t"
	if ($temp == '') then
	    echo '***option note: -t requires <[[[dd:]hh:]mm:]ss> as argument'
	    @ seconds = $DFT_RUN_TIME
   	else
	    set temp = `echo $temp:x | sed 's/:/ /g'`	# remove ":"s
	    if ($#temp == 1) then	# only [ss] entered
                @ seconds = $temp[1]  
	    else if ($#temp == 2) then	# [mm:ss] entered
		@ seconds = ($temp[1] * 60) + $temp[2]
	    else if ($#temp == 3) then	# [hh:mm:ss] entered
		@ seconds = ($temp[1] * 60 * 60) + ($temp[2] * 60) + $temp[3]
	    else if ($#temp == 4) then	# [dd:hh:mm:ss] entered
		@ seconds = ($temp[1] 24 * 60 * 60) + ($temp[2] 60 * 60) + ($temp[3] * 60) + $temp[4]
	    endif
        endif
    else  if ($i:x =~ -s*) then
	set size = `echo $i:x | sed 's/-s//'`
	if ($size == '') then
	    echo '***option note: -s<size> requires <size> as argument'
	    set size = $DFT_SIZE
	endif
    else if ($i:x !~ -*) then
	set DIR = $TMP/$i:x
    endif
end

if (!($?logfile)) then
    echo 'need to set log file with -a<file> option'
    exit
endif

if ($?dev) then #{
    onintr handler
    echo "$MODULE ($dev) OPEN LOG FILE===> `date`" >> $logfile
    set temp1 = $DIR/R0
    set temp2 = $DIR/RS
    if (!(-e $DIR)) then
	 mkdir $DIR
    endif

    @ pass = 0

    if ($seconds != $DFT_RUN_TIME) then
	set pid = $$
	((sleep $seconds ; kill -INT $pid) & )
    endif
#
# see if device really exists
#
    set status = 0
    set output = `opendev $dev`
    set errno = $status
    if ($errno == 2) then
	echo "Skipping $dev test: Device doesn't exist" |& tee -a $logfile
	exit;
    else if ($errno) then
	echo "$MODULE *ERROR=====>>>> Error ($errno) opening $dev" |& tee -a $logfile
	exit;
    endif
#
# device existed...  continue with test loop
#

    while ($pass < $loop || $loop == -1) #{

	set thing = "$MODULE beginning pass $pass"
        echo $thing >> $logfile
	if ($?extra_verbose) echo $thing

	@ pass = $pass + 1

#
# create the file to be written to the floppy device
# (remove the old one if its exists)
#
	\rm -f $temp1
	@ filesize = `filegen -s$size -n1 -m$bs -d$DIR`
	@ blocks = $filesize / $bs
	if ($?extra_verbose) echo "number of blocks = $blocks"
#
# write this file to the raw floppy device
# 
	set output = `dd if=$temp1 of=$dev bs=$bs count=$blocks |& tee -a $logfile` 
#
# see if the write was successful
#
	set errno = $status
	if ($errno) then #{
	    echo "$output"
	    echo "$MODULE *ERROR=====>>>> Error ($errno) on write to $dev" |& tee -a $logfile
	    break
	#} 
	else #{
#
# save the input file
#
	    mv $temp1 $temp2
	    set errno = $status
	    if ($errno != 0) then #{
		if (-e $temp1) then
		    echo "$MODULE *ERROR ($errno) cannot acces file $temp1" |& tee -a $logfile
		else
		    echo "$MODULE *ERROR file $temp1 has disappeared" |& tee -a $logfile
		endif
		break
	    endif #}
#
# read the file back from the floppy
#
	    set output = `dd if=$dev of=$temp1 bs=$bs count=$blocks |& tee -a $logfile`

	    set errno = $status
	    if ($errno) then
	        echo "$output"
	        echo "$MODULE *ERROR=====>>>> Error ($errno) on read of $dev" |& tee -a $logfile
	        break
	    else #{
#
# now compare the files and check for errors
#
	        set compare = `cmp $temp1 $temp2 |& tee -a $logfile` 
	        set errno = $status
	        if ($errno) then
		        if (!(-e $temp1)) then
			    echo "$MODULE *ERROR file $temp1 nonexistant" |& tee -a $logfile
		        else if (!(-e $temp2)) then
			    echo "$MODULE *ERROR file $temp2 nonexistant" |& tee -a $logfile
		        else
			    echo "$MODULE *ERROR ($errno) comparing files $temp1, $temp2" |& tee -a $logfile
		        endif
		else
		    if ($#compare) then
		        echo "$MODULE *ERROR: $compare" |& tee -a $logfile
		    endif
	        endif
	    endif #}
	    \rm -f $temp2
	endif #}
	@ secs = 0
        while ($secs < $time)
	   sleep 1
	   @ secs++
        end
    end #}

#  \rm -f $temp1
#  \rm -rf $DIR
endif #}
#
#fall thru
#
handler:
    \rm -f $temp1
    \rm -rf $DIR

    if ($?say_done) then
        echo "$MODULE EXIT($status)===> `date`"
    endif

    if ($?eject) then
        eject
    endif
    echo "$MODULE EXIT($status)===> `date`" >> $logfile
    exit
