#!/bin/csh -f
#
#Sorry Mike (or other maintainer)
#
#but this is the first time I have added something.
#
#This is a super awesome unix ftp script (IMHO).
#
#I know you guys like people to use gopher and other
#things but I found this to be really slick.
#
#I hope I put this in the right place and have given
#you enough info.
#
#Thanks!
#
#-Jack
#
#
#/---------------------------------------------------------\
#|  Jack W. Judy                  Office: (510) 642-1010   |
#|  Electrical Engineering        FAX:    (510) 643-6637   |
#|  497 Cory Hall (BSAC)          Lab:    (510) 642-2716   |
#|  University of California      Home:   (510) 849-2604   |
#|  Berkeley, CA  94720           email:  [email protected]  |
#\---------------------------------------------------------/
#
# ftpumich
#
# This script was written for all those people frustrated with the limited
# number of guest logins at the popular umich Mac archive.  It skips from
# mirror to mirror until it succeeds. It always works for me. NO MORE WAITING.
#
# Comments and suggestions/improvements are welcome.
#
# Written by Jack Judy <[email protected]> 12-21-93
#

# Some variables you may want to tweek to your liking.
@ sleep_time = 0
@ maxcount = 1
@ VERBOSE = 1

if ( $#argv == 0 || $#argv > 1 ) then
       if ( $VERBOSE == 1 ) then
               echo ""
               echo "usage:    ftpumich [file_containing_compete_pathnames]"
               echo "usage:    ftpumich"
               echo ""
               echo "The file (file_containing_compete_pathnames) holds the COMPLETE"
               echo "pathnames of the files you want to 'ftp'.  The pathname format"
               echo "of the files is that of the awesome archive at archive.umich.edu."
               echo ""
               echo "Example:"
               echo ""
               echo "[email protected] 22> cat new_umich_stuff"
               echo "/mac/00help/newthings.txt"
               echo "/mac/00ls-lRfile"
               echo "[email protected] 23> ftpumich new_umich_stuff"
               echo ""
               echo "The order in which the ftp sites/mirrors are attempted"
               echo "is given below.  The first ftp site is choosen to be the"
               echo "uiowa site because things are not binhexed. Saves work."
               echo ""
               echo "  grind.isca.uiowa.edu    in mac/umich (no .hqx)"
               echo "  archive.umich.edu       in mac"
               echo "  wuarchive.wustl.edu     in mirrors/archive.umich.edu/mac"
               echo "  archive.orst.edu        in pub/mirrors/archive.umich.edu/mac"
               echo "  archie.au               in micros/mac/umich"
               echo "  nic.switch.ch           in mirror/umich-mac"
               echo "  src.doc.ic.ac.uk        in packages/mac/umich (with .gz)"
               echo "  ftp.sunet.se            in /pub/mac/mirror-umich"
               echo "  ftp.inter.spin.ad.jp    in pub/Mac/Merit.mirror"
               echo "  ftp.technion.ac.il      in pub/unsupported/mac/umich"
               echo "  nctuccca.edu.tw         in /Macintosh/umich-mac"
               echo "  anl.anl.fr              in /pub/mac/umich (with .z)"
               echo ""

               echo "If you pass no arguments then the files"
               echo "  /mac/00help/newthings.txt"
               echo "  /mac/00ls-lRfile"
               echo "will be transfered so that you may browse them for the files"
               echo "you wish to download."
               echo ""
               echo "A file called newthings.txt.greped will be created that holds"
               echo "only the complete pathnames for the newest files in the archive."
               echo ""
               echo "The easiest way to download files is to edit this file (newthings.txt.greped)"
               echo "and delete all the lines except for the files you wish to download."
               echo "Then simply rerun ftpumich with this edited file as the argument."
               echo "Presto.  All the files will be obtained .. every time (practically)."
               echo ""
               echo "This program creates ~/.netrc macros.  Each macro can't exceed 4k"
               echo "which is just less than attempting to ftp all the new files."
               echo "The ~/.netrc file is used and so a backup copy is saved as"
               echo "~/.netrc.ftpumich incase there is some sort of problem and"
               echo "your ~/.netrc file is not restored.  Re-running this script with"
               echo "will not copy of the backup but instead it will restore the"
               echo "original ~/.netrc file."
               echo ""
               echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
               echo ""
       endif

       if ( $#argv > 1 ) then
               exit 1
       endif
endif

if ( $#argv == 0 ) then
       echo "/mac/00help/newthings.txt">       ftpumich.tmp
       echo "/mac/00ls-lRfile"         >>      ftpumich.tmp
       set file = ftpumich.tmp
       if ( -f newthings.txt ) then
               rm -f newthings.txt
       endif
       if ( -f 00ls-lRfile ) then
               rm -f 00ls-lRfile
       endif
else
       set file = $1
endif

echo ""
echo "-----------------------------------------------------------------"
echo "Now attmpting to ftp the following files:"
cat $file
echo "-----------------------------------------------------------------"
echo ""

if ( ! (-f ~/.netrc.ftpumich) && -f ~/.netrc ) then
       mv ~/.netrc ~/.netrc.ftpumich
endif

foreach machine ( grind.isca.uiowa.edu archive.umich.edu mirror.archive.umich.edu wuarchive.wustl.edu archive.orst.edu nic.switch.ch ftp.sunet.se ftp.inter.spin.ad.jp archie.au src.doc.ic.ac.uk anl.anl.fr ftp.technion.ac.il nctuccca.edu.tw )
       @ count = 0

       echo "machine $machine" > ~/.netrc
       echo "login anonymous" >> ~/.netrc
       if ( -f ~/.forward ) then
               echo "password `cat ~/.forward`" >> ~/.netrc
       else
               echo "password `echo $user`@`hostname`" >> ~/.netrc
       endif
       echo "macdef init" >> ~/.netrc
       echo "bin" >> ~/.netrc
       echo "hash" >> ~/.netrc
       echo "prompt" >> ~/.netrc

# Defaults:
       set hqx = .hqx
       set suffix = ""

       switch ($machine)
       case    grind.isca.uiowa.edu:
                       set dir = /mac/umich
                       set hqx =
                       breaksw
       case    archive.umich.edu:
                       set dir = /mac
                       breaksw
       case    mirror.archive.umich.edu:
                       set dir = /pub/mirrors/archive.umich.edu/mac
                       breaksw
       case    wuarchive.wustl.edu:
                       set dir = /mirrors/archive.umich.edu/mac
                       breaksw
       case    archive.orst.edu:
                       set dir = pub/mirrors/archive.umich.edu/mac
                       breaksw
       case    nic.switch.ch:
                       set dir = mirror/umich-mac
                       breaksw
       case    ftp.sunet.se:
                       set dir = /pub/mac/mirror-umich
                       breaksw
       case    ftp.inter.spin.ad.jp:
                       set dir = pub/Mac/Merit.mirror
                       breaksw
       case    archie.au:
                       set dir = /micros/mac/umich
                       breaksw
       case    ftp.technion.ac.il:
                       set dir = pub/unsupported/mac/umich
                       breaksw
       case    src.doc.ic.ac.uk:
                       set dir = /packages/mac/umich
                       set suffix = ".gz"
                       breaksw
       case    anl.anl.fr:
                       set dir = /pub/mac/umich
                       set suffix = ".z"
                       breaksw
       case    nctuccca.edu.tw:
                       set dir = /Macintosh/umich-mac
                       breaksw
       endsw
       echo "### /;^) Attmepting to connect to $machine /;^) ###"

       echo ""
       echo "cd $dir" >> ~/.netrc
       cat $file | sed -e 's/^\/mac\///' | sed -e 's/.hqx//' | awk -F/ '{print "get", $0"'$hqx$suffix'", $NF"'$hqx$suffix'" }' >> ~/.netrc
       endif
       echo "quit" >> ~/.netrc
       echo "" >> ~/.netrc
       chmod 600 ~/.netrc

       set length_before = `/bin/ls -l | wc -l`

       while ( `/bin/ls -l | wc -l` <= $length_before && $count < $maxcount )
               ( echo "quit" ) | ftp $machine
               if ( `/bin/ls -l | wc -l` == $length_before ) then
                       @ count ++
                       echo ""
                       echo "### /:^( Attempt #"$count" of $maxcount to connect to $machine has failed. /:^( ###"
                       echo ""
                       echo "-----------------------------------------------------------------"
                       echo ""
                       sleep $sleep_time
               else
                       echo ""
                       echo "-----------------------------------------------------------------"
                       echo ""
                       @ new = `/bin/ls -l | wc -l` - $length_before
                       echo "Finished ftp'ing (Successfully I think. Number of new files = ($new)"
                       echo ""
                       mv ~/.netrc.ftpumich ~/.netrc
                       chmod 600 ~/.netrc
                       rm -f ftpumich.tmp
                       if ( $#argv == 0 ) then
                               if ( -f newthings.txt.gz ) then
                                       unzip newthings.txt.gz
                               endif
                               if ( -f newthings.txt.Z ) then
                                       uncompress newthings.txt.Z
                               endif
                               cat newthings.txt | grep "^\/mac\/" > newthings.txt.greped
                               echo ""
                               echo "Please edit the file called (newthings.txt.greped) and delete the lines"
                               echo "that contain filenames you do not wish to ftp."
                               compress 00ls-lRfile
                               echo ""
                       endif
                       exit 0
               endif
       end

end

echo "All attempts to ftp have failed."
mv ~/.netrc.ftpumich ~/.netrc
chmod 600 ~/.netrc
rm -f ftpumich.tmp