#!/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.
#
# MODULE & VERSION : $Id: checkin.sh,v 1.8 1993/12/14 13:37:21 hernan Exp $
# DATE             : $Date: 1993/12/14 13:37:21 $
# TIME             : 
#
#	checkin [-?|-h|-k|-u|-c "comment"] files
#
# Version for the SCCS Source Code Control System
#
COMMENTS=""
SCCSDIR=SCCS
DELTADIR=""
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 [ -f "$SCCSDIR/s.$FILE" ]
	then
		EXISTS=1
		DELTADIR=$SCCSDIR
	elif [ -f "s.$FILE" ]
	then
		EXISTS=1
		DELTADIR=.
	else
		if [ \! -d $SCCSDIR ]
		then
			mkdir $SCCSDIR
			if [ $? -ne 0 ]
			then
				RET=2
				continue
			fi
		fi
		DELTADIR=$SCCSDIR
	fi
	if [ -n "$UNDO" ]
	then
		if [ -w "$FILE" ]
		then
			echo "File $FILE is writeable" 1>&2
			RET=6
			continue
		else
			if [ -f "$DELTADIR/p.$FILE" ]
			then
				echo "p.$FILE exists" 1>&2
				RET=7
				continue
			else
				rm -f $FILE
				get -p -k $DELTADIR/s.$FILE >$TMP1 2>$TMP2
				rmdel -r`grep -v lines $TMP2` $DELTADIR/s.$FILE
				get -e $DELTADIR/s.$FILE
				mv $TMP1 $FILE
				rm -f $TMP2
			fi
		fi
	else
		if [ -n "$EXISTS" ]
		then
			if [ -n "$COMMENTS" ]
			then
				delta -y"$COMMENTS" $DELTADIR/s.$FILE
			else
				delta $DELTADIR/s.$FILE
			fi
			if [ $? -ne 0 ]
			then
				RET=3
				continue
			fi
		else
			if [ -n "$COMMENTS" ]
			then
				admin -y"$COMMENTS" -i$FILE $DELTADIR/s.$FILE
			else
				admin -i$FILE $DELTADIR/s.$FILE
			fi
			if [ $? -ne 0 ]
			then
				RET=4
				continue
			fi
			rm -f $FILE
			if [ $? -ne 0 ]
			then
				RET=5
				continue
			fi
		fi
		if [ -n "$KEEP" ]
		then
			get $DELTADIR/s.$FILE 2>/dev/null
		fi
	fi
done
exit $RET
