#!/bin/ksh

# This script takes a list of filenames (absolute path or relative path) and
# backs out the latest sccs revision level by creating a new revision equal
# to the one before the latest one.  This will allow for putbacks in a manner
# that teamware can handle.  (Teamware cannot handle putbacks after a rmdel.)
#
# Limitations:  it cannot go back to a previous major revision level.  That is,
# it cannot go from 2.1 to 1.99.  Also, if there are gaps in revision levels,
# the script will not handle it at all.
#
# THIS SAMPLE PROGRAM IS BEING PROVIDED "AS IS" AND ONLY AS A COURTESY TO
# THE RECIPIENT.  SUN MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS 
# OR IMPLIED WITH RESPECT TO THIS SAMPLE PROGRAM INCLUDING QUALITY, PERF- 
# FORMANCE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON- 
# INFRINGEMENT.  IN NO EVENT WILL SUN BE LIABLE FOR ANY DIRECT, INDIRECT,  
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR  
# INABILITY TO USE THIS SAMPLE PROGRAM.

if [ $# = 0 ] ; then
	echo "Usage:  $0 <filename>"
	exit 1
fi

while [ ! $# = 0 ] ; do
	name=$1
	dir=`dirname $name`
	base=`basename $name`
	if [ ! -d $dir ] || [ ! -f $name ] ; then
		echo "File does not exist:  $name"
	elif [ ! -f $dir/SCCS/s.$base ] ; then
		echo "File is not under sccs control:  $name"
	else
		cd $dir
		version=`sccs prs $base | head -3 | tail -1 | cut -f2 -d' '`
		echo $name $version
		level=`echo $version | cut -f1 -d'.'`
		revision=`echo $version | cut -f2 -d'.'`
		revision=`expr $revision - 1`
		if [ $revision -eq 0 ] ; then
			echo "Cannot $0 $name--no previous revision"
		else
			sccs edit $base
			if [ ! $? = 0 ] ; then
				echo "Cannot sccs edit $name"
			else
				rm $base
				echo $level $revision
				sccs get -r$level.$revision SCCS/s.$base
				sccs delget $base -y"Backed out to previous rev"
			fi
		fi
		cd -
	fi
	shift
done
