#!/bin/csh -f
#>
#> PACT-SYNC - synchronize the local PACT sources with those in the repository
#>
#> Usage: pact-sync [-h] [-n] [-s] [-v]
#>
#> Options: h - Display the help package
#>          n - Do not change any files. Issue report only, do not
#>              update, or merge any esisting files. 
#>          s - Sleep if Lock on
#>          v - Display the Version and Release of the Source Control System
#>
#
# Modification History:
#   03-21-94 Jan Moura, LLNL:  Added Usage info and options [-h], [-n], [-v]
#                              Removed option [-m] not used.
#   12-08-94 Jan Moura, LLNL:  Resolve display of files with conflicts
#    7-27-94 Jan Moura, LLNL:  Chg name of logfile, cosmetic.
####################
# include "cpyright.h"
#

unalias cd

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

cd ..

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

set WhoIAm      = $USER
set Manager     = manager
set Report      = ""
set CvsOpt      = ""

set UserDir     = `pwd | sed "s|$RealHome|$home|"`/$Manager
set ConflFile   = $UserDir/Confl.Sources
set HelpCode    = $UserDir/$0
set SyncLogPerm = $UserDir/pact-sync.log
set SyncLogTmp  = $UserDir/sync.log
set Merges      = $UserDir/merge.el

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

# define the Source Control System (called twice)
source ./manager/pact-scs


set Opt = "$HelpCode:t  $argv"

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

#---Check the command argument list
if ($#argv > 1) then
   goto Help
endif

if ($#argv == 1) then
   switch ($argv[1])
      case -h*:                                  #print Usage:
   	   goto Help

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

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

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

      default:                                   #print Usage:
	   goto Help
   endsw
endif

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

rm -f $SyncLogTmp
rm -f $SyncLogPerm $ConflFile

# define the Source Control System (repeat to define -n cvs option)
source ./manager/pact-scs


echo ""
echo "Synchronizing Sources" $Report
echo `date`
echo "(see $SyncLogPerm for more information)"
echo ""
echo ""				       >>& $SyncLogPerm
echo "Synchronizing Sources" $Report   >>& $SyncLogPerm
echo "  Host: `uname -n` running `uname`" >>&! $SyncLogPerm
echo "  User:  $WhoIAm		Date: `date`" >>& $SyncLogPerm
echo " "   >>& $SyncLogPerm
echo "Operation: $Opt "  >>& $SyncLogPerm 
echo " "  >>& $SyncLogPerm


# build a list of directories which $SCSName is source managing
set List    = `find . -name $ADM -print`
set DirList = ""
foreach i ($List)
   set DirList = (`echo "$i:h"` $DirList)
end

# sync the sources in each of the $SCSName managed directories
foreach i ($DirList)
   cd $i
   echo  $i
   echo Checking $i >& $SyncLogTmp

   $Update -l  >>& $SyncLogTmp
   set ErrStat = $status
   KillZombie
   if ($ErrStat != 0) then
      echo "$SCSName UPDATE failed - terminating PACT-SYNC"
      echo "$SCSName UPDATE failed - terminating PACT-SYNC" >>& $SyncLogTmp
      exit(1)
   endif

# if "-n" option specified, then we are done (omit remainder of loop)
   if ($CvsOpt == "-nq") then
      cat $SyncLogTmp
      echo "" >>& $SyncLogPerm
      cat $SyncLogTmp >>& $SyncLogPerm
      rm $SyncLogTmp
      cd $UserDir:h
      continue
   endif

# take care of conflicting changes
   awk '($1 == "C")&&($2 !~ /Makefile/) {print $2}' $SyncLogTmp > Confl
   set Confl  = `cat Confl`
   foreach mod ($Confl)
      echo "     $mod      had conflicting changes<<<<<<<<<<<<<<<<<<<<"
   end

   cat Confl >>& $ConflFile
   rm Confl
   cd $UserDir:h

# report changed files
   awk '($1 ~ /[PUMAR]/) {print $1 " " $2}' $SyncLogTmp > Files.OK
   set Files = `cat Files.OK`
   while ($#Files > 1)
      echo "   $Files[1] $Files[2]"
      shift Files
      shift Files
   end
   rm Files.OK

   cat $SyncLogTmp >>& $SyncLogPerm
   rm $SyncLogTmp
end

# if there are no conflicts remove the misleading evidence
if (-z $ConflFile) then
   rm $ConflFile
endif

# have EDITOR display list of files in conflict
if (-e $ConflFile) then
   echo "========================================"
   echo " The following files are in conflict"
   echo ""
   echo `cat $ConflFile`
   echo ""
   echo "========================================" >>& $SyncLogPerm
   echo "   The following files are in conflict"   >>& $SyncLogPerm
   echo ""                                         >>& $SyncLogPerm
   echo `cat $ConflFile`                           >>& $SyncLogPerm
   echo ""                                         >>& $SyncLogPerm
   $EDITOR  $ConflFile
   if ($status != 0) then
      echo "$EDITOR failed - terminating PACT-SYNC"
      exit(1)
   endif
endif

echo "Sources Synchronized" $Report
echo "(see $SyncLogPerm for log information)"
echo ""
echo ""				      >>& $SyncLogPerm
echo "Sources Synchronized" $Report   >>& $SyncLogPerm
echo ""				      >>& $SyncLogPerm

cd $Manager

exit($status)

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