#!/bin/csh -f
#
# PACT-IMPORT - import PACT sources from another repository
#
# Usage:   pact-import
#
# Modification History:
#   08-12-94 Jan Moura, LLNL:  Tests/corrections; extended trace messages;
#                              added tagging; list source file conflicts;
#                              checkout from repo by import-tag; testing
####################
# include "cpyright.h"
#

unalias cd

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

set help          = FALSE
set DEBUG         = TRUE
set DateFile      = $CVSROOT/.import-date
set TagFile       = $CVSROOT/.import-tag
set LogFile       = $cwd/import.log
set ImportingDir  = $cwd
set SCSDir        = "."
set SCSRepository = ""
set SRCDate       = ""
set HN            = "`uname -n`"
set Message       = (\" $USER on "$HN", Running import.\")

if (-e $TagFile) then
   set ImportTag = `cat $TagFile`
   set SCSTag    = "-r $ImportTag"
else
   set SCSTag    = ""
endif

while ($#argv >= 1)
   switch ($argv[1])
      case -1:
         set SCSTag = ""
         echo ""
         echo "Doing import to repository head"
         echo ""
         breaksw
      case -h:
         set help = TRUE
         echo " "
         echo " PACT-IMPORT - import PACT sources from another repository"
         echo " "
         echo "Usage:   pact-import [-1] [-h]"
         echo " "
         echo "Options: "
         echo "   -1  Import to repository head"
         echo "   -h  Print this message"
         echo " "
         exit(1)
         breaksw
   endsw
   shift
end

# do an ls to wake up the automounter if one is present
ls $CVSROOT > /dev/null

set Repository = `find $CVSROOT -name manager -print`
#set Repository = `expr $Repository:h : $CVSROOT'\/\(.*\)'`

if (!(-d $Repository)) then
   echo ""
   echo "Non existent repository: $Repository"
   echo ""
   exit(1)
endif

if (!(-e $SCSDir/pact-scs)) then
   echo " "
   echo "Can't find Source Control System Information in $SCSDir"
   echo " "
   exit(1)
endif

# Source Control System Variables
source $SCSDir/pact-scs

#
# check if repository is locked
#
if ($ExistLock == TRUE) then
   echo ""
   echo " *** REPOSITORY IS LOCKED ***"
   echo ""
   CatLock
   echo ""
   echo ""
   exit(1)
endif

set Source = ""
touch pact-foo.tar.gz
foreach i (pact*tar.gz)
   if ($i != pact-foo.tar.gz) then
      set Source = $i
   else
      rm $i
   endif
end

set Distr = ""
touch pact-foo-src
foreach i (pact*-src)
   if ($i != pact-foo-src) then
      set Distr = $i
   else
      rm $i
   endif
end

echo "Getting new sources ...... "
if ($Source != "") then

   set CodeDate = `expr $Source : 'pact\(.*\).tar.gz'`
   set SrcFile  = "pact$CodeDate.tar.gz"

else if ($Distr != "" ) then
   echo "Distributions must be of the form pact.*.tar.gz"
   exit(1)

   set CodeDate = `expr $Distr : 'pact\(.*\)-src'`
   set SrcFile  = "pact$CodeDate.tar.gz"

   set STDOUT = extr.tmp
   rm -f $STDOUT
   touch $STDOUT

# write the shell script to extract the gzip'd tar file
   echo "#\!/bin/csh -f" >>& $STDOUT
   echo 'set Signature = "-----------------------***   SEPARATOR   ***-----------------------"' >>& $STDOUT
   echo 'while (1)' >>& $STDOUT
   echo '   set Input = "$<"' >>& $STDOUT
   echo '   if ("$Input" == "$Signature") then' >>& $STDOUT
   echo '      set File = "$<"' >>& $STDOUT
   echo '      break' >>& $STDOUT
   echo '   endif' >>& $STDOUT
   echo 'end' >>& $STDOUT
   echo 'if ($?File) then' >>& $STDOUT
   echo '   cat > '$SrcFile >>& $STDOUT
   echo 'endif' >>& $STDOUT
   echo 'exit($status)' >>& $STDOUT

   chmod a+x $STDOUT
   cat $Distr | $STDOUT
   rm $Distr
   rm -f $STDOUT

else

   echo "No sources to import"
   exit(1)

endif

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

rm -f $LogFile
touch $LogFile

echo "Version: $CodeDate Source File: $SrcFile"
echo "Date/Time: " `date`
echo "Version: $CodeDate Source File: $SrcFile" >>& $LogFile
echo "Date/Time: " `date` >>& $LogFile
if (-d import) then
   echo "Removing old import directory"
   echo "Removing old import directory" >>& $LogFile
   rm -rf import >>& $LogFile
endif

echo "Creating import directory"
echo " --------------------------------------------" >>& $LogFile
echo "Creating import directory" >>& $LogFile
mkdir import

echo "Moving new sources into import directory"
echo "Moving new sources into import directory" >>& $LogFile
mv $SrcFile import/pact.tar.gz >>& $LogFile

cd import

echo "Uncompressing new sources"
echo " --------------------------------------------" >>& $LogFile
echo "Uncompressing new sources" >>& $LogFile
gzip -d pact.tar.gz >>& $LogFile
if ($status != 0) then
   echo "Uncompress failed, check tar file in manager/import"
   echo "Entering inferior shell to correct tar file"
   echo "Type exit at the prompt to resume"
   set here = $cwd
   csh -f
   echo ""
   cd $here
endif
echo "" >>& $LogFile

echo "Backing up the repository to pact/manager/import/repos.tar.gz"
echo "Backing up the repository to pact/manager/import/repos.tar.gz" >>& $LogFile
tar cvf - $CVSROOT | gzip > repos.tar.gz
echo "" >>& $LogFile

if (-e $DateFile) then

   mkdir new

# we are in the import/new directory now
   cd new

   mv ../pact.tar .

   echo -n "Unpacking new sources ...... "  >>& $SCSLock
   echo "Unpacking new sources"
   echo "Current Directory:  " `pwd` 
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "" >>& $LogFile
   echo "Unpacking new sources" >>& $LogFile
   echo "Current Directory:  " `pwd` >>& $LogFile
   echo "" >>& $LogFile
   echo "------------------------------------------------------------------------------" >>& $LogFile
   tar xvf pact.tar >>& $LogFile
   if ($status != 0) then
      echo "TAR extract failed, check sources in manager/import"
      echo "Entering inferior shell to correct sources"
      echo "Type exit at the prompt to resume"
      set here = $cwd
      csh -f
      echo ""
      cd $here
   endif
   echo "" >>& $LogFile
   echo "done"  >>& $SCSLock

   cd ..
   mkdir old

# we are in the import/old directory now
   cd old
   set Repository = $Repository:h
   echo -n "Checking out old sources ... "  >>& $SCSLock
   echo "Checking out sources from $Repository"
   echo "Current Directory:  " `pwd` 
   echo "cvs co $SCSTag pact" 
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "" >>& $LogFile
   echo "Checking out sources from $Repository" >>& $LogFile
   echo "Current Directory:  " `pwd` >>& $LogFile
   echo "cvs co $SCSTag pact" >>& $LogFile
   echo "" >>& $LogFile
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "" >>& $LogFile
   cvs co $SCSTag pact >>& $LogFile
   set ErrStat = $status
   KillZombie
   echo "done"  >>& $SCSLock

# rename the old files to *.old
   echo -n "Processing old sources ..... "  >>& $SCSLock
   echo "Processing old sources"
   echo "" >>& $LogFile
   echo "Processing old sources" >>& $LogFile
   rm -f tmp tmp.lst
   touch tmp
   echo "#\!/bin/csh -f" >> tmp
   echo 'while (1)' >> tmp
   echo '   set x = $<' >> tmp
   echo '   if (($#x < 1) || ($x == "")) then' >> tmp
   echo '      exit(0)' >> tmp
   echo '   endif' >> tmp
   echo '   if (!(-d "$x")) then' >> tmp
   echo '      mv $x $x.old' >> tmp
   echo '      touch $x.chk' >> tmp
   echo '   endif' >> tmp
   echo 'end' >> tmp
   chmod a+x tmp

#   echo "" >>& $LogFile
#   echo "The shell script to process the old sources:" >>& $LogFile
#   echo "" >>& $LogFile
#   cat tmp >>& $LogFile
#   echo "" >>& $LogFile

   cd pact
   echo "done"  >>& $SCSLock

# we are in the import/old/pact directory now
   find . -name CVS -prune -o -print > ../tmp.lst
   cat ../tmp.lst | ../tmp
   cd ..

# we are in the import/old directory now
   rm -f tmp tmp.lst
   echo "" >>& $LogFile


# eliminate old directories with no new counterparts
# saves making repository changes erroneously
   echo "Adjusting the directory tree"
   echo "Current Directory:  " `pwd` 
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "" >>& $LogFile
   echo "Adjusting the directory tree" >>& $LogFile
   echo "Current Directory:  " `pwd` >>& $LogFile
   echo "" >>& $LogFile
   echo "------------------------------------------------------------------------------" >>& $LogFile
   rm -f tmp
   touch tmp
   echo "#\!/bin/csh -f" >> tmp
   echo 'while (1)' >> tmp
   echo '   set x = $<' >> tmp
   echo '   if ($x == ".") then' >> tmp
   echo '      exit(0)' >> tmp
   echo '   endif' >> tmp
   echo '   echo "   $x"' >> tmp
   echo '   if ((-d $x) && !(-d ../../new/pact/$x)) then' >> tmp
   echo '      rm -rf $x' >> tmp
   echo '      echo "      There is no $x directory in the new sources to import"' >> tmp
   echo '   endif' >> tmp
   echo 'end' >> tmp
   chmod a+x tmp

#   echo "" >>& $LogFile
#   echo "The shell script to prune directories:" >>& $LogFile
#   echo "" >>& $LogFile
#   cat tmp >>& $LogFile
#   echo "" >>& $LogFile

   cd pact

# we are in the import/old/pact directory now
# list all the directories of this tree sorted from most nested to least
# then blow them away if they have no counterpart in the other tree
   find . -type d \! -name CVS -print | sort -r | sed s'|\.\/||' > ../tmp.lst
   cat ../tmp.lst | ../tmp
   cd ..

# we are in the import/old directory now
   rm -f tmp tmp.lst
   echo "" >>& $LogFile

   cd ../new
# we are in the import/new directory now

# move the new files to the old directories as *.new
   echo -n "Processing new sources ..... "  >>& $SCSLock
   echo "Processing new sources"
   echo "" >>& $LogFile
   echo "Processing new sources" >>& $LogFile
   rm -f tmp
   touch tmp
   echo "#\!/bin/csh -f" >> tmp
   echo 'if (!(-d $1)) then' >> tmp
   echo '   mv $1 ../../old/pact/$1.new' >> tmp
   echo '   touch ../../old/pact/$1.chk' >> tmp
   echo 'endif' >> tmp
   chmod a+x tmp

#   echo "" >>& $LogFile
#   echo "The shell script to process the new sources:" >>& $LogFile
#   echo "" >>& $LogFile
#   cat tmp >>& $LogFile
#   echo "" >>& $LogFile

   cd pact
   echo "done"  >>& $SCSLock

# we are in the import/new/pact directory now

   find . -exec ../tmp {} \; >>& $LogFile
   cd ..

# we are in the import/new directory now
   rm -f tmp
   echo "" >>& $LogFile

   cd ../old
# we are in the import/old directory now

# reconcile things
   echo -n "Reconciling sources ........ "  >>& $SCSLock
   echo "Reconciling sources"
   echo "Current Directory:  " `pwd`
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "" >>& $LogFile
   echo "Reconciling sources" >>& $LogFile
   echo "Current Directory:  " `pwd` >>& $LogFile
   echo "" >>& $LogFile
   echo "------------------------------------------------------------------------------" >>& $LogFile
   rm -f tmp
   touch tmp
   echo "#\!/bin/csh -f" >> tmp
   echo 'set Base    = $1:r' >> tmp
   echo 'set BaseDir = $Base:h' >> tmp
   echo 'set File    = $Base:t' >> tmp
   echo 'set Here    = $cwd' >> tmp
   echo 'if ((-e $Base.new) && (-e $Base.old)) then' >> tmp
   echo '   cp $Base.new $Base' >> tmp
   echo 'else if (-e $Base.new) then' >> tmp
   echo '   cp $Base.new $Base' >> tmp
   echo '   cd $BaseDir' >> tmp
   echo '   cvs add $File' >> tmp
   echo '   cd $Here' >> tmp
   echo 'else if (-e $Base.old) then' >> tmp
   echo '   cd $BaseDir' >> tmp
   echo '   cvs update -A $File' >> tmp
   echo '   rm $File' >>  tmp
   echo '   cvs remove $File' >> tmp
   echo '   cd $Here' >> tmp
   echo 'endif' >> tmp
   echo 'rm $1' >> tmp
   chmod a+x tmp

#   echo "" >>& $LogFile
#   echo "The shell script to reconcile sources:" >>& $LogFile
#   echo "" >>& $LogFile
#   cat tmp >>& $LogFile
#   echo "" >>& $LogFile

   cd pact
   echo "done"  >>& $SCSLock

# we are in the import/old/pact directory now

   echo  Find nnn.CHK from `pwd` >>& $LogFile
   find . \( -name "*.chk" -o -name ".*.chk" \) -exec ../tmp {} \; >>& $LogFile
   echo  Find nnn.NEW from `pwd` >>& $LogFile
   find . \( -name "*.new" -o -name ".*.new" \) -exec rm {} \; >>& $LogFile
   echo  Find nnn.OLD from `pwd` >>& $LogFile
   find . \( -name "*.old" -o -name ".*.old" \) -exec rm {} \; >>& $LogFile
   cd ..

# we are in the import/old directory now
   rm -f tmp
   echo "" >>& $LogFile

   if ($SCSTag != "") then
      echo -n "Updating the sources ....... "  >>& $SCSLock
      echo "Updating the new source"
      echo "Current Directory:  " `pwd` 
      echo "------------------------------------------------------------------------------" >>& $LogFile
      echo "" >>& $LogFile
      echo "Updating the new source" >>& $LogFile
      echo "Current Directory:  " `pwd` >>& $LogFile
      echo "" >>& $LogFile
      echo "------------------------------------------------------------------------------" >>& $LogFile
      rm -f tmp Update.list
      touch tmp
      cvs update -A >>& tmp
      KillZombie
   else
      cd pact
      cvs update >>& ../tmp
      cd ..
   endif

   cat tmp >>& $LogFile
   awk '($1 ~ /^[UCMAR]$/) {print $1 "   " $2}' tmp >& Update.list
   if (-z Update.list ) then
      echo "UPDATE failed, NOTHING to update, Update.list is empty"
      echo "check sources in manager/import"
      echo "Entering inferior shell to correct sources"
      echo "Type exit at the prompt to resume"
      set here = $cwd
      csh -f
      echo ""
      cd $here
   endif
   echo "" >>& $LogFile

   set CONFS = "NO"
   grep "C " Update.list >& /dev/null
   if ($status == 0 ) then
      set CONFS = "YES"
      echo ""
      echo " *** UPDATE SOURCE CONFLICTS. ***"
      grep "C " Update.list
      echo "Entering DEBUG mode to correct sources"
   endif
   echo "done"  >>& $SCSLock

   if ($DEBUG == TRUE) then
      echo ""
      if ($CONFS == "YES") then
         echo "CONFLICT mode:"
         echo "   ** Open new logon window **"
         echo "   FIRST correct conflicting sources,"
         echo "   THEN check file Update.list in"
      else
         echo "CHECK mode:"
         echo "   ** Open new logon window **"
         echo "   Check file Update.list in"
      endif
      echo " manager/import/old to verify the files to commit"
      echo ""
      echo "Entering inferior shell for ACTION. When ready,"
      echo "type exit at the prompt to COMMIT sources."
      set here = $cwd
      csh -f
      echo ""
      cd $here
   endif
 
   rm -f tmp 
   echo "" >>& $LogFile

   echo -n "Committing the sources ..... "  >>& $SCSLock
   echo "Committing the new source"
   echo "Current Directory:  " `pwd`
   echo "cvs commit -m ***** Import from $CodeDate *****" 
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "" >>& $LogFile
   echo "Committing the new source" >>& $LogFile
   echo "Current Directory:  " `pwd` >>& $LogFile
   echo "cvs commit -m ***** Import from $CodeDate *****" >>& $LogFile
   echo "" >>& $LogFile
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "cvs commit -m Import from $CodeDate" >>& $LogFile
   cvs commit -m "***** Import from $CodeDate *****" >>& $LogFile
   set ErrStat = $status
   KillZombie
   if ($ErrStat != 0) then
      echo "Error status = " $Error
      echo "COMMIT failed, check sources in manager/import"
      echo "Entering inferior shell to correct sources"
      echo "Type exit at the prompt to resume"
      set here = $cwd
      csh -f
      echo ""
      cd $here
   endif
   echo "" >>& $LogFile
   echo "done"  >>& $SCSLock

   set TagID     = `/bin/date '+X%y_%m_%d_%H_%M_%S'`
   echo " "
   echo -n "Tagging the sources ........ "  >>& $SCSLock
   echo "Tagging this version with $TagID"
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "" >>& $LogFile
   echo "Tagging this version with $TagID" >>& $LogFile
   echo "Current Directory:  " `pwd` >>& $LogFile
   echo "" >>& $LogFile
   echo "------------------------------------------------------------------------------" >>& $LogFile
   echo "" >>& $LogFile

   echo "cvs rtag $TagID pact" >>& $LogFile
   cvs rtag $TagID pact >>& $LogFile
   KillZombie
   echo "done"  >>& $SCSLock


else

   echo "Unpacking new sources"
   echo "Unpacking new sources" >>& $LogFile
   tar xvf pact.tar  >>& $LogFile
   if ($status != 0) then
      echo "TAR extract failed, check sources in manager/import"
      exit(1)
   endif

   cd pact

   echo "Importing sources from $cwd into $Repository"
   echo "Importing sources from $cwd into $Repository" >>& $LogFile
   cvs import -m "Import from $CodeDate" $Repository v$CodeDate r$CodeDate >>& $LogFile
   set ErrStat = $status
   KillZombie
   if ($ErrStat != 0) then
      echo "IMPORT failed, check sources in manager/import"
      exit(1)
   endif

   cd ../../..

   echo "Merging sources"
   echo "Merging sources" >>& $LogFile

   cvs update -jv$CodeDate:yesterday -jv$CodeDate >>& $LogFile
   set ErrStat = $status
   KillZombie
   if ($ErrStat != 0) then
      echo "MERGE failed, check sources in manager/import"
      exit(1)
   endif

   cd manager

endif


cd $ImportingDir

rm -f $DateFile $TagFile
echo `date '+%D %T '` "PDT" > $DateFile
echo "$TagID" > $TagFile

echo "Cleaning up the import directory"
echo "Cleaning up the import directory" >>& $SCSLock
echo "Cleaning up the import directory" >>& $LogFile
rm -rf import >>& $LogFile

#
# release the lock
#
echo ""
echo "The Repository is Unlocked"
Unlock

echo ""
echo "Version $CodeDate imported"
echo "Date/Time: " `date`
echo "Version $CodeDate imported" >>& $LogFile
echo "Date/Time: " `date` >>& $LogFile

exit(0)
