#!/bin/csh
#
# PACT-UPDATE - update/install the PACT distribution system on this machine
# (PACT-SRC)    Some of the complexity of this process stems from the fact
#               that there are several ways of obtaining the source:
#                -  from a CVS repository
#                -  from an anonymous FTP node as a distribution tar file
#                -  from an executable PACT distribution file. PACT-SRC
#               and that the other parts of the manager interlock with
#               this script
#
# Usage: (see the help package below)
#
####################
# Source Version: 2.0
# Software Release #92-0043
#
# include "cpyright.h"
#
  goto Begin

#................................................... help package
HelpPackage:
  echo ""
  echo "Purpose:"
  echo "     update/install the PACT distribution system on this machine"
  echo "     (also runs as first part of a PACT-SRC file)"
  echo ""
  echo "Usage:"
  echo "       pact-update [-b <mail-dest>] [-c] [-f <directory>]"
  echo "                   [-i <directory>] [-k]"
  echo "                   [+-n <host>] | [-s] | [-t <tar-file>] [-x]"
  echo "                   <config>"
  echo "       pact-update -h | -v"
  echo ""
  echo "Options:"
  echo "       -b          - mail destination for background compilation"
  echo "       -c          - stop after configuring the distribution"
  echo "       -f          - get configuration file from named directory"
  echo "       -i          - base installation directory (default /usr/local)"
  echo "       -k          - keep existing PACT distribution, ie. keep 'pact/'"
  echo "                     in directory where new distribution being built"
  echo "          -- specify origin of source --"
  echo "       -n          - get the PACT source from the specified host"
  echo "                   - via anonymous FTP without the documentation"
  echo "       +n          - get the PACT source from the specified host"
  echo "                   - via anonymous FTP with the documenation"
  echo "       -s          - sources are out and only need to be configured"
  echo "                     (default for command pact-update"
  echo "       NOTE: default for command pact-src is to extract compressed tar"
  echo "                     file from end of pact-src script."
  echo "       -t          - compressed or uncompressed tar file, example"
  echo "                     for pact.tar.gz type:   -t pact.tar"
  echo "       -x          - setup/extract sources only"
  echo ""
  echo "       <config>    - system to configure the installation for"
  echo "                     (auto = detects the system configuration)"
  echo ""      
  echo "       -h          - print this help package"
  echo "       -v          - only print the PACT Version (No update)"
  echo ""
  exit(1)

#---------------------------------- Execution Entry 
Begin:

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

# don't assume the umask is reasonable - set it for PACT's needs
umask 002

unalias cd

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

set Base       = `pwd | sed "s|$RealHome|$home|"`
set LogFile    = $Base/Build.Log
set KEEP       = FALSE
set BACKGR     = FALSE
set HELP       = FALSE
set SaveDocs   = FALSE
set Manager    = manager
set ConfigDir  = ""
set ConfigOnly = FALSE
set Host       = ""
set MailAddr   = $USER
set Nodename   = `hostname`
set Pattern    = 'pact.README pact*-src pact*.tar.gz'
set InstArg    = ""
set WhoIAm     = $USER
set SrcOnly    = FALSE

set Name = $0
set Name = $Name:t

if ($Name == "pact-update") then
   set SrcMode   = "cvs"
else
   set SrcMode   = "self"
endif

# Source Control System variables
set CheckOut  = "cvs co"
set ADM       = "CVS"

# --------------------------------------------------------------------------

#                       HANDLE COMMAND LINE ARGUMENTS

# --------------------------------------------------------------------------
set Opt = "$Name  $argv"

if ($#argv < 1) then
   set HELP = TRUE
else if ("$1" == "-h" || "$1" == "-help" ||"$1" == "help") then
   set HELP = TRUE
endif
if ("$1" == "-v") then
   if (-e ../score/score.h) then
      set Vers = `grep "define VERSION" ../score/score.h`
      if ($#Vers > 0) then
         echo "PACT Version:" $Vers[4]
      endif
   else
      echo "Version unknown"
   endif
   exit(0)
endif

while ("$1" =~ -*)
   switch ($1)
      case -b:
               shift
               set BACKGR = TRUE
               set MailAddr = $1
               breaksw
      case -c:
               set ConfigOnly = TRUE
               breaksw
      case -f:
               shift
               set ConfigDir = $1
               breaksw
      case -i:
               shift
               set InstArg = "-i $1"
               breaksw
      case -k:
               set KEEP = TRUE
               breaksw
      case +n:
               set SaveDocs = TRUE
      case -n:
               shift
               set Host    = $1
               set SrcMode = "net"
               breaksw
      case -s:
               set SrcMode = "source"
               set KEEP = TRUE
               breaksw
      case -t:
               shift
               set TarFile = $1
               set SrcMode = "tar"
               breaksw
      case -x:
               set SrcOnly = TRUE
               breaksw
   endsw
   shift
end

if ($#argv < 1 || $HELP == "TRUE" ) then
   goto HelpPackage
endif

set Configuration = $1

unalias cd

rm -f $LogFile
touch $LogFile
echo "Operation:  $Opt "          >>& $LogFile
echo "Update PACT installation on $Nodename" >>& $LogFile
echo ""				  >>& $LogFile
echo "Update Log		Host: `uname -n` running `uname`" >>&! $LogFile
echo "User: $WhoIAm		Date: `date`" >>& $LogFile
echo ""				  >>& $LogFile
echo ""				  >>& $LogFile

# --------------------------------------------------------------------------

#                                 OBTAIN SOURCES

# --------------------------------------------------------------------------

switch ($SrcMode)

# sources are out and ready
   case source:
        breaksw

# cvs repository based update
   case cvs:
        groups | grep dpact > /dev/null
        if ($status == 1) then
           echo ""
           echo "You must be in the PACT developers group to access the repository"
           echo ""
           exit(1)
        endif
        breaksw

# anonymous ftp based update
   case net:
        echo "Getting the PACT sources from $Host"
        echo "Getting the PACT sources from $Host" >>& $LogFile

# echo the FTP command into the log
        echo "" >>& $LogFile
        echo "FTP Commands for Update" >>& $LogFile
        (echo "   open $Host" ; \
         echo "   user anonymous password $USER@$Nodename" ; \
         echo "   prompt" ; \
         echo "   bin" ; \
         echo "   cd pub" ; \
         echo "   mget $Pattern" ; \
         echo "   quit" ) >>& $LogFile
        echo "" >>& $LogFile

# do the transfer
        (echo "open $Host" ; \
         echo "user anonymous password $USER@$Nodename" ; \
         echo "prompt" ; \
         echo "bin" ; \
         echo "cd pub" ; \
         echo "mget $Pattern" ; \
         echo "quit" ) | ftp -n >>& $LogFile

        touch pact-foo-doc.tar.gz >>& $LogFile
        if ($SaveDocs == TRUE) then
           foreach i (pact*-doc.tar.gz)
              if ($i == pact-foo-doc.tar.gz) then
                 rm $i >>& $LogFile
              else
                 gzip -d $i >>& $LogFile
              endif
           end
        else
           rm -f pact*-doc.tar.gz >>& $LogFile
        endif

        set Name = ""
        touch pact-foo-src >>& $LogFile
        foreach i (pact*-src)
          if ($i != pact-foo-src) then
             set Name = $i >>& $LogFile
          else
             rm $i >>& $LogFile
          endif
        end
        if ($Name == "") then
           echo "FTP failed to find PACT sources on $Host" >>& $LogFile
           exit(1)
        endif

        if (-e pact/manager/.pact-date) then
           set OldDate = `cat pact/manager/.pact-date`
           set NewDate = $Name
           pact/manager/compare-date $NewDate $OldDate >>& $LogFile
           switch ($status)
              case 1:
                     echo "Local PACT distribution is out of date"
                     echo "Local PACT distribution is out of date" >>& $LogFile
                     breaksw
              case 0:
              case -1:
                     echo "Local PACT distribution is up to date"
                     echo "Local PACT distribution is up to date" >>& $LogFile
                     rm -f pact*.tar*
                     exit(0)
                     breaksw
           endsw
        endif

# self extraction update
   case self:
        set STDOUT = extr.tmp
        rm -f $STDOUT
        touch $STDOUT

# check if grep supports -a option (-a: process binary files as well as text files, necessary on some Linux systems)
        set GrepArgs = '-a'
        mkdir futmp
        cd futmp >>& /dev/null
        echo "foo" > file.tmp
        grep $GrepArgs foo file.tmp >>& /dev/null
        if ($status != 0) then
           set GrepArgs = ""
        endif
        cd .. >>& /dev/null
        rm -r futmp

# determine where pact.tar begins
# the >& /dev/null following is to throw away complaints from grep about line being too long
        (grep $GrepArgs -n  '\-----------------------\*\*\*   SEPARATOR   \*\*\*-----------------------' ./$Name > ln_.tmp) >& /dev/null
        set nlines = `grep -v echo ln_.tmp | awk -F: '{print $1}'`
        rm ln_.tmp

# write the shell script to extract the compressed tar file
        echo "#\!/bin/csh -f" >>& $STDOUT
        echo "@ nlines = $nlines" >>& $STDOUT
        echo "@ rlines = 0" >>& $STDOUT
        echo 'while (1)' >>& $STDOUT
        echo '   set Input = "$<"' >>& $STDOUT
        echo '   @ rlines += 1' >>& $STDOUT 
        echo '   if ("$rlines" == "$nlines") then' >>& $STDOUT
        echo '      set File = "$<"' >>& $STDOUT
        echo '      break' >>& $STDOUT
        echo '   endif' >>& $STDOUT
        echo 'end' >>& $STDOUT
        echo 'if ($?File) then' >>& $STDOUT
        echo '   set File = $File:t' >>& $STDOUT
        echo '   echo $File' >>& $STDOUT
        echo '   cat > $File' >>& $STDOUT
        echo 'endif' >>& $STDOUT
        echo 'exit($status)' >>& $STDOUT

        chmod a+x $STDOUT

# extract the tar file
        set TarFile = `cat ./$Name | ./$STDOUT`
        set TarFile = $TarFile:r
        set SrcMode = "tar"

        rm -f $STDOUT
        breaksw

# error
   default:
        echo ""
        echo "I have no idea how to do an update without a tar file, a"
        echo "CVS repository, or a network node."
        echo ""
        exit(1)
        breaksw
endsw

# --------------------------------------------------------------------------

#                                 SETUP SOURCES

# --------------------------------------------------------------------------

if ($KEEP == "FALSE") then
   if (-d pact) then
      echo "Removing old PACT files"
      echo "Removing old PACT files" >>& $LogFile
      rm -r pact
   endif
endif

switch ($SrcMode)
   case tar:
        echo "Unpacking the PACT sources"
        echo "Unpacking the PACT sources" >>& $LogFile

        if (-e $TarFile.gz) then
           gzip -d $TarFile.gz
        endif
	if (-e $TarFile.Z) then
	   uncompress $TarFile.Z
	endif

# determine whether local tar supports o option
        set TarArgs = xvmof
        mkdir futmp
        cd futmp >>& /dev/null
        echo "foo" > file.tmp
        tar cvf tmp.tar file.tmp >>& /dev/null
        tar $TarArgs tmp.tar >>& /dev/null
        if ($status != 0) then
           set TarArgs = xvmf
        endif
        cd .. >>& /dev/null
        rm -r futmp

        tar $TarArgs $TarFile >>& $LogFile
        if ($status != 0) then
           echo ""
           echo "  ERROR: TAR failed"
           echo "" >>& $LogFile
           echo "  ERROR: TAR failed" >>& $LogFile
           echo ""
           exit($status)
        endif

        cd pact

        echo "PACT sources unpacked"
        echo "PACT sources unpacked" >>& $LogFile
        breaksw

   case source:
        cd pact
        breaksw

   default:

        echo "Checking out the PACT Sources"
        echo "Checking out the PACT Sources" >>& $LogFile

        $CheckOut pact >>& $LogFile
        if ($status != 0) then
           echo ""
           echo "  ERROR: Source Control System failed"
           echo "" >>& $LogFile
           echo "  ERROR: Source Control System failed" >>& $LogFile
           echo ""
           exit($status)
        endif

# delete an unnecessary directory layer */
        mv pact/* .
        rmdir pact

# remove the $SCSName sub-directories
        echo "Removing Source Control Information"
        echo "Removing Source Control Information" >>& $LogFile
        set List = `find . -name $ADM -print`
        foreach i ($List)
           rm -r $i
        end

        echo "PACT sources checked out"
        echo "PACT sources checked out" >>& $LogFile
        breaksw
endsw

if ($SrcOnly == TRUE) then
   exit(0)
endif

echo "Current directory after source setup $cwd" >>& $LogFile

# --------------------------------------------------------------------------

#                              CONFIGURE DISTRIBUTION

# --------------------------------------------------------------------------

if ($ConfigDir != "") then
   set Configure = "-f $ConfigDir"
else
   set Configure = ""
endif

echo "Changing to manager directory: $Manager"
echo "Changing to manager directory: $Manager" >>& $LogFile
cd $Manager >>& /dev/null

set System    = `./system-id`
set EnvFile   = "env-$System"

echo "Configuring PACT for $Configuration"
echo "Configuring PACT for $Configuration" >>& $LogFile
if ($Configuration == "auto") then
   ./pact-config -n $InstArg >>& $LogFile
   set xstatus = $status
else
   ./pact-config $InstArg $Configure $Configuration >>& $LogFile
   set xstatus = $status
endif

if ($xstatus != 0) then
   echo ""
   echo "  ERROR: PACT-CONFIG failed"
   echo "" >>& $LogFile
   echo "  ERROR: PACT-CONFIG failed" >>& $LogFile
   echo ""
   exit($xstatus)
endif

if ($ConfigOnly == TRUE) then
   exit(0)
endif

echo "Sourcing environment file $EnvFile for $System"
echo "Sourcing environment file $EnvFile for $System" >>& $LogFile
if (-e $EnvFile) then
   source $EnvFile
   rehash
else
   echo ""
   echo "  ERROR: bad environment file"
   echo ""
   echo "" >>& $LogFile
   echo "  ERROR: bad environment file" >>& $LogFile
   echo "" >>& $LogFile
   exit(1)
endif

# --------------------------------------------------------------------------

#                               COMPILE DISTRIBUTION

# --------------------------------------------------------------------------

echo "Compiling the PACT distribution"
echo "Compiling the PACT distribution" >>& $LogFile

##########set DName = ("pact"`./code-date -u -f ymd`"-src")
set DName = `cat ./.pact-date`

if ($BACKGR == "FALSE") then

   pact update >>& $LogFile
   set upresult = $status
   echo "Host: `uname -n`" >>& $LogFile
   echo "Date: `date`" >>& $LogFile

   if ($upresult != 0) then
      echo ""
      echo "  ERROR: Make PACT failed"
      echo "" >>& $LogFile
      echo "  ERROR: Make PACT failed" >>& $LogFile
      echo ""
      echo "See the log file:  $LogFile"
      exit($status)
   endif

   tail -8 $LogFile
   echo "Compilation complete"
   echo "Compilation complete" >>& $LogFile

   rm -f .pact-date
   echo $DName 
   echo $DName > .pact-date
   echo $DName >>& $LogFile
   mv $LogFile ../../$Nodename.BUILD
   echo "See the log file " ../../$Nodename.BUILD

else
   echo "   Backgrounding the compilation, will advise via mail"
   echo "   Backgrounding the compilation, will advise via mail" >>& $LogFile
   (pact update >>& $LogFile ; \
    echo "Host: `uname -n`" >>& $LogFile ; \
    echo "Date: `date`" >>& $LogFile ; \
    rm -f .pact-date ; \
    echo $DName > .pact-date ; \
    tail -60 $LogFile | mail $MailAddr ; \
    mv $LogFile ../../$Nodename.BUILD) &
   nohup
endif

# --------------------------------------------------------------------------
# --------------------------------------------------------------------------

cd $Base >>& /dev/null
echo ""

exit($status)
