#!/bin/csh
#
#
#

set topdir=$1
set npasses=$2

set fop=./fop
set random=./random

echo ==== removing ...
set nonomatch
rm -rf $topdir/dir*
unset nonomatch

set np=0
while ($np < $npasses)
	@ np += 1
	set ndir=`$random 5 20`
	echo ==== creating $ndir directories
	set nd=$ndir
	while ($nd > 0)
		set dir=$topdir/dir$nd
		mkdir $dir
		set nb=`$random 10 100`
	
		set file=$dir/file0
		$fop -o m -b $nb $file

		set nf=`$random 10 100`
		echo ==== creating $nf files in dir $nd
		while ($nf > 0)
			ln $file $dir/file$nf
			@ nf -= 1
		end
		@ nd -= 1
	end

        echo ==== verifying....
	foreach f ($topdir/*/file0)
		$fop -o v $f
                if ( $status == 2 ) then
                        echo ==== VERIFY $f FAILED
                        echo ==== killall fsr
                        goto killfsr
                endif
        end

	echo ==== removing ...
	rm -r $topdir/dir*
end

killfsr:
echo ====
kill -HUP -$$
