#!/bin/sh

# rc -- automate the SCCS and RCS check-out/edit/check-in
# http://www.cs.duke.edu/~des/scripts.html
# https://kinzler.com/me/home.html#other

# @(#)  /u/des/src/rc/rc  1.21  99/07/28  16:36:41
#
# rc: revision control: automate the SCCS and RCS check-out/edit/check-in
#
# Copyright (c) 1999 by Daniel E. Singer.  All rights reserved.
# Permission is granted to reproduce and distribute this program
# with the following conditions:
#   1) This copyright notice and the author identification below
#      must be left intact in the program and in any copies.
#   2) Any modifications to the program must be clearly identified
#      in the source file.
#
# Written by Daniel E. Singer, Duke Univ. Dept of Computer Science, 9/6/95
#
# Modifications:
#
# 9/26/95, D.Singer
#	added command echo
# 4/2/96, D.Singer
#	added check for super-user
# 2/21/97, D.Singer
#	changed method of detecting echo newline suppression type;
#	added restore of file permissions;
#	added -p;
# 10/5/97, D.Singer
#	added mode preservation for editing stage;
# 1/27/98, D.Singer
#	added ability to deal with files that are not already in SCCS;
#	added ability to deal with files that do not exist at all;
# 2/98, D.Singer
#	changed name from sccss to rc;
#	added ability to deal with RCS;
#	added -s, -r, -h;
#	added 'not' and 'ask' routines;
#	lots of other little mods;
# 3/98, D.Singer
#	fixed SCCS mode preservation on create;
#	added path ability;
# 6/98, D.Singer
#	fixed some problems with RCS check;
# 2/99, D.Singer
#	added -o, -i, -c;
# 2/99, D.Singer
#	added -q;
# 6/99, D.Singer
#	changed FAVOR to RC_FAVOR; setable from env.;
#	changed EDITR to RC_EDITOR; setable from env.;
#	added RC_QUIET_CREATE_HIST and RC_QUIET_CREATE;
#	added DO_CHECK_IN, DO_EDIT, DO_CHECK_OUT, and DO_CREATE;
#	added -x;
#	messed a good bit with the main logic;
#

#PATH='/bin:/usr/bin:/sbin:/usr/sbin:/usr/ucb:/usr/ccs/bin:/usr/local/bin'
#export PATH

#
# these external commands can be adjusted here to adjust for platform
# differences; perhaps a case stmt. with `uname -sr`...
#
     AWK=awk
BASENAME=basename
   CHMOD=chmod
    LS_L='ls -ld'
     SED=sed
   UNAME='uname -sr'

#
# platform specific settings
#
SYS="`$UNAME`"      # OS type
case "$SYS" in
  "SunOS "*)
	AWK="nawk"
  esac

PROG=`$BASENAME "$0"`
DFLT_FAVOR='SCCS'	# make this your favorite; _must_ be one of RCS or SCCS;
DFLT_EDITOR='vi'

# added 2008-06 by kinzler for correct vim syntax coloring of this script
case "$DFLT_FAVOR" in
SCCS)	rdflt=; sdflt=' (default)';;
*)	sdflt=; rdflt=' (default)';;
esac

USAGE="
Usage:  $PROG [-hpqx] [-i|-o] [-r|-s] [-c \"comment\"] [path/]file

	'$PROG' automates the revision control edit cycle.

	  -h	print a help message, and exit;
	  -p	do not preserve file permissions, default is to preserve
		any read and execute permissions (SCCS only);
	  -x	set execute bits when creating a new file;
	  -c	supply a comment for the revision;
	  -q	quiet, minimal prompts and messages;

	  -i	just do a check-in of the file, and exit;
	  -o	just do a check-out of the file, and exit;

	  -r	favor RCS$rdflt
	  -s	favor SCCS$sdflt

	  path	optional directory path to file;
	  file	the file to edit;
"
SUMMARY="
$PROG:
Automate the check-out/edit/check-in cycle of files maintained under SCCS
and RCS (revision control).  Does various checks to determine which type
of revision control to use.  Will do initial entry of file to revision
control.  Will create a new file.  Will restore execute bits on SCCS files.
If a 'path' is specified, will first 'cd' to that path."

# RC_FAVOR can be set from the environment;
: ${RC_FAVOR:="$DFLT_FAVOR"}	# _must_ be one of RCS or SCCS

# RC_EDITOR can be set from the environment too;
: ${RC_EDITOR:=${VISUAL:-${EDITOR:-$DFLT_EDITOR}}}	# editor (or other
							# program) to use

# RC_QUIET_CREATE_HIST and RC_QUIET_CREATE are set from the environment;
: ${RC_QUIET_CREATE_HIST=""}	# if QUIET or no tty, create RC history for
				# FILE, if it doesn't exist;
: ${RC_QUIET_CREATE=""}		# if QUIET or no tty, create FILE, if it
				# doesn't exist;

SYNTAX="$PROG: option syntax error."

FILE=			# file name
DIR='.'			# working directory
DO_PRESERVE_MODE=1	# preserve file execute bits
GOT_MODE=		# only need to do this once
CL_RCS=			# RCS specified on the command line
CL_SCCS=		# SCCS specified on the command line
QUIET=			# quiet specified on the command line
CFLAG=			# comment specified on the command line
COMMENT=		# the specified comment
XFLAG=			# set execute on create
XMODE="+x"		# mode argument to use for XFLAG

CHECK_OUT_ONLY=		# only do a check-out
CHECK_IN_ONLY=		# only do a check-in
DO_CHECK_OUT=1		# do a file check-out
DO_EDIT=1		# do a file edit
DO_CHECK_IN=1		# do a file check-in
DO_CREATE=		# do a file create
DO_ALL=1		# do all of check-out/edit/check-in

IS_FILE=		# the file exists?
DO_NEW_FILE=		# make a brand new file?
ALREADY_CHECKED_OUT=	# file already checked out?
FILES_BEING_EDITED=	# list of files that are checked out

RCS_DIR='RCS'		# name of the RCS directory
RCS_PREFIX=''		# RCS history file prefix
RCS_SUFFIX=',v'		# RCS history file suffix
RCS_HISTORY_FILE=	# path/name of RCS history file

SCCS_DIR='SCCS'		# name of the SCCS directory
SCCS_PREFIX='s.'	# SCCS history file prefix
SCCS_SUFFIX=''		# SCCS history file suffix
SCCS_HISTORY_FILE=	# path/name of SCCS history file

RC_TYPE=		# which type of revision control (RCS or SCCS)
IS_RC_HISTORY=		# there is an RC history file
RC_CREATE=		# actual create command to use
RC_CHECK=		# actual check command to use
RC_CHECK_OUT=		# actual check-out command to use
RC_CHECK_IN=		# actual check-in command to use
RC_UNEDIT=		# actual check-in command to use

#
# actual commands for SCCS and RCS
#
    RCS_CREATE='ci -u -i'	# for RCS create
				# for RCS check
RCS_CHECK_PATS="$RCS_PREFIX*$RCS_SUFFIX $RCS_DIR/$RCS_PREFIX*$RCS_SUFFIX"
     RCS_CHECK="rlog -L -l XXX | rlog_filter"
 RCS_CHECK_OUT='co -l'		# for RCS check-out
  RCS_CHECK_IN='ci -u'		# for RCS check-in
    RCS_UNEDIT='co -u -f'	# for RCS check-in with no changes

   SCCS_CREATE='sccs create'	# for SCCS create
    SCCS_CHECK='sccs check'	# for SCCS check
SCCS_CHECK_OUT='sccs edit'	# for SCCS check-out
 SCCS_CHECK_IN='sccs delget'	# for SCCS check-in
   SCCS_UNEDIT='sccs unedit'	# for SCCS check-in with no changes


#
# get the right newline suppression for 'echo'
#
C= N=
if [ "/`echo -n`" = '/' ]; then
	N='-n ' # for BSD echo
  elif [ "/`echo '\c'`" = '/' ]; then
	C='\c'  # for SysV echo
  else
	:       # unknown
  fi


syntax_error() {
	echo "$SYNTAX" >&2
	echo "$USAGE" >&2
	exit 1
}

arg_syntax_check() {
	[ "$1" -lt 1 ] && syntax_error
}

#
# process command line options
#
while [ "$#" -gt 0 ]; do
	case "$1" in
	  # options without argument
	  -h)
		echo "$SUMMARY"
		echo "$USAGE"
		exit 0
		;;
	  -i)
		CHECK_IN_ONLY=1
		DO_CHECK_OUT=
		DO_EDIT=
		DO_ALL=
		;;
	  -o)
		CHECK_OUT_ONLY=1
		DO_EDIT=
		DO_CHECK_IN=
		DO_ALL=
		;;
	  -p)
		DO_PRESERVE_MODE=
		;;
	  -q)
		QUIET=1
		;;
	  -r)
		CL_RCS=1
		RC_FAVOR='RCS'
		;;
	  -s)
		CL_SCCS=1
		RC_FAVOR='SCCS'
		;;
	  -x)
		XFLAG=1
		;;
	# options with argument
	  -c)
		CFLAG=1
		shift
		arg_syntax_check "$#"
		COMMENT="$1"
		;;
	  # ...
	  --)
		shift
		break
		;;

	  # unknown option
	  -?)
		syntax_error
		;;
	  # compound option
	  -??*)
		# break up a compound option
		NEW_OPTS=`$AWK <&- -v "OPT_STR=$1" 'BEGIN {
			LEN = length(OPT_STR);
			NEW_OPTS = "";
			STATUS = 0;
			for (POS=2; POS+0 <= LEN; ++POS) {
				OPT = substr(OPT_STR,POS,1);
				if (OPT !~ /[a-zA-Z0-9_]/)
					STATUS = 1;
				NEW_OPTS = NEW_OPTS " -" OPT;
			}
			print NEW_OPTS;
			exit STATUS;
		  }'` || {
			syntax_error
		  }
		shift
		set -- $NEW_OPTS ${1:+"$@"}
		continue
		;;
	  # end of options, just command arguments left
	  *)
		break
	  esac
	shift
  done

if [ "$CHECK_IN_ONLY$CHECK_OUT_ONLY" = "11" ]; then
	echo "$SYNTAX" >&2
	echo "$PROG: cannot use -i and -o together." >&2
	echo "$USAGE" >&2
	exit 1
  fi

if [ "$CL_RCS$CL_SCCS" = "11" ]; then
	echo "$SYNTAX" >&2
	echo "$PROG: cannot use -r and -s together." >&2
	echo "$USAGE" >&2
	exit 1
  fi

if [ "$XFLAG" -a ! "$DO_PRESERVE_MODE" ]; then
	echo "$SYNTAX" >&2
	echo "$PROG: cannot use -p and -x together." >&2
	echo "$USAGE" >&2
	exit 1
  fi

#
# make sure exactly one file specified, and that it either is a file
# or doesn't exist;
#
if [ "$#" = 1 ]; then
	FILE="$1"
	if [ -f "$FILE" ]; then
		IS_FILE=1
	  elif [ -h "$FILE" ]; then
		echo "$PROG: \"$FILE\" is a symbolic link." >&2
		exit 1
	  elif [ -d "$FILE" ]; then
		echo "$PROG: \"$FILE\" is a directory." >&2
		exit 1
	  elif  ls -d "$FILE" 2>&- >&1 ; then
		echo "$PROG: \"$FILE\" is not a file." >&2
		exit 1
	  fi
  elif [ "$#" = 0 ]; then
	echo "$SYNTAX" >&2
	echo "$PROG: no file specified." >&2
	echo "$USAGE" >&2
	exit 1
  else
	echo "$SYNTAX" >&2
	echo "$PROG: too many files specified." >&2
	echo "$USAGE" >&2
	exit 1
  fi

#
# if path is specified, cd there first
#
case "$FILE" in
  */*)
	DIR=`dirname "$FILE"`
	cd "$DIR" || {
		echo "$PROG: cannot chdir to \"$DIR\"." >&2
		exit 1
	}
	FILE=`basename "$FILE"`
  esac

##
## make sure FILE does not include a path;
## might be able to relax this later...
##
#case "$FILE" in
#  */*)
#	echo "$PROG: argument \"$FILE\" must be a simple file name." >&2
#	echo "$USAGE" >&2
#	exit 1
#  esac

RCS_HISTORY_FILE="$RCS_PREFIX$FILE$RCS_SUFFIX"
SCCS_HISTORY_FILE="$SCCS_PREFIX$FILE$SCCS_SUFFIX"


## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##


not() {
  #
  # negate return value of command or function;
  # collapses all return values to 0 or 1;
  #
  "$@" && return 1
  return 0
}


ask() {
  #
  # ask a question with a yes/no answer;
  # Usage: ask [-y|-n] "question"
  # Set default: -y = yes; -n = no; otherwise no default;
  # Returns: 0 (true) = yes; 1 (false) = no;
  # Note: changing the default does not effect the return value;
  #
  ASK_DFLT=
  while [ $# != 0 ]; do
	case "$1" in
	  -[yY]*) ASK_DFLT='y' ;;
	  -[nN]*) ASK_DFLT='n' ;;
	  --) shift; break ;;
	  -*) ;;
	  *) break
	  esac
	shift
    done
  ASK_PROMPT="$*"

  while $TRUE ; do
	echo $N"$ASK_PROMPT (y/n)?${ASK_DFLT:+ [$ASK_DFLT]} "$C >&2
	read ASK_ANSWER ASK_JUNK

	#[ -z "$ASK_ANSWER" ] && ASK_ANSWER="$ASK_DFLT"
	: ${ASK_ANSWER:=$ASK_DFLT}

	case "$ASK_ANSWER" in
	  [yY]*)
		return 0
		;;
	  [nN]*)
		return 1
		;;
	  *)
		#echo $N""$C >&2
		echo "" >&2
	  esac
    done

  return
}


get_file_mode() {
  #
  # save file mode (permissions) to restore later;
  # write bits will not be preserved, since SCCS and RCS will complain;
  #	arg1 = file
  # returns mode on stdout;
  # -w = set owner write perm
  #
  GFM_WOPT=0
  while [ $# != 0 ]; do
	case "$1" in
	  -w)
		GFM_WOPT=1
		;;
	  *)
		break
	  esac
	shift
    done
  $LS_L "$1" 2>&- | $AWK 'BEGIN {
	GFM_WOPT = "'"$GFM_WOPT"'";
    }
    {
	split("0 0 0",NUMS);
	# check for proper mode setting;  Note the trailing optional
	# '+' for Solaris ACL indication;
	MODE = $1;
	if (MODE ~ /^-[-r][-w][-xsS][-r][-w][-xsS][-r][-w][-xtT]\+?$/) {
		for (I=0; I+0 < 9; ++I) {
			C = substr(MODE,I+2,1);
			TYPE = I % 3;		# rwx: 0=r, 1=w, 2=x
			SET  = int(I / 3) + 1;	# 1=owner, 2=group, 3=other
			if (TYPE == 0)	# read perm?
				NUMS[SET] += (C == "-" ? 0 : 4);
			else if (TYPE == 1)	# write perm?
				##NUMS[SET] += (C == "-" ? 0 : 2);
				## dont want to set write modes
				#NUMS[SET] += 0;
				NUMS[SET] += (SET == 1 && GFM_WOPT == 1 ? 2 : 0);
			# exec perm?
			else if (C != "-") {
				if (C == "x" || C == "s" || C == "t") {
					NUMS[SET] += 1;
				}
				if (C == "s" || C == "S") {
					if (SET == 1)
						NUMS[0] += 4;
					else
						NUMS[0] += 2;
				}
				else if (C == "t" || C == "T") {
					NUMS[0] += 1;
				}
			}
		}
		for (I=0; I+0 <= 3; ++I)
			if (NUMS[I] != "")
				printf("%d",NUMS[I]);
	}
	exit;
  }'
}


set_file_mode() {
  #
  # set the file permission mode bits;
  #	arg1 = mode
  #	arg2 = file
  #
  SFM_QUIET=
  while [ $# != 0 ]; do
	case "$1" in
	  -q)
		SFM_QUIET=1
		;;
	  *)
		break;
	  esac
	shift
    done
  if [ -n "$1" ]; then

	if [ ! "$QUIET" -a ! "$SFM_QUIET" ]; then
		echo "" >&2
		echo "$PROG: Restoring file mode \"$1\"..." >&2
	  fi

	cmd="$CHMOD $1 \"$2\""

	if [ ! "$QUIET" ]; then
		echo "" >&2
		echo "$PROG: $cmd" >&2
	  fi

	eval $cmd
    fi
}


rc_check() {
  #
  # check for RC files that are checked out;
  # SCCS and RCS are very different on this;
  # return 0, nothing checked out;
  # return 1, something checked out;
  #
  [ ! "$QUIET" ] &&
    echo "" >&2

  if [ "$RC_TYPE" = 'RCS' ]; then
	RC_CHECK_TMP=`echo "$RC_CHECK" | $AWK '{sub("XXX","'"$RCS_CHECK_PATS"'"); print;}'`
	[ ! "$QUIET" ] &&
	  echo "$PROG: $RC_CHECK_TMP" >&2
	# need to build the command based on how the PATS evaluate;
	# stick the PATS in positional parameters;
	set -f
	set -- $RCS_CHECK_PATS
	set +f
	RC_CHECK_TMP=
	for RC_CHECK_PAT do
		[ "`echo $RC_CHECK_PAT`" != "$RC_CHECK_PAT" ] &&
		  RC_CHECK_TMP="$RC_CHECK_TMP${RC_CHECK_TMP:+ }$RC_CHECK_PAT"
	  done
	if [ -z "$RC_CHECK_TMP" ]; then
		RC_CHECK_STATUS=0
	  else
		RC_CHECK_TMP=`echo "$RC_CHECK" | $AWK '{sub("XXX","'"$RC_CHECK_TMP"'"); print;}'`
		eval "RC_CHECK_OUTPUT=\`$RC_CHECK_TMP 2>&-\`"
	  fi
    else
	[ ! "$QUIET" ] &&
	  echo "$PROG: $RC_CHECK" >&2
	RC_CHECK_OUTPUT="`$RC_CHECK 2>&-`"
    fi
  RC_CHECK_STATUS="$?"

  # this needs to go to stdout
  echo "$RC_CHECK_OUTPUT"

  case "$RC_TYPE" in
    'SCCS')
	[ "$RC_CHECK_STATUS" = 0 ] && return 0
	;;
    'RCS')
	[ -z "$RC_CHECK_OUTPUT" ] && return 0
    esac
  return 1
}


rc_create() {
  #
  # initial check in of an RC file;
  # ie, create a new history file;
  #
  if [ ! "$QUIET" ]; then
	echo "" >&2
	echo "$PROG: $RC_CREATE \"$1\"" >&2
    fi

  $RC_CREATE "$1" || return 1

  IS_RC_HISTORY=1

  return
}

rc_check_out() {
  #
  # check out an RC file
  # if the working copy is zipped, unzip it;
  #
  if [ ! "$QUIET" ]; then
	echo "" >&2
	echo "$PROG: $RC_CHECK_OUT \"$1\"" >&2
    fi

  $RC_CHECK_OUT "$1" || return 1

  return
}


rc_check_in() {
  #
  # check in an RC file;
  #

  #
  # if revision message option given, format it properly
  #
  CI_CMSG=
  if [ "$CFLAG" ]; then
	if [ "$RC_TYPE" = 'SCCS' ]; then
		CI_CMSG=" -y\"$COMMENT\""
	  elif [ "$RC_TYPE" = 'RCS' ]; then
		CI_CMSG=" -m\"$COMMENT\""
	  fi
    fi

  if [ ! "$QUIET" ]; then
	echo "" >&2
	echo "$PROG: $RC_CHECK_IN$CI_CMSG \"$1\"" >&2
    fi

  #$RC_CHECK_IN "$1" || return 1
  eval "\$RC_CHECK_IN"$CI_CMSG" \"\$1\"" || return 1

  return
}


rc_unedit() {
  #
  # check in an RC file,
  # abandoning any changes that were made;
  #
  if [ ! "$QUIET" ]; then
	echo "" >&2
	echo "$PROG: $RC_UNEDIT \"$1\"" >&2
    fi

  $RC_UNEDIT "$1" || return 1

  return
}


check_for_rcs() {
  #
  # see if it looks like the FILE is under RCS
  #

  # check for RCS/FILE,v
  if [ -d "$RCS_DIR" ]; then
	if [ -r "$RCS_DIR" -a -f "$RCS_DIR/$RCS_HISTORY_FILE" ]; then
		#RCS_HISTORY_FILE="$RCS_DIR/$RCS_HISTORY_FILE"
		IS_RC_HISTORY=1
		return 0
	  fi
    fi
  # check for ./FILE,v
  if [ -f "$RCS_HISTORY_FILE" ]; then
	IS_RC_HISTORY=1
	return 0
    fi
  return 1
}


check_for_sccs() {
  #
  # see if it looks like the FILE is under SCCS
  #

  # check for SCCS/s.FILE
  if [ -d "$SCCS_DIR" ]; then
	if [ -r "$SCCS_DIR" -a -f "$SCCS_DIR/$SCCS_HISTORY_FILE" ]; then
		#SCCS_HISTORY_FILE="$SCCS_DIR/$SCCS_HISTORY_FILE"
		IS_RC_HISTORY=1
		return 0
	  fi
    fi
  return 1
}


set_for_rcs() {
  #
  # set vars for RCS
  #
  RC_CHECK="$RCS_CHECK"
  RC_CREATE="$RCS_CREATE"
  RC_CHECK_OUT="$RCS_CHECK_OUT"
  RC_CHECK_IN="$RCS_CHECK_IN"
  RC_UNEDIT="$RCS_UNEDIT"
  RC_TYPE="$RCS_DIR"
  DO_PRESERVE_MODE=
  return 0
}


set_for_sccs() {
  #
  # set vars for SCCS
  #
  RC_CHECK="$SCCS_CHECK"
  RC_CREATE="$SCCS_CREATE"
  RC_CHECK_OUT="$SCCS_CHECK_OUT"
  RC_CHECK_IN="$SCCS_CHECK_IN"
  RC_UNEDIT="$SCCS_UNEDIT"
  RC_TYPE="$SCCS_DIR"
  return 0
}


rlog_filter() {
  #
  # make `rlog' output look like `sccs check' output
  #
  $AWK '{
	if (NEXT == 1) {
		NEXT = 0;
		if ($0 ~ /^\t/) {
			USER = substr($1,1,length($1)-1);
			next;
		}
	}
	if ($0 ~ /^Working file:/) {
		FILE = $3;
		next;
	}
	if ($0 ~ /^locks:/) {
		NEXT = 1;
		next;
	}
	if ($0 ~ /^head:/) {
		VERSION = $2;
		next;
	}
	if ($0 ~ /^date:/) {
		DATE = $2 " " $3;
		DATE = substr(DATE,1,length(DATE)-1);
		next;
	}
	if ($0 ~ /^========/) {
		if (FILE && USER)
			printf("%12s: being edited: %s %s %s\n",FILE,VERSION,USER,DATE);
		FILE = "";
		NEXT = 0;
		USER = "";
		VERSION = "";
		DATE = "";
	}
  }'
}


## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##


#
# check to see if super-user;
# assume that if root is writable, must be super-user
#
if [ -w / ]; then
	if [ ! "$QUIET" -a -t 0 ]; then
		echo "" >&2
		echo "$PROG: You seem to be Super-User." >&2
		echo "" >&2
		if not ask -n "  Continue"; then
			echo "" >&2
			exit 0
		  fi
	  fi
  fi


#
# list the editor being used;
# this is because sometimes something other than an editor might be
# getting used, and this provides a reminder;
#
if [ "$DO_EDIT" ]; then
	if [ ! "$QUIET" ]; then
		echo "" >&2
		echo "$PROG: Using editor \"$RC_EDITOR\"..." >&2
	  fi
  fi


#
# look for the appropriate RC history file,
# and make settings accordingly;
#
if [ "$RC_FAVOR" = 'RCS' ]; then
	{ { check_for_rcs && set_for_rcs; } ||
	  { check_for_sccs && set_for_sccs; }
	} ||
	set_for_rcs
  elif [ "$RC_FAVOR" = 'SCCS' ]; then
	{ { check_for_sccs && set_for_sccs; } ||
	  { check_for_rcs && set_for_rcs; }
	} ||
	set_for_sccs
  else
	[ ! "$QUIET" ] &&
	  echo "" >&2
	echo "$PROG: \"RC_FAVOR\" is set incorrectly." >&2
	exit 1
  fi


#
# see if the file is checked out, and what's being edited;
# if SCCS, don't bother to do this if there's no SCCS dir;
#
if [ "$RC_TYPE" = 'RCS' -o \( "$RC_TYPE" = 'SCCS' \) -a -d "$SCCS_DIR" ]; then

	FILES_BEING_EDITED=`rc_check`
	CO_STATUS="$?"

	#
	# look for FILE in the output from rc_check()
	#
	if [ -n "$FILES_BEING_EDITED" ]; then
		{
		  echo "$FILES_BEING_EDITED" |
		  $AWK 'BEGIN {
			STATUS = 1;
		  }
		  {
			if ($0 ~ /^[ ]*'"$FILE"': being edited:/) {
				STATUS = 0;
				exit;
			}
		  }
		  END {
			exit STATUS;
		  }'
		} && {
			ALREADY_CHECKED_OUT=1
		}
	  fi
  fi


#
# if only checking-in, file must already be checked out
#
if [ "$CHECK_IN_ONLY" ]; then
	if [ ! "$ALREADY_CHECKED_OUT" ]; then
		if [ ! "$QUIET" ]; then
			echo "" >&2
			echo "$PROG: file \"$FILE\" is not checked out." >&2
			echo "" >&2
		  fi
		exit 1
	  fi
  fi


#
# report on status of things checked out
#
if [ "$CO_STATUS" = 0 ]; then
	if [ ! "$QUIET" ]; then
		echo "" >&2
		echo "$PROG: Nothing being edited." >&2
	  fi
  else
	[ ! "$QUIET" ] &&
	  echo "
$PROG: Files being edited:

$FILES_BEING_EDITED" >&2
	if [ ! "$CHECK_IN_ONLY" ]; then
		if [ "$ALREADY_CHECKED_OUT" ]; then
			[ ! "$QUIET" ] &&
			  echo "" >&2
			echo "$PROG: \"$FILE\" already being edited." >&2
		  fi
	  fi
  fi


if [ "$DO_CHECK_OUT" ]; then

	#
	# FILE already checked-out?
	#
	if [ "$ALREADY_CHECKED_OUT" ]; then
		if [ "$CHECK_OUT_ONLY" ]; then
			exit 1
		  fi
		if [ "$QUIET" -o ! -t 0 ]; then
			exit 1
		  fi
		echo "" >&2
		if not ask -n "  Continue"; then
			echo "" >&2
			exit 0
		  fi
		DO_CHECK_OUT=
	  else
		#
		# there is a file, but it's not under RC?
		#
		if [ "$IS_FILE" -a ! "$IS_RC_HISTORY" ]; then
			if [ "$QUIET" -o ! -t 0 ]; then
				if [ "$RC_QUIET_CREATE_HIST" ]; then
					DO_CREATE=1
				  else
					echo "$PROG: No $RC_TYPE history file for \"$FILE\"." >&2
					DO_CHECK_OUT=
					DO_CHECK_IN=
				  fi
			  else
				echo "" >&2
				if ask -n "  No $RC_TYPE history file for \"$FILE\".  Create"; then
					DO_CREATE=1
				  else
					DO_CHECK_OUT=
					DO_CHECK_IN=
				  fi
			  fi

			if [ "$DO_CREATE" ]; then
				# save mode?
				if [ "$DO_PRESERVE_MODE" ]; then
					MODE=`get_file_mode "$FILE"`
					GOT_MODE=1
					if [ "$MODE" = '444' ]; then
						DO_PRESERVE_MODE=
					  elif [ -n "$MODE" ]; then
						if [ ! "$QUIET" ]; then
							echo "" >&2
							echo "$PROG: Saving file mode \"$MODE\"..." >&2
						  fi
					  fi
				  fi

				# create the RC file
				if not rc_create "$FILE"; then
					[ ! "$QUIET" ] &&
					  echo "" >&2
					echo "$PROG: Problem creating $RC_TYPE file for \"$FILE\"." >&2
					exit 1
				  fi

				#
				# change the modes back to original;
				#
				if [ "$DO_PRESERVE_MODE" ]; then
					set_file_mode "$MODE" "$FILE" >&2
				  fi
			  fi

		#
		# there's no file, and no history file?
		#
		  elif [ ! "$IS_FILE" -a ! "$IS_RC_HISTORY" ]; then

			if [ "$QUIET" -a ! "RC_QUIET_CREATE" ]; then
				exit 1
			  fi
			if [ ! "$QUIET" ]; then
				echo "" >&2
				echo "  No such file \"$FILE\"." >&2
			  fi
			# we will create the file
			DO_NEW_FILE=1
			DO_CHECK_OUT=
			DO_PRESERVE_MODE=
		  fi

		if [ ! "$QUIET" -a -t 0 ]; then
			echo "" >&2
			if not ask -y "  Continue"; then
				echo "" >&2
				exit 0
			  fi
		  fi
	  fi


	#
	# see if need to save file mode
	#
	if [ "$DO_CHECK_OUT" -o "$DO_CHECK_IN" ]; then
		if [ "$DO_PRESERVE_MODE" ]; then
			if [ ! "$GOT_MODE" ]; then
				MODE=`get_file_mode "$FILE"`
				GOT_MODE=1
				if [ "$MODE" = '444' ]; then
					DO_PRESERVE_MODE=
				  elif [ -n "$MODE" ]; then
					if [ ! "$QUIET" ]; then
						echo "" >&2
						echo "$PROG: Saving file mode \"$MODE\"..." >&2
					  fi
				  fi
			  fi
		  fi
	  fi


	#
	# check out file
	#
	if [ "$DO_CHECK_OUT" ]; then
		if [ "$DO_PRESERVE_MODE" ]; then
			# mode for editing
			EMODE=`get_file_mode -w "$FILE"`
		  fi

		if [ ! "$QUIET" ]; then
			echo "" >&2
			echo "$PROG: Checking out \"$FILE\"..." >&2
		  fi

		# check it out for editing
		if not rc_check_out "$FILE"; then
			[ ! "$QUIET" ] &&
			  echo "" >&2
			echo "$PROG: Problem checking out \"$FILE\"." >&2
			exit 1
		  fi

		#
		# change the modes back to original, plus write perm;
		#
		if [ "$DO_PRESERVE_MODE" ]; then
			set_file_mode "$EMODE" "$FILE" >&2
		  fi
	  fi
  fi


if [ "$DO_EDIT" ]; then
	#
	# edit file
	#
	if [ ! "$QUIET" ]; then
		echo "" >&2
		if ask -y "$PROG: Edit \"$FILE\""; then
			echo "" >&2
			echo "$PROG: Editing \"$FILE\"..." >&2
			cmd="$RC_EDITOR \"$FILE\""
			echo "" >&2
			echo "$PROG: $cmd" >&2
			eval $cmd
		  fi
	  else
		cmd="$RC_EDITOR \"$FILE\""
		eval $cmd
	  fi

	#
	# set execute bits on a new file?
	#
	if [ "$DO_NEW_FILE" -a -f "$FILE" ]; then
		if [ "$XFLAG" ]; then
			set_file_mode -q "$XMODE" "$FILE" >&2
			DO_PRESERVE_MODE=1
		  fi
	  fi
  fi


if [ "$DO_CHECK_IN" ]; then
	#
	# save perms?
	#
	if [ "$DO_PRESERVE_MODE" ]; then
		if [ ! "$GOT_MODE" ]; then
			MODE=`get_file_mode "$FILE"`
			GOT_MODE=1
			if [ "$MODE" = '444' ]; then
				DO_PRESERVE_MODE=
			  elif [ -n "$MODE" ]; then
				if [ ! "$QUIET" ]; then
					echo "" >&2
					echo "$PROG: Saving file mode \"$MODE\"..." >&2
				  fi
			  fi
		  fi
	  fi
	#
	# check in file, or create if this is a new file
	#
	if [ "$DO_NEW_FILE" ]; then
		#
		# created a new file; enter into RC?
		#
		if [ ! "$QUIET" -a -t 0 ]; then
			echo "" >&2
			if not ask -y "$PROG: Create an $RC_TYPE history file for \"$FILE\""; then
				echo "" >&2
				exit 0
			  fi
		  fi

		if [ ! "$QUIET" ]; then
			echo "" >&2
			echo "$PROG: Creating history file for \"$FILE\"..." >&2
		  fi

		if [ ! "$QUIET" -o "$RC_QUIET_CREATE" ]; then
			if not rc_create "$FILE"; then
				[ ! "$QUIET" ] &&
				  echo "" >&2
				echo "$PROG: Problem creating $RC_TYPE file for \"$FILE\"." >&2
				exit 1
			  fi
			if [ "$DO_PRESERVE_MODE" ]; then
				set_file_mode "$MODE" "$FILE" >&2
			  fi
		  fi
	  else
		#
		# check in file
		#
		[ ! "$QUIET" ] &&
		  echo "" >&2
		if [ "$QUIET" -o ! -t 0 ]; then
			if not rc_check_in "$FILE"; then
				[ ! "$QUIET" ] &&
				  echo "" >&2
				echo "$PROG: Problem checking in \"$FILE\"." >&2
				exit 1
			  fi
		  else
			if ask -y "$PROG: Check in \"$FILE\""; then
				echo "" >&2
				echo "$PROG: Checking in \"$FILE\"..." >&2

				if [ ! "$CFLAG" ]; then
					echo "" >&2
					echo "  ***  COMMENTS  ***" >&2
				  fi

				if not rc_check_in "$FILE"; then
					[ ! "$QUIET" ] &&
					  echo "" >&2
					echo "$PROG: Problem checking in \"$FILE\"." >&2
					exit 1
				  fi

				#
				# change the modes back to original, sans write perms;
				#
				if [ "$DO_PRESERVE_MODE" ]; then
					set_file_mode "$MODE" "$FILE" >&2
				  fi
			  else
				#
				# Not checking in; unedit?
				#
				echo "" >&2
				if ask -n "$PROG: Unedit \"$FILE\""; then
					echo "" >&2
					echo "$PROG: Unediting \"$FILE\"..." >&2
					rc_unedit "$FILE"

					#
					# change the modes back to original,
					# sans write perms;
					#
					if [ "$DO_PRESERVE_MODE" ]; then
						set_file_mode "$MODE" "$FILE" >&2
					  fi
				  fi
			  fi
		  fi
	  fi
  fi


#
# Bye!
#
if [ "$DO_ALL" ]; then
	if [ ! "$QUIET" ]; then
		echo "" >&2
		echo "$PROG: Done." >&2
		echo "" >&2
	  fi
  fi

exit
