:
#	@(#) restore.sh 1.5 89/08/25 
#
#
#	      UNIX is a registered trademark of AT&T
#		Portions Copyright 1976-1989 AT&T
#	Portions Copyright 1980-1989 Microsoft Corporation
#    Portions Copyright 1983-1989 The Santa Cruz Operation, Inc
#		      All Rights Reserved

:
#	Copyright (c) 1984, 1986, 1987, 1988 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

# PURPOSE: Restore ${CPIO} files from floppy disk
# ---------------------------------------------------------------------
#	Options:
#	c - complete restore
#	d - device; defaults to /dev/rfd096ds15
#	t - tape device being used
#	o - overwrite files
#	i - index file

USAGE="$0 [ -d <device> ] [ -c ] [ -i ] [ -o ] [ -t ] [pattern [pattern] ...]"
MEDIA="floppy disk"
CPIO=cpio
CPIOFLGS="Bcik"
DEV=""
PATTERNS=
SFLAG=

trap "cd /; rm -f /tmp/msg$$ /tmp/R.chk$$ /tmp/cplst$$ /tmp/err$$ /tmp/cpcmd$$;\
echo \"You have canceled the restore.\"; exit 1 " 1 2 3 9 15
type=c
while getopts d:itcos c
do
	case $c in
	c)	type=c
		;;
	d)	DEV="$OPTARG"
		;;
	t)	MEDIA="cartridge tape"
		CPIOFLGS="$CPIOFLGS -C 10240 "
		;;
	o)	CPIOFLGS="u$CPIOFLGS"
		;;
	i)	type=i
		;;
	s)	SFLAG=s
		;;
	\?)	echo "$0: $USAGE" >&2
		exit 0
		;;
	*)	echo "$0: $USAGE" >&2
		exit 1
		;;
	esac
done
shift `expr $OPTIND - 1`

while [ -n "$1" ]
do
	PATTERNS="\"$1\" $PATTERNS"
	shift
done

if [ "$MEDIA" = "cartridge tape" -a ! "$DEV" ] 
then
	echo "$0: $USAGE" >&2
	echo "Must use -d with -t option."
	exit 1
fi

if [ ! "$DEV" ]
then
	while true
	do
		echo "Press ENTER to use default : Floppy Drive 0 (/dev/rfd0)."
		echo "Enter valid floppy drive number if different."
		echo "> \c"
		
		read ans
		case $ans in
		""|0)
			FDRIVE=/dev/rfd0
			break
			;;
		1)
			FDRIVE=/dev/rfd1
			[ -w $FDRIVE ] && break
			;;
		*)
			FDRIVE=/dev/rfd${ans}
			[ -w $FDRIVE ] && break
			;;
		esac
	done

	DEV="$FDRIVE"96ds15
fi

MESSAGE="You may remove the $MEDIA. To exit, strike 'q' followed by ENTER.
To continue, insert $MEDIA number %d and strike the ENTER key. "

case $type in
c )
	if [ "$SFLAG" = s ]
	then
		message -u "Be sure the first ${MEDIA} is inserted." >&2
	else
		message -u "Insert ${MEDIA} to restore from." >&2
	fi
	echo "            Restore in progress\n\n" >&2

	trap '' 1 2 3 9 15
	eval $CPIO -dmv${CPIOFLGS} -M \"'$MESSAGE'\" -I "$DEV" $PATTERNS 2>&1 >/tmp/R.chk$$ 
	err=$?
	rm -f /tmp/cpcmd$$ /tmp/flp_index
	trap 1 2 3 9 15
	if [ "$err" -ne 0 ]
	then
		echo "You have canceled the restore" >&2
		exit 1
	else
		NOMATCH=
		for i in $PATTERNS
		do
			A=`echo "$i" | sed 's;\";;g
				s;\.;\\\.;g
				s;\*;\.\*;g'`
			NUMERR=`cat /tmp/R.chk$$ | sed -n "\;$A;p" 2>/dev/null | wc -l`
			if [ "$NUMERR" -eq 0 ] 
			then
				NOMATCH="$NOMATCH\n\t$i"
			fi
		done
		if [ -n "$NOMATCH" ]
		then
			echo "The following files and directories were not found.\n${NOMATCH}\n\n" | pg # >&2
		fi
		rm -f /tmp/rlist$$ /tmp/R.chk$$ /tmp/err$$ /tmp/msg$$
		message -d "The restore is now finished." >&2
	fi
	exit
	;;
i)	
	message -c "Insert the $MEDIA." >&2
	if [ "$?" -ne 0 ]
	then
		echo "You have canceled the restore" >&2
		exit 1
	fi
	message -d "Reading ${MEDIA}.\nPlease do not remove." >&2
	trap '' 1 2 3 9 15
	xtract "${CPIOFLGS}" /tmp/flp_index  $DEV > /tmp/cplst$$
	cat /tmp/flp_index | sed '/^BLOCK/d' 
	rm -f /tmp/R.chk$$ /tmp/cplst$$ /tmp/flp_index
	;;
*)	echo "Invalid type $type" >&2
	echo "$0: $USAGE" >&2
	exit 1
	;;
esac
