#!/usr/bin/expect -f

#This script interative with the remote server, and send the file to it

set remoteip [ exec cat accout | grep RemoteServerIP | awk -F= "{print \$2}" ]
set username [ exec cat accout | grep LoginUserName | awk -F= "{print \$2}" ]
set userpass [ exec cat accout | grep LoginPassword | awk -F= "{print \$2}" ]

set mode [lindex $argv 0]
if { $mode == "getdir" } {
	set remote_dir [lindex $argv 1]
} else {
	if { $mode == "putdir" } {
		set local_dir [lindex $argv 1]
		set remote_dir [lindex $argv 2]
	} else {
		puts "Usage: ./sftpd getdir remotedir"
		puts "       ./sftpd putdir localdir remotedir"
		puts "       ./sftpd getfile remotefile"
		puts "       ./sftpd putfile localfile remotefile"
		exit
	}
}


set work_dir [ exec pwd ]
set remote_base_dir [ exec basename $remote_dir ]

proc get_directory {dir} {
	# get current local dir
        send "lpwd\n"
        expect "*sftp>*"
        set current_local_dir_str $expect_out(buffer)
        set current_local_dir [ exec echo $current_local_dir_str | grep "directory" | awk -F: "{print \$2}" | cut -c2- ]

	# get current remote dir
	send "pwd\n"
	expect "*sftp>*"
	set current_remote_dir_str $expect_out(buffer)
	set current_remote_dir [ exec echo $current_remote_dir_str | grep "directory" | awk -F: "{print \$2}" | cut -c2- ]

	# get remote base dir
	set current_remote_base_dir [ exec basename $dir ]

	# check the dir whether exist
	#set result [ exec ls -l | grep -c "$current_remote_base_dir\$" ]
	#if { $result == 1 } {
	#	send_user "file:$current_remote_base_dir already exist"
	#	exit
	#}

        # create the local dir
	send "lmkdir $current_remote_base_dir\n"
	expect "*sftp>*"
	
	# cd remote dir
        send "cd $dir\n"
        expect "*sftp>*"

	# cd local dir
        send "lcd $current_remote_base_dir\n"
        expect "*sftp>*"


	# list the file in the current remote dir
	send "ls -l\n"	
	expect "*sftp>*"
	set fileliststr $expect_out(buffer)

	# get remote files
	set runcmd [list exec echo $fileliststr | grep "^\[-d\]" | grep "^\[^d\]" | awk "{print \$9}"]
	if {[catch $runcmd res]} {
		send_user "skip"
	} else {
		set filelist [ exec echo $fileliststr | grep "^\[-d\]" | grep "^\[^d\]" | awk "{print \$9}" ]
		foreach file $filelist {	
			send "get $file\n"
			expect "*sftp>*"
		}
	}

	# process dir
	set runcmd [list exec echo $fileliststr | grep "^d" | awk "{print \$9}"]
	if {[catch $runcmd res]} {
		send_user "skip"
	} else {
		set dirlist [ exec echo $fileliststr | grep "^d" | awk "{print \$9}" ]
		foreach directory $dirlist {
			set new_remote_dir [ exec echo $dir/$directory ]
			get_directory $new_remote_dir
		}
	}	

	send "lcd ..\n"
        expect "*sftp>*"

	send "cd $current_remote_dir\n"
	expect "*sftp>*"
}

set timeout 120

spawn sftp $username@$remoteip


expect "*(yes/no)?" {send "yes\n"; expect "*assword: "} "*assword: "
send "$userpass\n"
expect "*sftp>*"
if { $mode == "getdir" } {
	get_directory $remote_dir
} else {
	send "$local_dir $remote_dir\n"

	expect "*sftp>*"
}
#set value $expect_out(buffer)
#send "$value\n"
#expect "*sftp>*"
exit

