#!/bin/sh
#	This file is part of the Concurrent Versions System CVS.
#	Written by Dick Grune, Vrije Universiteit, Amsterdam.
#	$Header: RV,v 3.1 89/09/25 16:34:36 dick Exp $

#
#		R e s t o r e   V e r s i o n
#	RV restores a version of the configuration, given the repository name
#	and a configuration identifier.  The call is
#		RV <repository-name> <configuration-identifier>
#	The files will be reconstructed with the correct revision number,
#	even if they have been removed by RM and CM in the meantime.
#
#	For backward compatibility, RV will still be able to work from
#	SV-records, as created by the late program SV. The SV-record will be
#	read from standard input by the call
#		RV <repository-name> -
Name=RV; export Name

# CVSBIN, CVSLIB and RCSBIN directories
CVSBIN=/home/top/dick/cvs
CVSLIB=/home/top/dick/cvs
RCSBIN=${RCSBIN-/usr/local/bin}
export CVSBIN CVSLIB RCSBIN

# avoid spurious identifications
PATH=${CVSPATH-/bin:/usr/bin}; export PATH

# is the number of parameters correct?
case $# in
2)	# OK
	;;
*)
	echo Call is: $Name repository-name configuration-identifier >&2
	exit 1
	;;
esac

Repository=$1
ConfIdf=$2				# configuration identifier;could be a -

# check the existence of the repository
. $CVSLIB/NR.aux

# set up names in Admin
Admin=$Repository/Admin
Cfr=CVS.confrec
RcsCfr=$Admin/$Cfr,v

case $ConfIdf in
-)	# obsolete case of a SV record
	# create the administration directory
	. $CVSLIB/CA.aux		# uses $Repository

	# get the CVS.adm/Entries file from standard input
	cat - >CVS.adm/Entries
	;;

*)	# $ConfIdf is a real configuration identifier
	if	[ -f $RcsCfr ]
	then
		# create the administration directory
		. $CVSLIB/CA.aux	# uses $Repository

		# get the configuration record of the denoted configuration
		if	$RCSBIN/co -p -q -r$ConfIdf $RcsCfr >CVS.adm/Entries
		then :
		else
			echo $Name: there is no configuration $ConfIdf >&2
			exit 1
		fi
	else
		echo $Name: no configuration history available \
			for $Repository >&2
		exit 1
	fi
esac

# construct list of files to check out
OLIST=` <CVS.adm/Entries sed 's/.* \(.*\)|/\1/' `

# see if any is already present
OK=yes
for User in $OLIST
do
	if	# there is a file $User already
		[ -f $User ]
	then
		echo $Name on $Repository: $User already exists >&2
		OK=no
	fi
done

case $OK in
no)
	echo $Name failed\; correct above errors first >&2
	exit 1
	;;
esac

# check out all files in $OLIST
for User in $OLIST
do
	. $CVSLIB/LR.aux	# sets $Rcs to $Repository/$User,v or /Attic/
	. $CVSLIB/VT.aux	# sets $VN_User, $VN_Rcs, $TS_User, $TS_Rcs
	
	# how is the RCS file?
	case $VN_Rcs in
	"")
		# there is no RCS file
		
		echo $Name: cannot find $Rcs >&2
		OK=no
		continue
		;;
	*)
		# there is an RCS file
		
		if	# check out correct version of $Rcs into $User
			$RCSBIN/co -r$VN_User $Rcs $User
		then	# adjust $User
			chmod +w $User
			# make a reference with the NEW time stamp
			# and the OLD version number
			. $CVSLIB/VT.aux
			$CVSLIB/RG.aux $User $VN_User "$TS_User"
		else
			echo $Name on $Repository: could not check out \
				$User >&2
			OK=no
		fi
		;;
	esac
done

$CVSLIB/EF.aux				# update CVS.adm/Files

# did we succeed?
case $OK in
no)
	exit 1
	;;
esac

exit 0
