#!/bin/bash
# autoftp -- script for FTP'ing a list of files
# Copyright 1998 by Richard L. Hawes under the GNU GPL v. 2
# email: rhawes@dmapub.dma.org   web:  http://www.dma.org/~rhawes
SYNOPSIS="autoftp -{h|g|l|n|p|r|s} [-{C|E|T} arg] FTP_ADDR REM_PATH < FILE_LIST"
VERSION='$Id: autoftp,v 0.93 1998/10/30 04:24:58 hr Exp $'
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

set -h
# init vars
FTPCmd_Ptr="pftp -iv"
FTP_Commands=""
MD_Func_Ptr=""
ArchCmd_Ptr=":"

function Do_FTP { # get site ftp files
	local FTCmd="$1"
	{
	# Connects to the ftp site at FTP_ADDRESS
	# through auto-login,
	# changes the remote directory to Rem_Path,
	echo "open $FTP_Addr
	cd $Rem_Path"
	echo "binary"
	echo "$FTP_Commands"
	while read File
		do
		if test "$File"; then
			$MD_Func_Ptr
			# gets the files listed in the standard input,
			echo "$FTCmd $File"
			$ArchCmd_Ptr $File
		fi
		done
	} | $FTPCmd_Ptr || exit
}

function MkDir_Func { # get site ftp files
	Directory=`dirname $File`
	if test ! -d $Directory; then
		mkdir -p $Directory
	fi
}

function DL_FTP { # get site ftp files
	MD_Func_Ptr="MkDir_Func"
	Do_FTP $@
}

function Size { # get site ftp files
	Do_FTP $@ | \
        awk ' BEGIN{sum=0}
        /^[0-9]+ [0-9].*/ {sum += $2}
        { print }
        END{print sum}'
}

function Error { # usage error message

	echo "$1 usage:
$SYNOPSIS
use -h for more information." >&2
	exit 2
}

function Version { # version

	echo  "autoftp version: `echo $VERSION | cut -f3 -d' '`
Copyright (C) 1998 by Richard L. Hawes
autoftp comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of autoftp
under the terms of the GNU General Public License version 2.
For more information about these matters, see the file named COPYING."
	exit
}

function Help_FTP { # help

	cat <<HELP_EOF
autoftp version: `echo $VERSION | cut -f3 -d' '`
Copyright 1998 by Richard L. Hawes released under the GNU GPL ver. 2
SYNOPSIS: (It uses auto-login -- configure .netrc)
$SYNOPSIS
FTP_ADDR	: FTP address
REM_PATH	: remote directory path
FILE_LIST	: list of relative pathnames to files

ACTION		: description
--version	: current version
-g, --get	: get the files in the list
-h, --help	: this help message
-l, --ls	: list remote files
-n, --newer	: get the newer files in the list
-p, --put	: put the files in the list
-r, --reget	: reget the files in the list
-s, --size	: calculate the total size of the files in the list

OPTIONS ARG	: description
-A Archive_Cmd	: of the form: 'tar -rf arch.tar --remove-files' 
-C FTP_Actions	: ftp commands passed onto ftp after connection
-E FTP_CMD	: executable ftp command with options
-T Tfr_Act	: File Transfer Action command for each file
HELP_EOF
exit
}

Opt_Str=""
# Action
case $1 in
	-l|--l*) Func_Ptr="Do_FTP"; Tfr_Act="ls" ;;
	-p|--p*) Func_Ptr="Do_FTP"; Tfr_Act="put" ;;
	-s|--s*) Func_Ptr="Size"; Tfr_Act="size" ;;
	-g|--g*) Func_Ptr="DL_FTP"; Tfr_Act="get";
		Opt_Str="A:"
	;;
	-r|--r*) Func_Ptr="DL_FTP"; Tfr_Act="reget";
	;;
	-n|--n*) Func_Ptr="DL_FTP"; Tfr_Act="newer";
	;;
	-h|--help) Help_FTP ;;
	--version) Version ;;
	*) Error "$0:$LINENO::Invalid Action: \"$1\"
"
		;;
esac
shift

# Options
Opt_Str="${Opt_Str}xtT:C:E:"
while getopts $Opt_Str Option
	do
	case $Option in
		A) ArchCmd_Ptr="echo ! $OPTARG" ;;
		T) Tfr_Act="$OPTARG" ;;
		C) FTP_Commands="$OPTARG" ;;
		E) FTPCmd_Ptr="$OPTARG" ;;
		x) set -xue ;;
		?) Error "" ;;
	esac
	done

shift `expr "$OPTIND" - 1`
# Arguments
if [ 2 -ne $# ] ; then
	Error "$0:$LINENO::incorrect number of arguments: $#
"
fi

FTP_Addr="$1"
Rem_Path="$2"

$Func_Ptr $Tfr_Act
