#! /bin/sh
#
# rcspurge - Remove all revisions in an RCS file except the head one.
# 
# Author: Robert E. Bruccoleri, Bristol-Myers Squibb 
#	  Pharmaceutical Research Institute.
#
#	$Id: rcspurge.sh,v 1.1 1991/09/10 14:41:16 root Exp $

usage='usage: rcspurge [-f] file...
-f means that no backup of RCS file should be made. The backup is a copy
   with ~ appended.'

case $# in
0) echo >&2 "$usage"; exit 2
esac

besafe=1

for i do
	case $1 in
	-f) besafe=0 ;;
	-*) echo >&2 "$usage"; exit 2 ;;
	esac
done

exitcode=0
for i do
	case $i in
	-f) ;;
	*)	rcsname=`rlog -h $i | grep "RCS file:" | cut -f2 -d:`
		if [ -z "$rcsname" ]
		then
			echo >&2 $i is not an RCS file.
			exitcode=1
		else
			if [ $besafe -eq 1 ] 
			then 
				cp ${rcsname} ${rcsname}.~$$
			fi
			toprev=`rlog -h ${rcsname} | grep "head: " | cut -f2 -d:`
			if [ -z "$toprev" ] 
			then
				echo >&2 Unable to get head revision for ${rcsname}.
				exit 3
			fi
			penrev=`rlog ${rcsname} | grep "^revision " | head -2 | tail -1 | tr "	" " " | cut -f2 -d\  `
			echo >&2 Head revision for ${rcsname} is ${toprev}. 
			if [ $penrev != $toprev ]
			then
				rcs -o:${penrev} ${rcsname}
			fi
		fi ;;
	esac
done

exit $exitcode
