#!/bin/sh
PATH=/usr/lbin:$PATH
export PATH
tp1=`selectdevice -q "Select which tape device:" -c $$ /dev/rSA qtape`
tp2=`ls -l $tp1  |sed 's/.*\(...\),.*$/\1/'`
no_rewind=/tmp/no_rewind$$
rewind=/tmp/rewind$$
mknod $rewind c $tp2 0
mknod $no_rewind c $tp2 1
count=0
case $1 in
	READ)
		echo "\nPlace tape to be read in the $tp1 drive! Hit RETURN when ready! \c"
		read junk
		< $rewind
		< $rewind
                echo "\nCopying records from $tp1 to /dev/null"
                echo "\n       The copy is now in progress, Be patient!!!"
                while true
                do
		dd if=$no_rewind of=/dev/null bs=16k 2>>/tmp/junk$$ 
		junk=$?
		if [ $junk -ne 0 ]
		then
		break
		else
                count=`expr $count + 1`
                echo ".\c"
		fi
                done
		;;
		
        CMP)
		echo "\nPlace tape to be checked in the $tp1 drive! Hit RETURN when ready! \c"
		read junk
		< $rewind
		< $rewind
		echo "\n\n\nEnter directory where valid tape image is stored -   \c"
		read node1
                if test -d $node1
		then
			find $node1 -print >/tmp/src$$
			ed -s /tmp/src$$ <<!
			g/^/s//cmp /
			w
			q
!
			echo "\n\n\nEnter directory to be used for temporary storage -   \c"
			read node
	                if test -d $node
	                then
	                        echo "\nDirectory $node already exists! Continue ??? (y or n) \c"
	                        read answer
	                        if test $answer = "n"
	                        then
	                                exit
	                        fi
	                else
	                        if `mkdir $node`
	                        then
	                                echo "\nMaking $node directory"
	                        else
	                                exit
	                        fi
	                fi
	                LIMIT=`ulimit`
	                ulimit 409600
	                echo "\nCopying records from $tp1 to $node"
	                echo "\n       The copy is now in progress, Be patient!!!\n"
	                while `dd if=$no_rewind of=$node/$count bs=16k 2>/dev/null `
	                do
	                count=`expr $count + 1`
	                echo ".\c"
	                done
	                ulimit $LIMIT
			find $node -print >/tmp/hld$$
			paste /tmp/src$$ /tmp/hld$$ >/tmp/dst$$
			echo "Comparing tape records!\n\n"
			sh /tmp/dst$$
			echo "\nRemove the directory containing the suspect tape image? (y or n) [n]  \c"
			read answ
			case $answ in
				"y")	rm -fr $node
					echo "Removal of $node complete!\n"
					;;
				  *)	;;
			esac
		else
			echo "Directory $name does not exist!\n"
		fi
                ;;
        IN)
		echo "\nPlace tape to be copied in the $tp1 drive! Hit RETURN when ready! \c"
		read junk
		< $rewind
		< $rewind
		echo "\n\n\nEnter directory to be used for temporary storage -   \c"
		read name
		node="$name"
                if test -d $name
                then
                        echo "\nDirectory $name already exists! Continue ??? (y or n) \c"
                        read answer
                        if test $answer = "n"
                        then
                                exit
                        fi
                else
                        if `mkdir $name`
                        then
                                echo "\nMaking $name directory"
                        else
                                exit
                        fi
                fi
                LIMIT=`ulimit`
                ulimit 409600
                echo "\nCopying records from $tp1 to $name"
                echo "\n       The copy is now in progress, Be patient!!!\n"
                while `dd if=$no_rewind of=$node/$count bs=16k 2>/dev/null `
                do
                count=`expr $count + 1`
                echo ".\c"
                done
                ulimit $LIMIT
                ;;
        OUT)
		echo "\nPlace tape to be written in the $tp1 drive! Hit RETURN when ready! \c"
		read junk
		< $rewind
		< $rewind
		echo "\n\n\nEnter directory pathname where records are saved!  \c"
		read name
		node="$name"
                if test -d $name
                then
                        stop=`ls $node |sort -n |tail -1`
                        echo "\nWriting $stop records to $tp1"
                        echo "\n                 Be patient!!!\n"
                        while test $stop -gt $count
                        do
                        if `dd if=$node/$count of=$no_rewind bs=16k 2>/dev/null`
			then 
			:
			else
			echo "Error on write! Make sure that the tape is not write protected!!!\n"
			break
			fi
                        echo ".\c"
                        count=`expr $count + 1`
                        done
                else
                        echo "Directory $name does not exist!"
                fi
                ;;
        *)
                echo Usage: copy IN - reads load tape into a given directory
                echo "       copy OUT - generates load tape from a given directory"
                echo "       copy READ - reads a tape into the bit bucket"
                exit
                ;;
esac
< $rewind
rm $rewind
rm $no_rewind
if test -r /tmp/junk$$
then
if [ `grep error /tmp/junk$$ >/dev/null` ]
then
echo "\n\nCopy $1 Failed!!!\n"
count=`grep -v error /tmp/junk$$ |wc -l`
c