#!/usr/bin/ksh 
#
#ident	"@(#)cdrom.sh	1.13	05/06/07 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

prgdir=../../../../prgs

srcfiles=`$prgdir/free_spc`/cdrom.$$
destdir=${srcfiles}_dir
cdsrc=$1
maxwarn=2
warnings=0


# mkdir $destdir for cmp
mkdir $destdir 2>/dev/null

# create catalog for cdfiles
echo "Building list of files from CD-ROM."
find $cdsrc -type f -print | egrep -v ' |\*|\?' > $srcfiles

# check for errors
if [ $? -ne 0 ]
then
        echo "!ERROR: CD-ROM drive $cdsrc read error -> $srcfiles"
        exit 1
fi

echo $srcfiles

echo "End of building list of files from CD-ROM."

err=0
while [ -d /proc/$sleepid ]
do
	starttime=`../../../../prgs/time_sec`

	for srcfile in $(cat $srcfiles)
	do
		src_f=$(basename $srcfile)

		cp $srcfile $destdir 2>&1
		
		if [ $? -ne 0 ]
		then
			echo "!WARNING: cp $srcfile $destdir failed"
			err=1
		fi

		cmp -l $destdir/$src_f $srcfile 2>&1

		if [ $? -ne 0 ]
		then
			echo "!WARNING: cmp -l $destdir/$src_f $srcfile failed"
			err=1
		fi
	
        	rm $destdir/$src_f 2>&1 | sed 's/^/!WARNING  : /g'
    	done

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

	if [ $err = 1 ]
	then
		warnings=`expr $warnings + 1`
		err=0
	else
		echo "!PASS: No errors found in CD-ROM test"
		break;
	fi

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

rm -rf $srcfiles*
