# Backup and Restore Menu
#
# Created:	 05-Sep-87
# Modified Date: 09-Nov-87
#
# Created By:	 Sandy Marshall
# Modified By:	 Sandy Marshall
#

# Edit DEFAULT as needed:
DEFAULT="q"

# Edit TITLE as needed:
TITLE="			  Backup/Restore Menu"

if [ "`echo -n`" = "-n" ]
then
	NLF1=""
	NLF2="\c"
else
	NLF1="-n"
	NLF2=""
fi

while true
do

t=`pwd`
s=`date '+%a %h %d, 19%y'`
clear=`tput clear`

echo $clear

# SHOW ==================================================

cat << TBLOCK
$TITLE

TBLOCK
echo "Present Directory: $t		Date: $s \n"
cat << SHOWEND
Tape					Floppy

a. Backup File and/or Directory		d. Backup File and/or Directory
b. Restore Files and/or Directory	e. Restore Files and/or Directory
c. List Tape				f. List Floppy
					g. Format a Floppy for Backup

			  Miscellaneous

			  h. Change Directory
		
		q. Return to previous menu	? Help

SHOWEND
#
# Add additional lines before 'SHOWEND' and
# add corresponding entries in 'SELECT' below:
# <tab><letter(s)><tab><description"	# format for additional lines
#
# Use blank lines for visual grouping.
#

# ENTER ==================================================

echo $NLF1 "

			       Select [$DEFAULT] :  $NLF2"
read SELECT

if [ -z "$SELECT" ]
then
	SELECT=$DEFAULT
fi

DEFAULT=$SELECT
RT="0"

echo $clear

# SELECT ==================================================

case $SELECT in
	a)	DEVICE=/dev/tape
		echo "		Backup Files and/or Directories\n"
		echo "Enter filenames and/or directory names to be saved to tape."
		echo "Names may be entered on one line separated by spaces,"
		echo "or entered on separate lines. A blank line will terminate"
		echo "the list. Then the files will be copied to tape.\n"

		LIST=""
		set -f
		while :
		do
			echo "Enter names: \c"
			read NAME
			if [ -z "$NAME" ]
			then	break
			fi
			LIST="$LIST $NAME"
		done

		if [ "$LIST" = "" ]
		then	echo "\nNo files have been requested. Exiting program."
			EXERR=0
			RT=1
		else	mt rewind
			if find $LIST -print | cpio -ov | wbuf $DEVICE
			then	echo "\nBackup done, rewinding tape."
			else	echo ""
				echo "\nAn error has occurred while copying the requested"
				echo "files to tape. The backup on the tape may not be good."
				echo "Please either try the backup again, or verify"
				echo "that the tape is good by running the \"List Tape\" menu option."
			fi
			set +f
			mt rewind
			EXERR=$?
		fi						;;

	b)	DEVICE=/dev/tape

		echo "		Restore Files and/or Directories\n"

		echo "Enter list of filenames and/or directories to be restored from tape."
		echo "Names may be entered on one line separated by spaces,"
		echo "or entered on separate lines. A blank line will terminate"
		echo "the list. Then the files will be copied from tape.\n"

		set -f
		LIST=""
		while :
		do
			echo "Enter names: \c"
			read NAME
			if [ "$NAME" = "" ]
			then	break
			fi
			LIST="$LIST $NAME"
		done

		if [ "$LIST" = "" ]
		then	echo "\nNo files have been requested. Exiting program."
			EXERR=0
			RT=1
		else	mt rewind
			if rbuf $DEVICE | cpio -ivduma $LIST
			then	echo "\nRestore done, rewinding tape."
			else	echo ""
				echo "\nAn error has occurred while copying the requested"
				echo "files from tape. The backup on the tape may not be good."
				echo "Please either retry the restore, or verify"
				echo "that the tape is usable by using the \"List Tape\" menu option."
			fi
			set +f
			mt rewind
			EXERR=$?
		fi						;;

	c)	echo "				List Tape\n"
		mt rewind
		rbuf /dev/tape | cpio -ivt
		mt rewind
		EXERR=$?					;;

	d)	echo "		Backup Files and/or Directories\n"
		echo "Enter filenames and/or directory names to be saved to floppy."
		echo "Names may be entered on one line separated by spaces,"
		echo "or entered on separate lines. A blank line will terminate"
		echo "the list. Then the files will be copied to floppy.\n"
		echo "Backups will only be done on 1.2MB High Density floppys!\n"

		LIST=""
		set -f
		while :
		do
			echo "Enter names: \c"
			read NAME
			if [ -z "$NAME" ]
			then	break
			fi
			LIST="$LIST $NAME"
		done

		if [ "$LIST" = "" ]
		then	echo "\nNo files have been requested. Exiting program."
			EXERR=0
			RT=1
		else	echo "\nIs floppy in drive 1 or 2: \c"
			read DRV
			if [ -n "$DRV" ]
			then	if [ "$DRV" -eq 1 -o "$DRV" -eq 2 ]
				then	echo "\nInsert floppy into the drive and type \"go\", anthing else to abort: \c"
					read ANS
					if [ "$ANS" = "go" ]
					then	if find $LIST -print | cpio -ov > /dev/rdsk/"$DRV"s33
						then	echo "\nBackup done."
							EXERR=0
							RT=1
						else	echo ""
							echo "\nAn error has occurred while copying the requested"
							echo "files to floppy. The backup on the floppy may not be good."
							echo "Please either try the backup again, or verify"
							echo "that the floppy is good by by using the \"List Floppy\" menu option."
							EXERR=0
							RT=1
						fi
					else	EXERR=0
					fi
					set +f
				else	echo "\nDrive selected does not exist"
					EXERR=0
					RT=1
				fi
			else	echo "\nNo drive selected"
				EXERR=0
				RT=1
			fi
		fi						;;

	e)	echo "		Restore Files and/or Directories\n"
		echo "Enter list of filenames and/or directories to be restored from floppy."
		echo "Names may be entered on one line separated by spaces,"
		echo "or entered on separate lines. A blank line will terminate"
		echo "the list. Then the files will be copied from floppy.\n"
		echo "Files can only be restored from 1.2MB floppys written"
		echo "using the backup function of the menu system or with"
		echo "cpio -ov command!\n"

		set -f
		LIST=""
		while :
		do
			echo "Enter names: \c"
			read NAME
			if [ "$NAME" = "" ]
			then	break
			fi
			LIST="$LIST $NAME"
		done

		if [ "$LIST" = "" ]
		then	echo "\nNo files have been requested. Exiting program."
			EXERR=0
			RT=1
		else	echo "\nIs floppy in drive 1 or 2: \c"
			read DRV
			if [ -n "$DRV" ]
			then	if [ "$DRV" -eq 1 -o "$DRV" -eq 2 ]
				then	echo "\nInsert floppy into the drive and type \"go\", anthing else to abort: \c"
					read ANS
					if [ "$ANS" = "go" ]
					then	if cpio -ivduma $LIST < /dev/rdsk/"$DRV"s33
						then	echo "\nRestore done."
							EXERR=0
							RT=1
						else	echo ""
						echo "\nAn error has occurred while copying the requested"
						echo "files from floppy. The backup on the floppy may not be good."
						echo "Please either retry the restore, or verify"
						echo "that the floppy is usable by using the \"List Floppy\" menu option."
						EXERR=0
						RT=1
						fi
					else	EXERR=0
					fi
					set +f
				else	echo "\nDrive selected does not exist"
					EXERR=0
					RT=1
				fi
			else	echo "\nNo drive selected"
				EXERR=0
				RT=1
			fi
		fi						;;

	f)	echo "				List Floppy\n"
		echo "Is floppy in drive 1 or 2: \c"
		read DRV
		if [ -n "$DRV" ]
		then	if [ "$DRV" -eq 1 -o "$DRV" -eq 2 ]
			then	echo "\nInsert floppy into the drive and type \"go\", anthing else to abort: \c"
				read ANS
				if [ "$ANS" = "go" ]
				then	cpio -ivt < /dev/rdsk/"$DRV"s33
					EXERR=0
					RT=1
				else	EXERR=0
				fi
			else	echo "\nDrive selected does not exist"
				EXERR=0
				RT=1
			fi
		else	echo "\nNo drive selected"
			EXERR=0
			RT=1
		fi						;;

	g)	echo "Only high density (1.2MB) format can be used\n\n"
		echo "Is floppy to be formatted in drive 1 or 2: \c"
		read x
		if [ -n "$x" ]
		then	if [ "$x" -eq 1 -o "$x" -eq 2 ]
			then	echo "\n\nInsert a high density floppy into drive $x"
				echo "To start format type \"go\", anything else to abort: \c"
				read y
				if [ "$y" != "go" ]
				then	echo "\n\nFormat aborted"
					EXERR=0
					RT=1
				else	echo "\n\n Formatting floppy, please wait...\n"
					format /dev/rdsk/"$x"s33
					EXERR=$?
				fi
			else	echo "Drive does not exist"
				EXERR=0
				RT=1
			fi
		else	EXERR=0
		fi							;;

	h)	echo "Current directory: \c"
		pwd
		echo "Enter new directory name: \c"
		read x
		if [ -n "$x" ]
		then	if [ -d "$x" ]
			then	cd $x
				EXERR=$?
			else	echo "\nDirectory $x does not exist"
				EXERR=0
				RT=1
			fi
		else	EXERR=0
		fi							;;

	\?)	sh /usr/bin/back.help
		EXERR=0
		RT=1							;;

	q)	exit $EXERR						;;


esac

if [ "$EXERR" != "0" ]
then
	echo $NLF1 " Exit error: $EXERR	$NLF2	"
	echo $NLF1 "  Press <RETURN> to continue:  $NLF2"
	read OK
else
	if [ "$RT" = "1" ]
	then
		echo $NLF1 " \n Press <RETURN> to continue:  $NLF2"
		read OK
	fi
fi

done
