#	ATT: ident	"sadmin:admin/menu/filemgmt/restore	2.9"

#ident	"@(#)sadmin/admin/menu/filemgmt:restore	25.1"

#menu# restore files from "backup" or "store" tapes to hard disks
#help# 
#help#	Restore files from a tape made by "backup" or "store" back onto
#help#	a file system.  You can restore individual files, directories of
#help#	files, or the entire contents of the tape.
#help#	You can restore from both "incremental" and "complete" tape.
#help#	You can also list the names of files stored on the tape.

DEVICES="9mt0 9mt1 rmt0 rmt1"
flags="-qq -k$$"
trap 'exit 0' 1 2 15
trap '	trap "" 1 2
	rm -f /tmp/$$restall' 0

rnamewarn='WARNING:
	Be very careful when you rename a file.  Files incorrectly named
	by typing errors are difficult to find and repair.
	Remember that only the first 14 characters of each part of the
	file name (i.e. the characters between the "/"s) are significant.
You will be asked to rename each file in turn.  An empty response skips that
file.  An answer of "." (period) restores the file with its original name.'

cpiocmd=cpio
cpioargs=

ddrive=`selectdevice -c $$ /dev ${DEVICES}`

cd /

while true
do
	type=`checklist ${flags} -fe -H'
	Option 1 restores only the individual files selected.
	Option 2 restores all files AND directories below the directory
	that you specify.
	Option 3 restores every file from the tape.
	Option 4 list the file names from the tape without restoring them.
	REMEMBER- If you do not rename a file, "restore" will write over any
	copies of files that are presently on the file systems.  Also, you MUST
	use a full path name when renaming files.' \
	'Select:
	1. restore single files
	2. restore directories of files
	3. restore all files
	4. list all the files
Enter a number [?, q]:' 1 2 3 4`
	rflag=
	l=`disklabel $$ ${ddrive}`
	if  dd if=${ddrive} of=/tmp/$$restall count=2 2>/dev/null
	then
		if  file /tmp/$$restall  |
			grep 'cpio archive' >/dev/null
		then
			case "${type}" in
			1 )	# single file
				sfile=`checkre ${flags} -fe -l ' ' -H '
	You may also specify the file names as cpio(1) patterns and you
	may specify more than one file name.
	If you do not find files that you expect to find, use Option 4 to
	list out all the file names on the tape.  Entering file names
	exactly as they exist on the list will always work.' \
	'Enter full path name of file(s) to be restored [?, q]:
	' \
	'.' ' ' \
	'^[^ ;	][^ ;	]*$' 'Blanks or tabs not permitted in the file name.' \
	'^/' 'A full path name must begin with "/".' \
	'[^/]$' 'No trailing "/" in the file name.' \
	'/[^/]\{1,14\}$' 'No more than 14 characters in a file name.'  |
				sed "/./s;.*;'&';"  |
				tr '\012' ' '`
				if  checkyn ${flags} -f 'Do you want to rename each file as it is copied in?'
				then
					echo "${rnamewarn}"
					rflag=r
				else
					rflag=v
				fi

				#  Magic here!  We must keep the shell from
				#  interpreting meta-characters that may be in
				#  ${sfile}.
				eval ${cpiocmd} ${cpioargs} \
					-idu${rflag} <${ddrive} "${sfile}"
				;;
			2 )	# a directory
				sfile=`checkre ${flags} -fe -l ' ' \
	'Enter full path name of directory(s) to be restored [q]:
	' \
	'.' ' ' \
	'^[^ ;	][^ ;	]*$' 'Blanks or tabs not permitted in the file name.' \
	'^/' 'A full path name must begin with "/".' \
	'[^/]$' 'No trailing "/" in the file name.' \
	'/[^/]\{1,14\}$' 'No more than 14 characters in a file name.'  |
				sed "/./s;.*;'&/*';"  |
				tr '\012' ' '`
				if  checkyn ${flags} -f 'Do you want to rename each file as it is copied in?'
				then
					echo "${rnamewarn}"
					rflag=r
				else
					rflag=v
				fi

				#  Magic here!  We must keep the shell from
				#  interpreting meta-characters that may be in
				#  ${sfile}.
				eval ${cpiocmd} ${cpioargs} \
					-idu${rflag} <${ddrive} "${sfile}"
				;;
			3 )	# entire contents
				if  checkyn ${flags} -f 'Do you want to rename the files as they are copied in?'
				then
					echo "${rnamewarn}"
					rflag=r
				else
					rflag=V
				fi
				${cpiocmd} ${cpioargs} \
					-idBmu${rflag} <${ddrive}
				;;
			4 )	# list contents
				if  checkyn -f 'Do you want to see the file status information, too?'
				then
					rflag=v
					echo '
 mode  owner   size   date of last access	  filename
------ ------  -----  --------------------  --------------------'
				fi
				${cpiocmd} ${cpioargs} \
					-idt${rflag} <${ddrive}
				echo "You may now unmount the tape from ${ddrive}."
				continue
			esac
			if [ $? = 0 ]
			then
				echo 'Restoration complete.'
			else
				echo '	Restoration did not work.
	See the Owner/Operator manual for help.'
				exit
			fi
			echo "You may now unmount the tape from ${ddrive}."
			continue
		fi
		echo 'This tape is not a "backup" tape or it is 
not the first tape in a series.  Try again.'
	else
		echo "Unable to read tape in ${ddrive}.  Remount it and try again."
	fi
done
