#!/bin/csh
#>
#> PACT-SEND - transport a distribution to the named host
#>  
#> Usage: pact-send -d <directory> -dh <host> [-h] [-b] [-e] [-o] [-u] [-m]
#>  
#> Options: 
#>    -d  Destination directory
#>    -e  Export distribution for source repository only
#>    -dh Destination host
#>    -h  Help Package
#>    -m  Send a MAC distribution
#>    -o  Omit the documents
#>    -r  Omit the README files
#>    -u  Use the distribution that is already out
#> 
# Source Version: 2.0
# Software Release #92-0043
#
# include "cpyright.h"
#

unalias cd

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

set Anonymous  = FALSE
set PACTDir    = `pwd | sed "s|$RealHome|$home|"`
set LogFile    = $PACTDir/send.log
set UNIX       = "TRUE"
set MAC        = "FALSE"
set Dest       = "FALSE"
set DestDir    = "FALSE"
set Docs       = "-t"
set help       = "FALSE"
set GetDist    = "TRUE"
set Passwd     = ""
set RMFiles    = "YES"
set Pattern    = ""

set Export     = ""

while ($#argv >= 1)
   switch ($1)
      case -a:
	       set Anonymous = TRUE
	       breaksw
      case -d:
	       shift
               set DestDir = $1
               breaksw
      case -e:
               set Export = "-r"
               set Pattern = "e"
               breaksw
      case -g:
               echo -n "Password: "
               stty -echo
               set Passwd = $<
               stty echo
               echo ""
               breaksw
      case -dh:
	       shift
               set Dest = $1
               breaksw
      case -h:
               set help = TRUE
               breaksw
      case -m:
               set MAC = "TRUE"
               breaksw
      case -o:
               set Docs = ""
               breaksw

      case -r:
               set RMFiles = ""
               breaksw
      case -u:
               set GetDist = FALSE
               breaksw
      default:
               set help = TRUE
               breaksw
   endsw
   shift
end

if (($DestDir == "FALSE") || ($Dest == "FALSE") || ($help == "TRUE")) then
   echo " "
   echo "Usage: pact-send [-a] [-b] -d <directory> -dh <host> [-h] [-e] [-o] [-u] [-m]"
   echo " "
   echo "Options: "
   echo "   -a  Use anonymous FTP"
   echo "   -d  Destination directory"
   echo "   -e  Export distribution for repository only"
   echo "   -dh Destination host"
   echo "   -h  Help Package"
   echo "   -m  Prepare a MAC distribution as well"
   echo "   -o  Omit the documents"
   echo "   -r  Omit the README files"
   echo "   -u  Use the distribution that is already out"
   echo " "
   exit(1)
endif

# make the distribution files
cd $PACTDir
rm -f $LogFile
echo "Sending PACT distribution to $Dest (see send.log for details)"
echo "Sending PACT distribution to $Dest" > $LogFile

if ($GetDist == "TRUE") then
   touch pactfoo-src
   rm -f pact*-src pact*mac.tar.gz pact*doc.tar.gz pact-*README
   echo "Building PACT Unix distribution (see distrib.log for details)"
   echo "Building PACT Unix distribution (see distrib.log for details)" >>& $LogFile
   echo "pact-dist $Docs $Export" >>& $LogFile

   ./pact-dist $Docs $Export >>& $LogFile

   if ($status != 0) then
      echo "Problem in pact-dist - Abort"
      echo "Problem in pact-dist - Abort" >>& $LogFile
      exit(1)
   endif

   if ($MAC == "TRUE") then
      echo "" >>& $LogFile
      echo "Building PACT MAC distribution (see distrib.log for details)"
      echo "Building PACT MAC distribution (see distrib.log for details)" >>& $LogFile
      echo "pact-dist -m" >>& $LogFile

      ./pact-dist -m >>& $LogFile

      if ($status != 0) then
         echo "Problem in pact-dist - Abort"
         echo "Problem in pact-dist - Abort" >>& $LogFile
         exit(1)
      endif
   endif
endif

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

if ($Anonymous == TRUE) then
   set Who = "anonymous"
   set Passwd = "$USER"@`uname -n`
else
   set Who = $USER
endif

# do the transfer
if ($UNIX == "TRUE") then
   if ($Pattern == "") then
      set Pattern    = 'pact.FAQ pact*-src pact*doc.tar.gz'
   else
      set Pattern = 'pact*.tar.gz'
   endif
   if ($RMFiles == "YES") then
      cp README pact-unix.README
      set Pattern = ($Pattern pact-unix.README)
   endif
   chmod 664 $Pattern
   chmod 775 pact*-src
   echo "" >>& $LogFile
   echo "Doing the transfer via FTP" >>& $LogFile
   (echo "open $Dest" ; \
    echo "user $Who $Passwd" ; \
    echo "prompt" ; \
    echo "bin" ; \
    echo "cd $DestDir" ; \
    echo "mdele $Pattern" ; \
    echo "mput $Pattern" ; \
    echo "quit" ) | ftp -n >>& $LogFile
   echo "Transfer done" >>& $LogFile
   
# echo the FTP commands into the log
   echo "" >>& $LogFile
   echo "FTP Commands" >>& $LogFile
   (echo "   open $Dest" ; \
    echo "   user $Who xxxxx" ; \
    echo "   prompt" ; \
    echo "   bin" ; \
    echo "   cd $DestDir" ; \
    echo "   mdele $Pattern" ; \
    echo "   mput $Pattern" ; \
    echo "   quit" ) >>& $LogFile
   echo "" >>& $LogFile
   if ($RMFiles == "YES") then
      rm pact-unix.README
   endif
endif

if ($MAC == "TRUE") then
   set MacPattern = 'pact.FAQ pact*mac.tar.gz pact*doc.tar.gz'
   if ($RMFiles == "YES") then
      cp mac/README.MAC pact-mac.README
      set MacPattern = ($MacPattern pact-mac.README)
   endif
   chmod 664 $MacPattern
   echo "" >>& $LogFile
   echo "Doing the transfer" >>& $LogFile
   (echo "open $Dest" ; \
    echo "user $Who $Passwd" ; \
    echo "prompt" ; \
    echo "bin" ; \
    echo "cd $DestDir" ; \
    echo "mdele $MacPattern" ; \
    echo "mput $MacPattern" ; \
    echo "quit" ) | ftp -n >>& $LogFile
   echo "Transfer done" >>& $LogFile

# echo the FTP commands into the log
   echo "" >>& $LogFile
   echo "FTP Commands" >>& $LogFile
   (echo "   open $Dest" ; \
    echo "   user $Who xxxxx" ; \
    echo "   prompt" ; \
    echo "   bin" ; \
    echo "   cd $DestDir" ; \
    echo "   mdele $MacPattern" ; \
    echo "   mput $MacPattern" ; \
    echo "   quit" ) >>& $LogFile
   echo "" >>& $LogFile
   if ($RMFiles == "YES") then
      rm pact-mac.README
   endif
endif

set Passwd = ""

echo "PACT distribution sent to $Dest"
echo ""
echo "PACT distribution sent to $Dest" >>& $LogFile
echo "" >>& $LogFile

exit($status)

