#!/bin/sh -x
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   copy a file to a remote server using ftp
# author
#   idc@planetlarg.net 26 nov 2002
# description
#   ftp the file to another host. No user intervention is required.
#   If successful, delete the original.
#   This script keeps a log of transfers made.
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup
        usage_message="
        usage:
          $0 file
        example:
          the command
                 $0 /tmp/stuff.txt
        description
          ftp the file to another host.
          If successful, delete the original.
          This script keeps a log of transfers made.
        "
# checks
	# command line options
	# no options provided? tell the punter how to use this utility
	if [ $# -eq 0 ]; then
		echo "no options were provided."
		echo "$usage_message" 1>&2
		exit 1
	fi

	# Check to see if we are being called by 'user01'
	if [ `/usr/ucb/whoami` != "user01" ]; then
		echo "$0: You must be user01 to run this script" >&2
		exit 128
	fi

	# Check if passed argument is actually a file
	if [ ! -f $1 ]; then
		echo "$0: Argument passed is not a file" >&2
		exit 130
	fi


# main
        LOG=/tmp/ftp.log
        USER=anonymous
        PASS=me@isp01.co.uk
        DEST_HOST=ics01
        DEST_PORT=21
        DEST_DIR=/pub
        SOURCE_DIR=`dirname $1`
        SOURCE_FILE=`basename $1`

# start logging
        echo "------------------------------------------" >> $LOG
        echo "Run date: "`date` >> $LOG

# copy file
        { echo "open $DEST_HOST $DEST_PORT
        user $USER $PASS
        hash
        cd $DEST_DIR
        lcd $SOURCE_DIR
        put $SOURCE_FILE
        close"
        } | ftp -i -n -v 2>&1 | tee -a $LOG
        RET=$?

# Check return code
        if [ $RET -ne 0 ]; then
                echo "$0: ftp to $DEST_HOST failed with return code $RET" >&2;
                exit $RET
        fi

# Remove file
        /bin/rm -f $SOURCE_FILE
        exit 0


