#!/bin/sh -x
#
# title
#   get a file from another host using ftp
# author
#   idc@planetlarg.net 15 May 04
# description
#   Create a configuration file ".netrc". Try "man netrc" for a description.
#   Start ftp. It will login to the remote server using the settings in the
#   .netrc file.
#   Find the files we are interested in and copy them.
#   Logout.
#
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup

   TODAY=`date +%d%m%Y`

   LOCAL_DIR=/tmp/ics01

   REMOTE_HOST=ics01
   REMOTE_USER=admin
   REMOTE_PASS=pass
   REMOTE_DIR=/users/newsreel/site-1/$TODAY
   REMOTE_FILE=stories_${TODAY}.csv

   usage_message="
   usage:
      $0
   example:
      the command
         $0
      transfers $REMOTE_HOST:$REMOTE_DIR/$REMOTE_FILE to $LOCAL_DIR
         something
   description
     FTP a file from a remote server to here. Uses a .netrc file
     which contains user name and password so security is a bit
     dodgy here.
   "
# command line options
   # no options provided? tell the punter how to use this utility
   if [ $# -ne 0 ]; then
      echo "$usage_message" 1>&2
      exit 1
   fi
#---------#---------#---------#---------#---------#---------#---------#---------
# main
    date

# create a new .netrc file
    mv $HOME/.netrc $HOME/.netrc.bak

    echo machine  $REMOTE_HOST  > $HOME/.netrc
    echo login    $REMOTE_USER >> $HOME/.netrc
    echo password $REMOTE_PASS >> $HOME/.netrc
    chmod go-rwx $HOME/.netrc

# check the local dir
    if [ ! -d $LOCAL_DIR ]
    then
        mkdir     $LOCAL_DIR
        chmod 700 $LOCAL_DIR
    fi
    cd $LOCAL_DIR

# transfer
   # login
   # find the files
   # transfer the files
   # close the connection
   ftp -v << DONE
   open $REMOTE_HOST
   binary
   cd   $REMOTE_DIR
   get  $REMOTE_FILE
   bye
DONE


# clean up and finish off
   date
   echo

   # replace the temporary netrc file
   cd
   rm $HOME/.netrc
   mv $HOME/.netrc.bak $HOME/.netrc

