#!/bin/csh
#
#	Tag
#
#	Use:
#
#		Tag [-Verbose] -Tag <tag>
#
#	Description:
#
#	All files from the CVS respository in each package hierarchy have
#	their checked-in revision tagged. The tag format is:
#
#	<tag>-<version>
#
#	where <tag> is the user specified -tag and <version> is the version
#	number obtained from the VERSION file with all '.' characters
#	replaced with '_' characters.
#
#	If -verbose is specified all files tagged will be listed, otherwise
#	only the total number of files tagged in each package will be listed.
#
#	A file can not be given the same tag twice. Thus Running Tag again
#	without a change to the VERSION number or -tag name will only affect
#	files that have been added and committed to the CVS repository since
#	the previous Tag.
#
#	-Help
#
#	List the brief usage description.
#
#	Author:
#
#		Bradford Castalia, UA/PIRL
#
#	CVS ID: Tag,v 2.1 2011/08/02 00:32:20 castalia Exp


set procedure = `basename $0`

while ($#argv)
	switch ($argv[1])
		case -[Tt]*:
			shift
			if ($?Tag) then
				echo "${procedure}: Multiple tag names specified -"
				echo "  $Tag"
				echo "  $argv[1]"
				goto Usage
			endif
			if (! $#argv) then
				echo "${procedure}: Missing tag name."
				goto Usage
			endif
			set Tag = $argv[1]
			breaksw
		case -[Vv]*:
			set Verbose = 1
			breaksw
		case -[Hh]*:
			Usage:
			echo "Usage: $0 [-Verbose] -Tag <tag>"
			echo '  Tag format: <tag>-<VERSION>'
			exit 1
			breaksw
		case -*:
			echo "${procedure}: Unrecognized $argv[1] option."
			goto Usage
			breaksw
	endsw
	shift
end

if (! $?Tag) then
	echo "${procedure}: No tag specified."
	goto Usage
endif


#	Check for completness.

set version = (`grep '^[0-9]' VERSION`)
set version = `echo $version[1] | tr . _`
if ("$version" == "") \
	set Missing_VERSION = 1

set Directory = ".."
set Packages = \
	( \
	Build \
	Conductor \
	Configuration \
	Database \
	Image_Tools \
	Messenger \
	PVL \
	Strings \
	TreeTable \
	Utilities \
	Viewers \
	)

set Missing_package = ()
set Missing_CVS = ()
foreach package ($Packages)
	if (! -d $Directory/$package) then
		set Missing_package = ($Missing_package $package)
	else
		if (! -d $Directory/$package/CVS) \
			set Missing_CVS = ($Missing_CVS $package)
	endif
end
if ($?Missing_VERSION) then
	set Incomplete
	echo "${procedure}: Missing VERSION"
	echo
endif
if ($#Missing_package) then
	set Incomplete
	echo "${procedure}: Missing packages -"
	foreach package ($Missing_package)
		echo "  $package"
	end
	echo
endif
if ($#Missing_CVS) then
	set Incomplete
	echo "${procedure}: Missing package CVS subdirectories -"
	foreach package ($Missing_CVS)
		echo "  $package"
	end
	echo
endif
if ($?Incomplete) then
	echo "$procedure aborted."
	exit 2
endif


#	Tag each package.

set CWD = $PWD
set version = $Tag-$version
echo $version

@ total = 0

foreach package ($Packages)
	echo "==> $package"
	cd $Directory/$package
	if ($?Verbose) then
		cvs -q tag $version
		set exit_status = $status
		echo
	else
		set tagged = `cvs -q tag $version`
		set exit_status = $status
	endif
	if ($exit_status) then
		echo '\!\!\!' "There was a problem tagging the $package package."
		echo "    cvs exit status $exit_status"
		cd $CWD
		exit $exit_status
	endif
	if (! $?Verbose) then
		@ count = $#tagged / 2
		@ total += $count
		echo "  $count files tagged"
	endif
	cd $CWD
end

if (! $?Verbose) then
	echo "  $total total files tagged"
endif

exit 0
