#menu# store files and directories of files onto tape
#help# 
#help#	Store copies files from the built-in disk to tape and allows
#help#	you to optionally verify that they worked and to optionally remove them
#help#	when done.  Typically, these would be files that you want to archive or
#help#	restrict access to.  You can store single files and directories of
#help#	files.  Files placed on the tape may be retrieved with cpio -i.

dir=${0}.d
if [ -d ${dir} ]
then
	patterns=`ls ${dir} 2>/dev/null`
else
	patterns=
fi
ncpiocmd=ncpio
ncpioargs=

ddrive=`selectdevice -c $$ /dev/rmt ${patterns}`
rndrive=`basename ${ddrive}`
case "${rndrive}" in
Q* | ctape* | m* ) ndrive="Cartridge Tape"
	;;
h0* | h4* ) ndrive="Half inch Tape"
	;;
hc* ) ndrive="High Capacity Tape"
	;;
esac

if [ -n "${patterns}" ]
then
	. ${dir}/`selpattern ${ddrive}`
fi

verbose=-V
chkflags="-f -qq -k$$"

trap 'exit 0' 1 2  15

umask 022
trap '	trap "" 1 2
	rm -f /tmp/$$store*' 0

unset morehelp moreitems morenumbers

while true
do
	case `checklist ${chkflags} -e -H'
	Option 1 selects one file to be copied to tape.
	Option 2 selects the specified directory and all files and directories
		below it.'"${morehelp}"'
	You MUST use full path names when naming files and directories to be
	stored.  After the copies have been made, you will be given the option
	of REMOVING them all from the built-in disk.' \
	"
	1. Select a single file for storing.
	2. Select all files under a directory for storing.${moreitems}
Enter a number [?, q]:" 1 2 ${morenumbers}` in
	1 )	# single file
		sfile=`checkre ${chkflags} -e \
			'Enter full path name of file to be stored [q]:
	' \
	'.' ' ' \
	'^[^ ;	][^ ;	]*$' 'Blanks or tabs not permitted in the path name.' \
	'^/' 'A full path name must begin with "/".' \
	'[^/]$' 'No trailing "/" may be in the path name.'`

		if [ ! -f ${sfile} ]
		then
			echo "	Cannot find a file '${sfile}'."
			if [ -d ${sfile} ]
			then
				echo "	That is a directory."
			fi
			continue
		fi
		echo ${sfile} >>/tmp/$$store1
		;;
	2 )	# all files in a directory
		sfile=`checkre ${chkflags} -e \
			'Enter full path name of directory to be stored [q]:
	' \
	'.' ' ' \
	'^[^ ;	][^ ;	]*$' 'Blanks or tabs not permitted in the path name.' \
	'^/' 'A full path name must begin with "/".' \
	'[^/]$' 'No trailing "/" may be in the path name.'`

		if [ ! -d ${sfile} ]
		then
			echo "	Cannot find a directory '${sfile}'."
			if [ -f ${sfile} ]
			then
				echo "	That is a file."
			fi
			continue
		fi
		find ${sfile} -print  |  sort >>/tmp/$$store1
		;;
	3 )
		echo \\n `wc -l </tmp/$$store1` files selected so far.\\n
		cat /tmp/$$store1
		;;
	4 )
		echo \\n `wc -l </tmp/$$store1` files selected.\\n
		break
	esac
	if [ -z "${morenumbers}" ]
	then
		morenumbers='3 4'
		moreitems='
	3. List files selected so far.
	4. Store selected files.'
		morehelp='
	Option 3 shows the list of files you have been selecting.
	Option 4 performs the actual copying of the files to the tape.'
	fi
done

if  checkyn ${chkflags} -H'
	If you answer "yes", the name of each file will be printed as it
	is being copied to the tape.  This could be useful should you need
	to find a particular file later.
	If you answer "no", then only a dot, ".", will be printed for each file
	copied.' \
	'Print each file name as it is being stored?'
then
	verbose=-v
fi

msg=`date +"
Before inserting the next part into the ${ndrive}, mark it:

	Files stored on:
	%a. %D, %r
		part %%d
 "`

trap '	trap "" 1 2
	rm -f /tmp/$$store*
	echo You may now remove the tape.' 0

#while true
#do
	# tell user to label medium
	echo "${msg}"  |  sed '	s/the next /the first /
				s/%d/1/'
	# tell user to insert medium, then read internal disk label
#	l=`disklabel -n "${ndrive}" $$ ${ddrive}`

	# assign disk label to fsname variable
#	eval `labelfsname "${l}"`

	# alert user if medium is a file system since we will write it
#	if [ -n "${fsname}"  -a  -d "/${fsname}" ]
#	then
#		if  checkyn ${chkflags} "
#This is file system '${fsname}', labeled '${label}'.
#Do you wish to overwrite it?"
#		then
#			break
#		fi
#		echo '	Get another.'
#	else
#		break
#	fi
#done
echo Put the tape in the $ndrive drive.  Press RETURN  when it is ready.
read xx

msg="Remove the tape.
${msg}
Insert the next tape into the ${ndrive}.
Press <RETURN> when ready. [q] "

# now write the files out to the tape
if  ${ncpiocmd} ${ncpioargs} -oac -M"${msg}" ${verbose} -O${ddrive} \
	</tmp/$$store1 
then
	echo 'Store complete.'
else
	echo "	There was some problem while trying to store the files.
	See your manuals for help."
	exit 1
fi

# after copying files, verify successful copy
if  checkyn ${chkflags} -H'
	If you answer "yes", all the files just stored will be checked
	to confirm that they were successfully copied.
	This means that you will have to re-insert all parts starting with
	"part 1".' \
	-f 'Do you want to verify that your file(s) were stored properly?'
then
	echo 'PLEASE NOTE:
To verify that the store worked properly, you must re-insert
all parts that were just written to, starting with "part 1".
'
	while true
	do
		# tell user to insert part
echo Put the first tape in the $ndrive drive.  Press RETURN  when it is ready.
read xx
	#	l=`disklabel $$ ${ddrive}`

		# read disk to ensure that it is a cpio file
		if  dd if=${ddrive} of=/tmp/$$store count=2 2>/dev/null
		then
			if  file /tmp/$$store  |
				grep 'ASCII cpio archive' >/dev/null
			then
				break
			else
				echo '	This is not a "store" tape or it is 
	not the first "store" part in a series.  Try again.'
			fi
		else
			echo '	Unable to read.  Reinsert and try again.'
		fi
	done

	# generate table of contents and
	# compare original list of files with table of contents
	if  ${ncpiocmd} ${ncpioargs} -ict -I${ddrive} >/tmp/$$store2  &&
	    diff -b /tmp/$$store1 /tmp/$$store2 
	then
		echo 'Verification complete.'
	else
		echo '	Store did not work.  Could not verify files.
	The tape may be bad or there is some other problem.
	Get another and try again.'
		exit 1
	fi
fi
if  checkyn ${chkflags} -H'
	If you answer "yes", the files you specified above will be
	REMOVED from the disk.  Your ONLY copy of these files will
	be the copy that has just been stored.' \
	-f 'Should the stored files be removed from the disk?'
then
	sort -r </tmp/$$store1  |  xargs rm -rf
	echo 'Removal complete.'
fi
