#!/bin/csh -f

if ( "$1" == "" ) then
   echo " "
   echo "SYNTAX:  ckin <new | old | mod | del | test> <file>"
   echo " "
   echo "ckin invokes PVCS software in order to manipulate"
   echo "the specified file archive.  One of four operations"
   echo "must be selected to operate on the file archive:"
   echo " "
   echo "new            completey new unit, does not exist"
   echo "old            existing unit reused, unchanged"
   echo "mod            existing unit with modifications"
   echo "del            existing unit has been obsoleted"
   echo " "
   echo "If test or any other operation is selected, then"
   echo "ckin will simply print the environment and exit"
   echo "without invoking the PVCS software."
   echo " "
   echo "ckin always produces a release/subsystem specific"
   echo "log file called something like ISTP_R1.recon.vlog"
   echo "in the current user's home directory.  Output is"
   echo "always appended to this file so that the original"
   echo "contents, if any, will not be destroyed."
   echo " "
   echo "ENVIRONMENT:  the following variables must be set"
   echo " "
   echo '$PROJECT       { istp, pacor2 }'
   echo '$SUBSYSTEM     { ad , dps , fs , recon , trend }'
   echo '$RELEASE       { ISTP_B1 , ISTP_B2, ISTP_R1, ... }'
   echo " "
   echo "WARNING:  It is easy to submit files to the wrong"
   echo "archives if you set these values automatically."
   echo "It is best to leave them undefined and allow the"
   echo "script to complain when they are not set properly."
   echo " "
   exit 1
endif

if ( ! $?PROJECT ) set PROJECT=NULL
if ( ! $?SUBSYSTEM ) set SUBSYSTEM=NULL
if ( ! $?RELEASE ) set RELEASE=NULL
if ( ! $?HOME ) set HOME=.

echo "PROJECT=$PROJECT  SUBSYSTEM=$SUBSYSTEM  RELEASE=$RELEASE"
set logfile=$HOME/$RELEASE.$SUBSYSTEM.vlog

if ( "$PROJECT" != "istp" && "$PROJECT" != "pacor2" ) then
   echo " "
   echo 'FATAL:  $PROJECT invalid'
   echo " "
   exit 2
endif

if (      "$PROJECT" != "pacor2" \
     && "$SUBSYSTEM" != "ad"     \
     && "$SUBSYSTEM" != "fs"     \
     && "$SUBSYSTEM" != "recon"  \
     && "$SUBSYSTEM" != "trend"  \
) then
   echo " "
   echo 'FATAL:  $SUBSYSTEM invalid'
   echo " "
   exit 3
endif

set pattern=`expr "$RELEASE" : '\(.....\).*'`
if ( "$pattern" != "ISTP_" ) then
   echo " "
   echo 'FATAL:  $RELEASE invalid'
   echo " "
   exit 4
endif

if ( "$1" == "new" ) then

   vcs -I "$2"
   #continue

else if ( "$1" == "old" ) then

   vcs -V$RELEASE "$2"
   if ( "$status" == "0" ) then

      echo -n "$1  " >> $logfile
      vlog -Bn "$2" >> $logfile
      mv $2 .$2.$1

   endif
   exit $status

else if ( "$1" == "mod" ) then

   set status=0
   #continue

else if ( "$1" == "del" ) then

   echo -n "$1  " >> $logfile
   vlog -Bn "$2" >> $logfile
   mv $2 .$2.$1
   exit $status

else

   echo "LOGFILE=$logfile"
   exit 0

endif

if ( "$status" == "0" ) then
   # lock and load
   vcs -L "$2"
   put -V$RELEASE -GDEV "$2"
   if ( "$status" == "0" ) then
      echo -n "$1  " >> $logfile
      vlog -Bn "$2" >> $logfile
   else
      echo -n "$1* " >> $logfile
      vlog -Bn "$2" >> $logfile
   endif
endif

exit $status
