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

#
#		N e w   R e l e a s e
#	NR <number> <repository-name> will raise the version number of all
#	files in the repository to <number>.1  The <number> must be higher
#	than the highest release number in the repository.
#
Name=NR; 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

# check parameters
case $# in
2)	# OK
	;;
*)
	echo Call is: $Name release-number repository >&2
	exit 1
	;;
esac

# check the existence of the repository
Repository="$2"
. $CVSLIB/NR.aux

# is the repository properly locked?
LCK=$Repository/\#cvs.lock		# the lock
LFL=$Repository/\#cvs.lfl		# the general lock flag

if	# there is a lock and it comes from LCK
	[ -d $LCK -a -f $LFL ]
then	:
else
	echo $Name: repository $Repository was not locked by LCK on >&2
	exit 1
fi

NewNumber=$1
case "$1" in
[0-9]*)	# OK
	;;
*)
	echo $Name on $Repository: release-number $NewNumber \
		is not numerical >&2
	;;
esac

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

# get the old release number (also insures the presence of $Repository/Admin)
OldNumber=`$CVSBIN/HR $Repository`

if	[ $NewNumber -le $OldNumber ]
then	# release number can only go up
	echo $Name on $Repository: new release number $NewNumber \
		not larger than old release number $OldNumber >&2
	exit 1
fi

Message="Release number raised to $NewNumber"

# make an empty scratch directory to check out the files
NRDir=$Admin/NRDir
rm -rf $NRDir				# remove left-overs
if	# we can make such a directory
	mkdir $NRDir
then	:
else
	echo $Name: could not make scratch directory $NRDir >&2
	exit 1
fi

# set trap to remove scratch directory on interrupt and exit
trap 'rm -rf $NRDir; exit 1' 1 2 3 15
trap 'rm -rf $NRDir; exit 0' 0

# clear the configuration record
cp /dev/null $UsrCfr

# get all files in the repository
(cd $Repository; ls *,v .*,v 2>/dev/null | grep -v '\*') |
while	read File
do
	Rcs=$Repository/$File
	Working=$NRDir/`basename $File ,v`

	if	# check out the RCS file into the scratch directory
		$RCSBIN/co -l -q $Rcs $Working 2>&1
	then	:
	else
		echo $Name: could not check out $Working >&2
		exit 1
	fi

	if	# check in the user file with new release number
		$RCSBIN/ci -r$NewNumber.1 -m"$Message" -f $Rcs $Working 2>&1
	then	:
	else	# something is very wrong
		echo $Name: could not check in $Working >&2
		rm $Working
		if	# unlock $Rcs
			$RCSBIN/rcs -u $Rcs
		then	:
		else
			echo $Name: could not UNlock $Rcs >&2
		fi
		exit 1
	fi

	# add to configuration record
	echo "$NewNumber| $File|" >>$UsrCfr
done

# check in the configuration record, to establish the new situation

ACT=					# no -n option to NR
Revision=-r$NewNumber.1
. $CVSLIB/CC.aux			# commit configuration record

# update $Highest
echo $NewNumber >$Highest

exit 0

