home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2317 / autoftp30.sh next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  1990-12-28  |  5.6 KB  |  170 lines

  1. #! /bin/sh
  2. #
  3. # AutoFtp Version 3.0, by Mingqi Deng, July 6, 1989
  4. # -------------------------------------------------
  5. # Refer to file README for instructions to run the program
  6.  
  7. # Set-up: 
  8. #
  9. # 1. Set the following 3 parameters in this program on lines 23-62:
  10. #
  11. #              ALARM, RemoteHost, FtpLibDir 
  12. #
  13. #    Instructions and examples are given as comments in the following.
  14. #
  15. # 2. Make sure that you do NOT have any executable files of the same
  16. #    name as the following UNIX commands' names:
  17. #
  18. #        cat chmod cp echo expr grep mv rm sh sleep sed test
  19. #
  20. #    Otherwise, rename your files to different names.
  21.  
  22. # An ftp attempt will be aborted after ALARM many seconds. It must at 
  23. # least be equal to 300. The default # is 3600, eg., ALARM=3600 (do 
  24. # not leave blanks between '=' and '3600'!)
  25.  
  26. ALARM=3600
  27.  
  28. # Define host names:
  29. #   Use one of the following addresses for simtel20 archive site for the
  30. # parameter RemoteHost below:
  31. #         "26.2.0.74"
  32. #         "WSMR-SIMTEL20.ARMY.MIL"
  33. #         "SIMTEL20.ARPA"
  34. # The first one is highly recommended if it works, since it is the 
  35. # most time-saving address to use. The second is the official name. The
  36. # third is an alias, thus least recommended. However, you may find only
  37. # one of them works for you, depending on how your UNIX installation
  38. # handles the addresses. One symptom is a quickly failed ftp (less than
  39. # 5 seconds), which can be noticed by the ps command of UNIX.
  40. #
  41. # Also note that it has been noticed that some system does not recognize
  42. # the address in upper case. Therefore, if none of the above three
  43. # works for you, change the names to lower case and try again.
  44. #
  45. # LocalHost is set to "guest". If you find it not working, replace
  46. # it with the host name of your machine on which this program is run.
  47. #   eg.,     RemoteHost="26.2.0.74"
  48. #            LocalHost="shire.cs.psu.edu"
  49.  
  50. RemoteHost="26.2.0.74" 
  51. LocalHost="guest"
  52.  
  53. # Set the path name for the directory where three compiled C programs
  54. # ftpget.c, nextfile.c and checkout.c are. This program assumes as 
  55. # default that the three compiled programs are in $HOME/bin (a directory
  56. # "bin" in your home directory). If they are to be put in a different
  57. # directory, say, lib in your home directory, you should set FtpLibDir 
  58. # as:
  59. #        FtpLibDir="$HOME/lib"
  60. # Otherwise, just leave 'FtpLibDir=""' on next line as it is.
  61. FtpLibDir=""
  62.  
  63. #
  64. #-------AUTOFTP30.SH Set-up Stops Here: No further set-up needed------
  65. #
  66.  
  67. test 1$FtpLibDir = 1 && FtpLibDir=$HOME/bin
  68. _ftpget="$FtpLibDir/ftpget"
  69. _checkout="$FtpLibDir/checkout"
  70. _nextfile="$FtpLibDir/nextfile"
  71.  
  72. test -s "$_ftpget" ||
  73.    { echo "***Cannot find file '$_ftpget'!"
  74.      echo "   Please compile 'ftpget.c', name the compiled program as"
  75.      echo "   'ftpget' and place it in your '$FtpLibDir' directory." ; 
  76.      exit 2 ; }
  77. test -s "$_checkout" ||
  78.    { echo "***Cannot find file '$_checkout'!"
  79.      echo "   Please compile 'checkout.c', name the compiled program as"
  80.      echo "   'checkout' and place it in your '$FtpLibDir' directory." ;
  81.      exit 2 ; }
  82. test -s "$_nextfile" ||
  83.    { echo "***Cannot find file '$_nextfile'!"
  84.      echo "   Please compile 'nextfile.c', name the compiled program as"
  85.      echo "   'nextfile' and place it in your '$FtpLibDir' directory." ;
  86.      exit 2 ; }
  87.  
  88. test $# != 1 && 
  89.    { echo "***Usage: sh autoftp30.sh in_file" ; exit 2; }
  90.  
  91. exec 1>$$stdout 2>$$stderr
  92.  
  93. # Try FTP into simtel20 SLEEP many seconds until being connected
  94. # SLEEP will be increased and decreased randomly for subsequent tries.
  95. SLEEP=60
  96.  
  97. # save messages
  98. cat  $$stdout $$stderr > $$msg
  99.  
  100. #initialize the loop
  101. WS=`expr \( $SLEEP \* 47 + 31 \) \% 89 + 50`
  102.  
  103. ctr=1
  104. cp $1 $$input
  105. retry=no
  106.  
  107. # Repeat if there is more request in the input file or if the last
  108. # attempt failed and a retry can be performed.
  109. while { test -s $$input || test $retry = yes ; }
  110. do
  111.  
  112.   > $$stderr
  113.   > $$stdout
  114.  
  115. # If not a re-try for a requested file, then fetch next requested 
  116. # file name. If the fetch fails, then quit autoftp30.sh.
  117.   test $retry = no && 
  118.   { echo "%%%%%%% Process File Request #$ctr %%%%%%%" >> $$msg
  119.     ctr=`expr $ctr + 1`
  120.     $_nextfile $$input $RemoteHost anonymous $LocalHost $$ftp.script >> $$msg || 
  121.     { status=92; break;}
  122.   }
  123.  
  124. # Attempt to get the file. If not able to make an attempt, then quit.
  125. # Note that exit status=99 means there is no alarm call.
  126.   chmod og-rx $$ftp.script
  127.   $_ftpget $ALARM $$ftp.script ||
  128.   { test $? = 99 &&        # alarm called, do not quit yet, $_checkout 
  129.                # will respond 
  130.      { status=93; break; } ; }
  131.  
  132. # Check the result of the transfer. If a retry is not to be made, then 
  133. # set "retry" to no, otherwise set it to yes.
  134.   $_checkout $$stdout $$stderr $$ftp.script $$tmp >> $$msg
  135.   status=$?
  136.   { test $status = 99 && { status=94; break ; } ; } ||
  137.   { test $status = 0  && retry=no ; } || retry=yes
  138.  
  139. # if not the last request, i.e., $$input is not empty or retry=yes, 
  140. # then wait for a while before next attempt. Otherwise done!!!
  141.   test -s $$input || test $retry = yes || { status=95; break ; }
  142.  
  143. # save the messages for only the last attempt on a request
  144.   test $retry = no && cat  $$tmp >> $$msg
  145.   rm $$tmp
  146.  
  147.   sleep $WS
  148.   WS=`expr \( $WS \* 13 \/ 10 \) \% 900 + 60`
  149.  
  150. done
  151.  
  152. test $status -ge 92 && 
  153.    { test $status -le 93 &&  cat $$stderr >> $$msg || 
  154.         { cat $$tmp >> $$msg ; rm $$tmp ; }   # status=94,95
  155.    }
  156.   
  157. case $status in
  158.  92) echo "*******Fatal error: when preparing an ftp attempt!*******" >> $$msg;;
  159.  93) echo "*******Fatal error: during an ftp attempt!*******" >> $$msg;;
  160.  94) echo "*******Fatal error: when checking last ftp result!*******" >> $$msg;;
  161.  95) echo "###################Execution Completed###################" >> $$msg;;
  162.   *) ;;
  163. esac
  164.  
  165. #remove ftp command and working files
  166. rm $$ftp.script $$stderr $$stdout $$input
  167.  
  168. exit 0
  169.