#
#       Script to install paws source files into RCS.
#       Sets up strict locking and proper access lists.
#       Also makes sure the file has the proper owner
#       and group.
#
#       09/88 added version logging.  The log message is entered
#       with an editor and then passed to both ci during check in
#       and a version log file.
#
#       NOTE: newci never checked for proper owner and group.
#


versionfile=/src/paws/adm/newciupdates  # this should be identical to allnewrev
flagfile=/src/paws/src/turninprogress   # this should be identical to rcs2pws
					# and allnewrev.  

fixfile=/tmp/$0fix.$$
logfile=/tmp/$0log.$$
cioutfile=/tmp/$0out.$$

Usage="Usage: $0 file \n"

trap "echo trap encountered.;rm -f /tmp/$0*;exit 1" 1 2 3 6 7 8 10 12 13 15 19

if [ $# -lt 1 ]
then
	echo $Usage
	exit 1
fi

today=`date`

f=$1

#
# make sure the file $f exists and is not empty.
#
if [ ! -f $f -o ! -s $f ]
then
	echo $0 error: $f either does not exist or is empty.
	echo $0 aborting.
	exit 1
fi

#
# if this file exists under RCS,
# make sure the file is locked in RCS and this user can check it in!
#
if ls RCS | grep $f > /dev/null
then
	if rlog -L -l $f | grep "locked by:" | grep $LOGNAME > /dev/null
	then
		: #file is checked out locked by logging person.
	else
		echo $f is either not locked or not locked by $LOGNAME
		echo $0 aborting.
		exit 1
	fi
else
	echo RCS/$f,v does not exist.
	junk='a'
	while [ $junk != 'y' -a $junk != 'n' ]
	do
		echo "Is $f a new RCS file? (y/n). \c"
		read junk
	done
	if [ $junk = 'n' ]
	then
		echo $0 aborting.
		exit 1
	fi
fi



#
# DEW 12/14/88
#
# if this file is located in /src/paws/src/...
# then normalize, get a log message, and store.
# otherwise, just ci as normal.  This extension
# is added so that newci can be used all of the
# time.
#
if echo $PWD | grep "/src/paws/src" > /dev/null 2>&1 
then
	#
	# DEW 01/04/89
	#
	# if the flag file exists, then can not check files
	# in.
	#
	if [ -f $flagfile ]
	then
		echo "ERROR:  System Turn in progress."
		echo "Files under /src/paws/src can not be modified."
		echo "See the turn master."
		echo $0 aborting.
		exit 1
	fi

	#
	# update all tabs to spaces,
	# get rid of trailing tabs, spaces,
	# get rid of empty lines at then end of the file.
	#
	# this creates a standard format and makes diffs and srmcps alot
	# easier.
	#
	expand $f >$fixfile
	mv $fixfile $f
	normalize $f

	#
	# compare users file with RCS file, confirming
	# user wants to check in.
	#
	echo "Differences between $f and RCS/$f"
	echo "< indicates line out.\n> indicates line in."
	rcsdiff $f | more
	echo
	junk='a'
	while [ $junk != 'y' -a $junk != 'n' ]
	do
		echo "Do you want to check this file in? (y/n) \c"
		read junk
	done
	if [ $junk = 'n' ]
	then
		echo $0 aborting.
		exit 1
	fi


	#
	# get the log message and store in logfile.
	#
	echo Enter Log Message.
	echo "Editor: (return for vi) \c"
	read editname
	if [ "$editname" = "" ]
	then
		editname=vi
	fi
	done=0
	rm -f $logfile
	while [ $done -ne 1 ]
	do
		$editname $logfile
		if [ ! -s $logfile ]
		then
			echo You must make a log entry!
			echo Press return to continue.
			read junk
		else
			done=1
		fi
	done

	#
	# check the file in, using the data in $logfile.
	# if ci is o.k., log $logfile and other info to $versionfile
	#
	if cat $logfile | ci $f >$cioutfile 2>&1
	then
		if grep abort $cioutfile > /dev/null 2>&1
		then
			cihaserred=1
		else
			cihaserred=0
		fi
	else
		cihaserred=1
	fi
	if [ $cihaserred -eq 1 ]
	then
		#
		# display cioutput
		# generate internal error statement.
		#
		cat $cioutfile
		echo "$0 error: ci execution failed.  $versionfile NOT updated."
		echo $0 aborting.
		rm $cioutfile $logfile
		exit 1
	else
		#
		# display ci output.
		# it is saved for later use.
		#
		cat $cioutfile

		echo updating your log entry into $versionfile


		#
		# get the full file name.
		#
		fullname=`dirname $f`
		if [ $fullname = . ]
		then
			fullname=$PWD
		fi
		fullname=$fullname/`basename $f`

		#
		# get the revision.
		#
		rev=`cat $cioutfile | grep "new revision" | cut -d\  -f3 -s`

		#
		# update the log file.
		#
		echo "\n-------------------------\nFILE: $fullname; REV: $rev\nUSER: $LOGNAME;  DATE: $today;\n" >> $versionfile
		cat $logfile >> $versionfile
		rm $cioutfile $logfile
	fi
else
	ci $f
fi

