#! /usr/bin/sh
#
#ident	"@(#)tape.sh	1.7	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# by Mark F. Logan - rewritten 22/12/1999 by Andrew McCarthy
# rewritten by Badrinath Kollu(01/26/99).

testdir=`pwd`
tmpfile=/tmp/result.$$
tmplist_file=/tmp/tmplist_file.$$
prgdir=opt/SUNWstaf/prgs

#
# filltape/verifytape tests
#
./tapetest -b 4 -d $1 -s 50
if [ $? -ne 0 ]
then
	echo "!ERROR: tapetest failed!"
	>/tmp/tape_result
	exit 1
fi

# Not all systems will have the files or dir's present in the file files.
# So strip the unwanted stuff.
for fil in `cat files`
do
	if [ -f /$fil -o -d /$fil ] 
	then 
	echo $fil >> ${tmplist_file}
	fi
done

FILES=`cat ${tmplist_file}`

#
# cpio tests
#
# cpio the FILES to tape.
cd /
find $FILES -print|cpio -ocB > $1
if [ $? -ne 0 ]
then
	echo "!ERROR: cpio out failed!"
	>/tmp/tape_result
	exit 1
fi

cd /mnt

rm -rf * .[A-z0-9]*
echo "rm left the following files:"
ls -la

# cpio the file from tape to the /mnt dir.
cpio -icdumB < $1
if [ $? -ne 0 ]
then
	echo "!ERROR: cpio in failed!"
	>/tmp/tape_result
	exit 1
fi

cd $testdir
for i in `cat files`
do
	# check if they are regular files or dir's, else do not compare
	if [ -f $i -o -d $i ] 
        then
	echo $i ---------------------------------------------------------------
	if [ -f /$i ]
	then
		cmp /$i /mnt/$i
	else
        	if [ `uname -r` = "5.8" ] 
		then 
			# If solaris version is 5.8, dircmp command 
			# has problems, use the modified version 
			# present in the local directory.
			# This part of code can be removed after 
			# the problem with dircmp is fixed. 
              		${prgdir}/dircmp -ds $i /mnt/$i > ${tmpfile}
           	else   
              		dircmp -ds $i /mnt/$i > ${tmpfile}
        	fi
	fi
	if [ -s ${tmpfile} ]
	then
		echo "!ERROR: compare after cpio failed on $i"
		>/tmp/tape_result
		exit 1
	fi
        fi # if [ -f $i -o -d $i ]
done

#
# tar tests
#

cd /
tar cf $1 $FILES
if [ $? -ne 0 ]
then
	echo "!ERROR: writing tar to tape failed"
	>/tmp/tape_result
	exit 1
fi

cd /mnt

rm -rf * .[A-z0-9]*
echo "rm left the following files:"
ls -la
tar xf $1
if [ $? -ne 0 ]
then
	echo "!ERROR: reading tar from tape failed"
	>/tmp/tape_result
	exit 1
fi

>${tmpfile}
cd $testdir
for i in `cat files`
do
	# check if they are regular files or dir's, else do not compare
	if [ -f $i -o -d $i ] 
        then
	echo $i ---------------------------------------------------------------
	if [ -f /$i ]
	then
		cmp /$i /mnt/$i
	else
                if [ `uname -r` = "5.8" ]
		then
			# If solaris version is 5.8, dircmp command 
			# has problems, use the modified version 
			# present in the local directory.
			# This part of code can be removed after 
			# the problem with dircmp is fixed. 
                        ${prgdir}/dircmp -ds $i /mnt/$i > ${tmpfile}
                else
                        dircmp -ds $i /mnt/$i > ${tmpfile}
                fi
	fi
	if [ -s ${tmpfile} ]
	then
		echo "!ERROR: compare after tar failed on $i"
		>/tmp/tape_result
		exit 1
	fi
	fi #if [ -f $i -o -d $i ]
done
rm -rf ${tmpfile}
rm -rf ${tmplist_file}

