#!/bin/csh
#>
#> PACT-TRANSFER - transfer the PACT distribution from one host to another
#>
#>  
#> Usage: pact-transfer -sh <src-host> -sd <src-directory> -dh <dst-host> -dd <dst-directory>
#>                      [-p] [-sp] [-dp] [-u]
#>  
#> Options: 
#>    -sh  Source host
#>    -sd  Source directory
#>    -su  Source user id (defaults to uid)
#>    -sp  Request source password
#>    -dh  Destination host
#>    -dd  Destination directory
#>    -du  Destination user id (defaults to anonymous
#>    -dp  Request destination password
#>    -p   Request password
#>    -u   Do not delete the the source files
#>  
#
# Modification History:
#   08-16-94 Jan Moura, LLNL:  Expanded prolog
####################
# Source Version: 2.0
# Software Release #92-0043
#
# include "cpyright.h"
#

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

set SrcHost = FALSE
set DstHost = FALSE
set SrcDir  = FALSE
set DstDir  = FALSE
set DelDist = TRUE
set help    = FALSE
set LogFile = ~/transfer.log
set SUser   = $USER
set SPasswd = ""
set DUser   = "anonymous"
set DPasswd = "$SUser@$DstHost"
set Pattern = 'pact.README pact*-src pact*.tar.gz'

while ($#argv >= 1)
   switch ($1)
      case -dd:
	       shift
               set DstDir = $1
               breaksw
      case -dh:
	       shift
               set DstHost = $1
               breaksw
      case -du:
	       shift
               set DUser = $1
               breaksw
      case -dp:
               echo -n "Destination Password: "
               stty -echo
               set DPasswd = $<
               stty echo
               echo ""
               breaksw
      case -sd:
	       shift
               set SrcDir = $1
               breaksw
      case -sh:
	       shift
               set SrcHost = $1
               breaksw
      case -su:
	       shift
               set SUser = $1
               breaksw
      case -sp:
               echo -n "Source Password: "
               stty -echo
               set SPasswd = $<
               stty echo
               echo ""
               breaksw
      case -p:
               echo -n "Password: "
               stty -echo
               set Passwd = $<
               stty echo
               echo ""
               set SPasswd = $Passwd
               set DPasswd = $Passwd
	       set Passwd = ""
               breaksw
      case -u:
               set DelDist = FALSE
               breaksw
      default:
               set help = TRUE
               breaksw
   endsw
   shift
end

if (($DstDir == "FALSE") || ($DstHost == "FALSE") || ($SrcDir == "FALSE") || ($SrcHost == "FALSE") || ($help == "TRUE")) then
   echo " "
   echo "Usage: pact-transfer -sh <src-host> -sd <src-directory> -dh <dst-host> -dd <dst-directory>"
   echo "                     [-p] [-sp] [-dp] [-u]"
   echo " "
   echo "Options: "
   echo "   -sh  Source host"
   echo "   -sd  Source directory"
   echo "   -su  Source user id (defaults to uid)"
   echo "   -sp  Request source password"
   echo "   -dh  Destination host"
   echo "   -dd  Destination directory"
   echo "   -du  Destination user id (defaults to anonymous"
   echo "   -dp  Request destination password"
   echo "   -p   Request password"
   echo "   -u   Do not delete the the source files"
   echo " "
   exit(1)
endif

rm -f $LogFile
echo "Transferring PACT distribution" > $LogFile

(echo "open $SrcHost" ; \
 echo "user $SUser $SPasswd\" ; \
 echo "prompt" ; \
 echo "bin" ; \
 echo "cd $SrcDir" ; \
 echo "proxy open $DstHost" ; \
 echo "proxy user $DUser $DPasswd" ; \
 echo "proxy bin" ; \
 echo "proxy cd $DstDir" ; \
 echo "proxy mdele $Pattern" ; \
 echo "proxy mget $Pattern" ; \
 echo "proxy close" ; \
 if ($DelDist == "TRUE") echo "$Cmmd""mdele $Pattern" ; \
 echo "quit" ) | ftp -n

set SPasswd = ""
set DPasswd = ""

# echo the FTP command into the log
echo "" >>& $LogFile
echo "FTP Commands for Transfer" >>& $LogFile
(echo "   open $SrcHost" ; \
 echo "   user $SUser xxxxx\" ; \
 echo "   prompt" ; \
 echo "   bin" ; \
 echo "   cd $SrcDir" ; \
 echo "   proxy open $DstHost" ; \
 echo "   proxy user $DUser xxxxx" ; \
 echo "   proxy bin" ; \
 echo "   proxy cd $DstDir" ; \
 echo "   proxy mdele $Pattern" ; \
 echo "   proxy mget $Pattern" ; \
 echo "   proxy close" ; \
 if ($DelDist == "TRUE") echo "   $Cmmd""mdele $Pattern" ; \
 echo "   quit" ) >>& $LogFile

echo "" >>& $LogFile

exit($status)

