#!/bin/sh
#--
#-- mkfsc - make an ftp script from an aminet index file. this file is 
#--         then used in the command line:
#--
#--         % ftp < script_file
#--
#--         where 'script_file' is the file created by mkfsc.  Usage of 
#--         mkfsc is:
#--
#--         mkfsc <index> <site:dir>
#--
#--         where index is the section from the aminet INDEX file that 
#--         contains the list of files that are desired. "site" is the 
#--         address of the aminet site and "dir" is the top level dir-
#--         ectory to aminet at that site.  So for example:
#--
#--         % mkfsc shell-list ftp.luth.se:/pub/aminet > foo
#--
#--         would create a script in the file 'foo' which would be a 
#--         collection of commands that would go to ftp.  Note that
#--         mkfsc echos all output to stdout so that it needs to be
#--         redirected to a file unless you wish to redirect the output
#--         right into ftp:
#--
#--         % ftp < mkfsc shell-list ftp.luth.se:/pub/aminet
#--
#--         Note that you should have an entry in your .netrc for the
#--         site you wish to go to.  That entry should look like:
#--
#--         machine ftp.luth.se
#--         login anonymous
#--         password eraugust@igate1.hac.com
#--
#-- bugs -  well, it's slow.  it's a script.  i needed something that
#--         would work.  i thought after it was done that it might be 
#--         useful for someone else.  there's probably a better way to
#--         do this with a bourne shell script - if you think of it
#--         let me know.
#--
#-- help -  email to eraugust@igate1.hac.com
#--
#-- mkfsc is Copyright (c)1996 Eric R. Augustine, all rights reserved.
#--
#-- This software is free and as such carries no warranty or garantee
#-- of any sort implied or otherwise expressed.  This software should
#-- not be distributed for any profit whatsoever and must be distri-
#-- buted complete with all documentation and without modification 
#-- except with the expressed permission of the author.
#--
#-- $Id: mkfsc,v 1.2 1996/02/29 23:01:20 august Exp $
#--
PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin
MYNAME=`basename $0`
USAGE="usage: $MYNAME <index> <site:directory>"
DATE=`date "+%H%M%S%m%d%y"`
TMPFILE="$MYNAME.$DATE"

#-- check usage
case $# in
	2)
		INDEX=$1
		SITE=$2
		;;
	*)
		echo $USAGE
		exit 0
		;;
esac

#-- create the temporary file that will be modified.
cp $INDEX $TMPFILE
echo "END_OF_INDEX" >> $TMPFILE

#-- this value to increment "pointer" through file
UPCOUNT=0

#-- get site name and top level directory name from command line
TOP=`echo $SITE | cut -d: -f2`
SITE=`echo $SITE | cut -d: -f1`

#-- begin creating file
echo "open $SITE"
echo "bin"
echo "prompt"

#-- loop though file and create get and cd commands
#-- if previous directory is the same create no cd command.
while [ 1 ] 
	do
		FILE=`head -$UPCOUNT $TMPFILE | tail -1 | awk '{ print $1 }'`
		DIRE=`head -$UPCOUNT $TMPFILE | tail -1 | awk '{ print $2 }'`
		UPCOUNT=`expr "$UPCOUNT" + 1`
		if [ "$FILE" = "END_OF_INDEX" ] ; then
			echo "quit"
			rm -f $TMPFILE
			exit 0
		fi
		if [ "$DIREC" != "$DIRE" ] ; then
			DIREC=$DIRE
			echo "cd $TOP/$DIRE"
		fi
		if [ "$FILE" ] ; then
			echo "get $FILE"
		fi
	done

#-- echo command to get out of ftp, delete temporary file and exit.
echo "quit"
rm -f $TMPFILE
exit 0

