#!/bin/bash

remote_ip=$1
logName=$2
logPassword=$3
remote_dir=$4
local_dir=$5

echo remote_ip=$remote_ip
echo logName=$logName
echo logPassword=$logPassword
echo remote_dir=$remote_dir
echo local_dir=$local_dir

file_list_filename=/tmp/file_list.txt
rm -rf $file_list_filename

function get_whole_dir()
{
	cur_local_dir=$1
	cur_remote_dir=$2

	cd $cur_local_dir
	echo cur_remote_dir=$cur_remote_dir
	echo cur_local_dir=$cur_local_dir
	response=`ftp -i -n $remote_ip  <<haha
user $logName $logPassword
binary
cd $cur_remote_dir
ls
bye
haha`

	file_list=`echo "$response" |grep -v '<DIR>'|grep '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]'|awk '{print $4}'`
	dir_list=`echo "$response" |grep -i '<DIR>'|grep -v '\.'|awk '{print $4}'`
	
	echo -------FILE----------
	for file in $file_list; do
#ftp -i -n $remote_ip  <<haha
#user $logName $logPassword
#binary
#cd $cur_remote_dir
#lcd $cur_local_dir
#get $file
#bye
#haha
	echo "$cur_remote_dir,$cur_local_dir,$file" >> $file_list_filename
	done

	echo -------DIR----------
	for dir in $dir_list; do
		mkdir $cur_local_dir/$dir 2>/dev/null
                chmod 755 $cur_local_dir/$dir
                echo mkdir $cur_local_dir/$dir

		get_whole_dir $cur_local_dir/$dir $cur_remote_dir/$dir
	done


}


get_whole_dir $local_dir $remote_dir

