#!/bin/sh

###########################################################################
# get -- Get files from MS-DOS archive site.                    Version 1.2
#
# call: get [site] <directory> <file...> <directory> <file...> ...
#
# Wildcard file specifications are supported, but must be quoted so that the
# calling shell doesn't try to expand them.  All parameters are basenamed and
# shifted to lower case, to make it easy to cut and paste references from
# news articles or other sources.  (This feature can be disabled for sites,
# such as ulowell, that depend on mixed case names).
#
# The optional site name parameter can be any significant substring of the
# ftp address.  For example, "simtel", "simtel20", and "WSMR" would all serve
# to select the wsmr-simtel20.army.mil archive site.  You can easily change
# the table of site names: see the "FAVORITE SITES" comment below.
#
# If the environment variable FTPLOG is set to a file name, then a record of
# your ftp transactions will be appended to that file.
#
# Example: get files from oak.oakland.edu, including names matching wildcard:
#
#          get info 'inter33*.zip' turbopas anivga11.zip unitre12.zip
#              (dir) (w/c files)   (dir)    (file)       (file)
#
# Example: show basename and lower-case features (useful for cut/paste):
#
#          get /pub/msdos/microsoft PD0646.ZIP
#              (dir)                (file)
#
# Example: connect to a specified site, rather than to oak:
#
#          get garbo  turboobj totdoc.zip
#              (site) (dir)    (file)
#
# Notes:
# 1) Before using this script, change the ftp login to use your internet
#    address (see comment "change to your address" below).  Also take a look
#    at the site name table below, to make sure it includes the sites you
#    use.
# 2) Allowing non-exact site name matches is usually helpful, allowing
#    "simtel" as an adequate name for "wsmr-simtel20.army.mil," for example.
#    However, if your first directory name is something short (e.g. "c" for
#    C compiler stuff), confusion is possible ("c" matches "wuarchive").  In
#    that case, simply remember to specify the site name, e.g.:
#                     get oak c actlib13.zip bltc13.zip
# 3) If you want to deposit incoming files in various directories on your
#    local system, it's easy to add "lcd" commands to accomplish this.  The
#    place to do so is indicated in comments below.
# 4) It is *not* necessary to get a fresh update of this script everytime an
#    archive site changes its directory structure.  No directory names are
#    hard coded.
# 5) Some versions of Unix apparently do not support the "getopts" parsing
#    command.  If yours is one of these versions, remove the "getopts"
#    section below (it's marked) and uncomment the "getopt" alternative.
#    Many thanks to Kees Visser for providing this information, as well as
#    many site table entries for European sites.
# 6) I welcome comments.
#
# Author: Jonathan C. Rice, rice@zizania.cray.com
###########################################################################


set -f
remote=`(
#############################################################################
#     **** MODIFY THE FOLLOWING TABLE TO INCLUDE YOUR FAVORITE SITES ****
#
# Format:
#   -s <site name> -r <path name to directories of interest> [-b] [-l] 
#                                                  -b = basename arguments
#                                                  -l = force lower case
##############################################################################
grep -i ".*-s\ *[a-z0-9.-]*$1[^\ ]*" <<EOF | head -1
  -s oak.oakland.edu        -r /pub/msdos/*         -bl
  -s wuarchive.wustl.edu    -r /mirrors/msdos/*     -bl
  -s garbo.uwasa.fi         -r /pc/*                -bl
  -s ftp.cica.indiana.edu   -r /pub/pc/win3/*       -bl
  -s wsmr-simtel20.army.mil -r pd1:<msdos.*>        -bl
  -s ftp.ulowell.edu        -r /msdos/Games/*       -b
  -s archive.orst.edu -r /pub/mirrors/oak.oakland.edu/simtel20/msdos/* -bl
  -s ftp.uwp.edu            -r /pub/msdos/games/ulowell/*  -b
  -s ftp.uu.net             -r /systems/ibmpc/msdos/simtel20/*  -bl
  -s src.doc.ic.ac.uk       -r /packages/ibmpc/wsmr-simtel20.army.mil/* -bl
  -s sun0.urz.uni-heidelberg.de -r /mirrors/msdos/* -bl
  -s nic.funet.fi           -r /pub/msdos/SIMTEL20-mirror -bl
EOF
)`

if [ -n "$remote" ]
then
  shift
else
  remote="-s oak.oakland.edu -r /pub/msdos/* -bl"  ####### <<-- default site
fi

basename="echo"
lower=""

##### USE THIS IF YOUR VERSION OF UNIX SUPPORTS "GETOPTS" #####
while getopts blr:s: p $remote
do
  case $p in
    b) basename="basename";;
    l) lower="| tr A-Z a-z";;
    r) root=$OPTARG;;
    s) site=$OPTARG;;
  esac
done
##### USE THIS IF YOUR VERSION OF UNIX DOES NOT SUPPORT "GETOPTS" #####
# inputargs=$*
# set -- `getopt blr:s: $remote`
# while [ $1 != -- ]
# do
#   case $1 in
#     -b) basename="basename"
#         shift;;
#     -l) lower="| tr A-Z a-z"
#         shift;;
#     -r) root=$2
#         shift 2;;
#     -s) site=$2
#         shift 2;;
#   esac
# done
# set -- `getopt x $inputargs`
# shift
########################################################################

# Loop through remaining parameters to create ftp commands.

(
echo "user anonymous $USER@site.com"   ########### <<-- change to your address!
echo binary
while [ $# -gt 0 ]
do
  f=`eval $basename $1 $lower`
  case $f in
    *\**|*\?*)
      echo mget $f
      echo `date '+19%y-%m-%d %T GET '`$site:$d/$f >> ${FTPLOG:=/dev/null};;
    *.*)
      echo get $f
      echo `date '+19%y-%m-%d %T GET '`$site:$d/$f >> ${FTPLOG:=/dev/null};;
    *)
      d=`echo $root|sed -e "s!\*!$f!"`
      echo cd $d
      # Personal preference: lcd to various local directories depending
      # on the site connected to.  You can do the same sort of thing, using
      # whatever combinations of $site, $d (directory path), and
      # $f (directory basename) are appropriate for you.
      #
      ### case $site in
      ###   oak.oakland.edu) echo lcd $HOME/pc/oak/$f;;
      ###   ftp.cica.indiana.edu) echo lcd $HOME/pc/cica;;
      ###   *) echo lcd $HOME/pc/other;;
      ### esac
      ;;
  esac
  shift
done
echo quit
) | ftp -vin $site
