#!/bin/sh
#
# Automatic ftp'er, written by Daniel Stenberg <Daniel.Stenberg@sth.frontec.se>
# Version 1.8 - 21 July 1997. THIS IS ADJUSTED TO WORK WITH DANCER.
#
# Edit the email address in the script below to use your own. The address is
# used as password for anonymous ftp login.
# Should work with any unix ftp client, but uses ncftp prior to the standard
# one.
# If the original ftp client is used, it preserves the original .netrc (if any)
# (NOTE: some shells don't allow the HOME env to be changed this easily
# which will make this script use the ~/.netrc file. If you run a regular ftp
# client that exepects that file while using this script, there might become
# a conflict.)
# 
# USAGE:
#    fileget <site> <remote file> <local file> <temporary download name>
#
if test "$#" -lt 4; then
 echo "Usage: ftper <site> <remote file> <local file> <temp name>"
 exit
fi

# Get setup info
. ./filesetup

# We have a temporary name we should use while downloading. When done, we
# rename it from that to the $3 name.
DLNAME="$4"

# this strips the path nice and clean
destname=`basename $3`

# "Fake" home directory for ftp (fark, doesn't work for all shells, but I'll
# leave it in for those that do)
HOME=/tmp


if test \! -x "/usr/bin/ncftp"; then
  # We don't have the ncftp client.

  # If there is a .netrc file, backup it and restore it after our ftp
  if test -f "$HOME/.netrc"; then
    move="move"
    mv $HOME/.netrc $HOME/.netrc.$$
  fi

  # Let's create the file the way WE (well, ftp) want it to be
  echo "machine $1 login anonymous password auto-ftp@sth.frontec.se macdef" >$HOME/.netrc
cat >>$HOME/.netrc <<EOF
bin


EOF

# Now get the sacred file home
ftp $1 <<END
get $2 $DLNAME
bye
END

  if test -n "$move"; then
    # restore the previous .netrc file
    mv $HOME/.netrc.$$ $HOME/.netrc
  else
    # just remove it after us again
    rm $HOME/.netrc 
  fi

else
  # use ncftp, if this fails well then FAIL! ;)
  ncftp -c $1:$2 >$DLNAME
fi


if test -f "$DLNAME"; then
  # This file exists

  bytes=`wc -c "$DLNAME" | awk '{print $1}'`

  if test $bytes -gt 0; then
    # We downloaded the file to a temporary name, now we rename it to the 
    # actual destination name to use.
    mv "$DLNAME" "$CACHEDIR/$destname"

    # Call the counter!
    ./filecount $2 $3
  else
    # Remove the zombie 0-bytes file
    echo "No such file!"
    rm "$DLNAME"
  fi
fi

# ftp done, script terminates


syntax highlighted by Code2HTML, v. 0.9.1