#!/bin/csh -f
#>
#> PACT-COMMIT - commit to the PACT repository
#>
#> Usage: pact-commit [-d] [-f] [-h] [-m] [-n] [-s] [-v] [-M <msgfile>] "message"
#>
#> Options:
#>          d - Commit docs only
#>          f - Do sync and full, clean build first
#>          h - Display the help package
#>          m - Commit manager only
#>          n - Do not change any files.
#>              NOT USEFUL use "pact-sync -n"
#>          s - Sleep if Lock on
#>          v - Display the Version and Release of the Source Control System
#>          M - Name of file with a message about the commit.  
#>  "message" - Brief message about the commit. Every thing following the
#>              options is included as part of the message. Use quotes to
#>              preserve blanks, special characters.
#>
#> The "message" or at least one option MUST be specified.
#>
####################
# include "cpyright.h"
#

# see PCD for the reason for this
set Here = `pwd` ; cd ; set RealHome = `pwd` ; cd $Here ; unset $Here

if ($?USER == 0) then
   if ($?LOGNAME == 0) then
      set USER = "anonymous"
   else
      set USER = $LOGNAME
   endif
endif

set UserDir   = `pwd | sed "s|$RealHome|$home|"`
set BaseDir   = $UserDir/..
set WhoIAm    = $USER
set Manager   = manager
set Message   = ""
set MsgFile   = ""
set Report    = ""
set CvsOpt    = ""
set ManOnly   = FALSE
set DocOnly   = FALSE

set TagID     = `/bin/date '+D%y_%m_%d_%H_%M_%S'`
set TAGing    = ""

set HelpCode  = $UserDir/$0
set CommitLog = $UserDir/commit.log
set UpdateLog = $UserDir/update.log
set Changed   = $UserDir/Changed
set UnChanged = $UserDir/UnChanged
set System    = `./system-id`

set SCSRepository = ""
set SRCDate       = ""
set Sleep         = "NO"

set CleanBuild   = FALSE

switch (`uname -s`)
   case "IRIX":
        set MAIL = Mail
        breaksw

   default:
        set MAIL = mail
        breaksw
endsw

if ($#argv == 0) then
   goto Help
endif


set Opt = "$HelpCode:t  $argv"

while ("$1" =~ -*)
   switch ($1)
      case -d*:                                  #doc only
              set DocOnly = TRUE
              set ManOnly = FALSE
	      breaksw

      case -f*:
	      set CleanBuild = TRUE
	      breaksw

      case -h*:                                  #print Usage:
	      goto Help

      case -m*:                                  #manager only
              set ManOnly = TRUE
              set DocOnly = FALSE
	      breaksw

      case -M*:                                  #get log message from file
              shift
              set MsgFile = $UserDir/$1
              if (! -e $MsgFile ) then
                 echo "Can not find message file $MsgFile"
                 goto Help
              endif
              breaksw

      case -n*:                                  #NO update, report only
	      set Report = " Report ONLY..."
	      set CvsOpt = "-nq"
	      breaksw

      case -s*:
              set Sleep = "YES"
              breaksw

      case -T*:                                  #NO tagging
#	      set TAGing = "FALSE"
	      breaksw

      case -v*:                                  #Display version...
              source $UserDir/pact-scs
	      echo "  Source Version:   " $SCSVersion
	      echo "  Software Release: " $SCSRelease
	      echo ""
	      exit(1)

      default:
 	      goto Help
   endsw
   shift
end
set Message = (\"<$WhoIAm|$TagID> $argv\")

# define the Source Control System
source $UserDir/pact-scs

# do everything with full option
if (($CleanBuild == TRUE) && ($ManOnly == FALSE) && ($DocOnly == FALSE)) then
   ./pact-sync
   pact remove
   pact build
endif

if ("$RemoteHost" == "") then
   groups | grep dpact > /dev/null
   if ($status == 1) then
      echo ""
      echo "You must be in the PACT developers group to run PACT-COMMIT"
      echo ""
      exit(1)
   endif
endif

#
# check if repository is locked
#
if ($Sleep == "YES") then
   ./check.lock
else
   if ($ExistLock == TRUE) then
      echo ""
      echo " *** REPOSITORY IS LOCKED ***"
      echo ""
      CatLock
      echo ""
      echo ""
      exit(1)
   endif
endif

#
# initiate a lock on the repository
#
Makelock
echo $Message >>& $SCSLock

#
# run the test suite
#
if (($ManOnly == FALSE) && ($DocOnly == FALSE)) then
   echo -n "Testing ................... "  >>& $SCSLock
   pact test
   if ($status != 0) then
      Unlock
      echo ""
      echo "Your version failed the test suite"
      echo "All tests must pass before the commit will be allowed"
      echo ""
      exit(1)
   endif
   echo "done"  >>& $SCSLock
endif

rm -f $CommitLog $Changed $UnChanged $UpdateLog

echo "Commit Log		Host: `uname -n` running `uname`" >>&! $CommitLog
echo "User:  $WhoIAm		Date: `date`" >>& $CommitLog
echo " "   >>& $CommitLog
echo "Operation: $Opt "  >>& $CommitLog 
echo " "  >>& $CommitLog

echo " "
if ($ManOnly == TRUE) then
   echo "Committing changes to the Manager only"
   echo "Committing changes to the Manager only" >>& $CommitLog

else if ($DocOnly == TRUE) then
   cd ../doc
   echo "Committing changes to the Documents only"
   echo "Committing changes to the Documents only" >>& $CommitLog

else
   cd $BaseDir
   echo "Committing source changes"
   echo "Committing source changes" >>& $CommitLog
endif

#
# do the $SCSName update
#
echo " "
echo " " >>& $CommitLog
echo "Checking Local PACT Sources"
echo "Checking Local PACT Sources" >>& $CommitLog
echo -n "Checking PACT Sources ..... "  >>& $SCSLock
echo "Host: `uname -n`           Date: `date`" >>&! $UpdateLog
echo " "  >>& $UpdateLog
($Update >> $UpdateLog) >& /dev/null
if ($status != 0) then
   Unlock
   echo "$SCSName UPDATE FAILED"
   exit($status)
endif
echo "Local PACT Sources Checked"
echo "done"  >>& $SCSLock

#
# categorize the update status of the files
# as M,A,R - ok to commit and C - resolve conflicts
#
awk '($1 ~ /[MAR]/) {print $2}' $UpdateLog > $UnChanged
awk '($1 == "C") {print $2}' $UpdateLog > $Changed

set ChangedList   = `cat $Changed`
set UnChangedList = `cat $UnChanged`

if ($#ChangedList > 0) then
   Unlock
   echo ""
   echo "There are files with conflicts to be resolved."
   echo "See $Changed for the list."
   echo ""
   cd $Manager
   exit(1)
else
   rm $Changed
endif

#
# do the actual commit
#
echo " "
echo " " >>& $CommitLog
echo "Committing Local PACT Sources"
echo "Committing Local PACT Sources" >>& $CommitLog
echo -n "Committing PACT Sources ... "  >>& $SCSLock
if ($MsgFile == "" ) then
     echo "$Commit -m $Message" >>& $CommitLog
     eval "$Commit -m $Message" >>& $CommitLog
     if ($status != 0) then
        Unlock
        echo "Commit FAILED"
        exit(1)
     endif
else
     set tmpMSG = "$UserDir/tmpMSG"
     rm -f $tmpMSG
     echo $Message | sed s/\"//g > $tmpMSG
     cat   $MsgFile >> $tmpMSG
     echo "$Commit -f $tmpMSG" >>& $CommitLog
     eval "$Commit -f $tmpMSG" >>& $CommitLog
     if ($status != 0) then
        rm -f $tmpMSG
        Unlock
        echo "Commit FAILED"
        exit(1)
     endif
     rm -f $tmpMSG
endif

#
# report on changed files
#
echo " "
echo "   The following files have been updated:"
cat $UnChanged
echo " "

echo "Local PACT Sources Committed"
echo "done"  >>& $SCSLock

if (($ManOnly == TRUE) || ($DocOnly == TRUE)) then
   cd $BaseDir
endif

#
# tag the new repository version
#
echo " "
echo " " >>& $CommitLog
echo "Tagging this version with $TagID"
echo "Tagging this version with $TagID" >>& $CommitLog
echo -n "Tagging ................... "  >>& $SCSLock
echo "$Tag $TagID pact" >>& $CommitLog
eval "$Tag $TagID pact" >>& $CommitLog
echo "Version tagged"
echo "done"  >>& $SCSLock

#
# release the lock
#
echo ""
echo "" >>& $CommitLog
echo "The Repository is Unlocked"
echo "The Repository is Unlocked" >>& $CommitLog
Unlock

#
# untouch the local source libraries
#
# since CVS has touched the sources
# lets touch the relevant files and keep the time stamps
# properly ordered

if (($ManOnly == FALSE) && ($DocOnly == FALSE)) then
   echo ""
   echo "" >>& $CommitLog
   echo "Resetting time stamps on Local PACT Sources"
   echo "Resetting time stamps on Local PACT Sources" >>& $CommitLog
   find . -name "*.[chf]" -exec touch {} \;
   cd dev/$System/lib
   touch foo.a
   mkdir ftmp
   mv *.a ftmp
   cd ftmp
   foreach lib (*.a)
      if ($lib == "foo.a") then
         rm $lib
      else if ($lib == "libpdb.a") then
         mv $lib ..
      else
         ar x $lib
         rm $lib
         touch *.o
         ar rc $lib *.o
         mv $lib ..
         touch ../$lib
         rm -f *.o _*
      endif
   end
   cd ..
   rm -rf ftmp
   touch ../bin/*

# PDB depends on the detect executable so extra
# measures must be taken now
   if (-e libpdb.a) then
      touch ../../../pdb/pdform.h
      ar x libpdb.a
      rm libpdb.a
      touch *.o
      ar rc libpdb.a *.o
      rm -f *.o _*
   endif
   echo "Time stamps reset"
endif

cd $UserDir

echo ""
echo "" >>& $CommitLog
echo "PACT Repository Updated"
echo "PACT Repository Updated" >>& $CommitLog
echo "See the file $CommitLog for more detail."
echo ""

exit($status)

Help:
   awk '($1 == "#>") {print}' $HelpCode      #print Usage:
   exit(1)
