#!/bin/bash

function hostpath() {
TMP=`expr index "$1" /`
echo ${1:0:$TMP-1}
}

GSA_ID="anonymous nopass"
# $1=host $2=local_path_file $3=remote_path
function ftp_mput(){
echo -e "user $GSA_ID\n prompt \n cd $3 \n mput $2 " | ftp -n $1
}

# $1=host $2=remote_path
function ftp_mkdir(){
echo -e "user $GSA_ID\n mkdir $2 \n chmod 777 $2" | ftp -n $1
}

# $1=$host, $2=$remotepath
function ftp_subdir() {
for subfile in *
do
        echo processing dirent $subfile
        if [ -d $subfile ]
        then
            ftp_mkdir $1 "$2/$subfile"
            cd $subfile
            ftp_mput $host "*" "$2/$subfile"
            ftp_subdir $1 $2/$subfile
            cd ..
        fi
done
}

if [ $# -ne 2 ]; then
        echo Usage: $0 localdir remotedir
        exit
fi        
file=$2
host=`hostpath $file`
path=${file##$host} 
machinename=`hostname -s`
#echo file=$file, host=$host, path=$path, machinename=$machinename
#exit
cd $1
for file in *
do
        echo processing dirent $file
        if [ -d $file ]
        then
       	    ftp_mkdir $host "$path/$machinename.$file"
       	    cd $file
       	    ftp_mput $host "*" "$path/$machinename.$file"
            ftp_subdir $host $path/$machinename.$file
            cd ..
        fi
done
