#!/bin/sh
#
# Copyright (c) 1993 InterSoft Co.  All Rights Reserved
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF InterSoft Co.
# The copyright notice above does not evidence any
# actual or intended publication of such source code.
#
# $Id: checkin.sh,v 1.5 1993/12/14 13:39:33 hernan Exp $
#
# checkin [-?|-h|-k|-u|-c "comment"] files
#
# Version for the RCS Revision Control System
#
COMMENTS=""
KEEP=""
UNDO=""
RET=0
SAVEPWD=`pwd`
USAGE="Usage: `basename $0` [-?|-h|-k|-u|-c "comment"] files"
TMP1=/tmp/chkin1.$$
TMP2=/tmp/chkin2.$$
#trap "rm -f $TMP1 $TMP2" 0

while getopts h?kuc: OPT
do
	case $OPT in
	u)
		UNDO=1
		;;
	k)
		KEEP=1
		;;
	c)
		COMMENTS=$OPTARG
		;;
	h|\?)
		echo $USAGE 1>&2
		exit 1
		;;
	esac
done

shift `expr $OPTIND - 1`

if [ $# -eq 0 ]
then
	echo $USAGE 1>&2
	exit 1
fi

for i
do
	EXISTS=""
	DIR=`dirname $i`
	FILE=`basename $i`
	cd $SAVEPWD
	cd $DIR
	if [ $? -ne 0 ]
	then
		RET=1
		continue
	fi

	if [ -n "$UNDO" ]
	then
		# toDo: should work correctly when undoing first checkin.
		if [ -w "$FILE" ]
		then
			echo "File $FILE is writeable" 1>&2
			RET=6
			continue
		else
			rm -f $FILE
			co -p -kk $FILE >$TMP1 2>$TMP2
			rcs -o`grep revision $TMP2 | awk '{print $2}'` $FILE
			co -l $FILE
			mv $TMP1 $FILE
			rm -f $TMP2
		fi
	else
		if [ ! -d "RCS" ]
		then
			mkdir RCS
		fi

		if [ -n "$COMMENTS" ]
		then
			# toDo: should only use -t if ,v file did not exist
			ci -m"$COMMENTS" -t"-initial rev checked into RCS without description" $FILE
		else
			ci $FILE
		fi
		if [ $? -ne 0 ]
		then
			RET=3
			continue
		fi
		if [ -n "$KEEP" ]
		then
			co $FILE 2>/dev/null
		fi
	fi
done
exit $RET
