#!/usr/bin/env ruby

# Version 1.0.1

=begin

= FtpSendTo

Written by Zoran Lazarevic <laza at cs.columbia.edu>

This software is free.
You can freely distribute/modify this software.


=end

require 'net/ftp'
require 'getoptlong'

def main

	parse_options
	
#
# For Windows paths, convert backslash to forward slash.
#
	ARGV.each do |filepath| filepath.tr!('\\','/') ; end
	
	ftpfiles ARGV

end


def parse_options

	$optlist=[
	  [ "--help",                     GetoptLong::NO_ARGUMENT ],
	  [ "--verbose", "-v",            GetoptLong::NO_ARGUMENT ],
	  [ "--host", "--ip", "-h",       GetoptLong::REQUIRED_ARGUMENT ],
	  [ "--username","-u",            GetoptLong::REQUIRED_ARGUMENT ],
	  [ "--password","-p",            GetoptLong::REQUIRED_ARGUMENT ],
	  [ "--remote_dir", "-d",         GetoptLong::REQUIRED_ARGUMENT ]
	]
	
	opts = GetoptLong.new *$optlist
	
	# process the parsed options
	
	$username="anonymous"
	$password=""
	$password_is_blank=FALSE
	$host=""
	$verbose=false
	$blocksize=2048
	
	
	begin
		opts.each do |opt, arg|
			puts "Option: #{opt}, arg #{arg.inspect}"
			
			case opt
			when "--help"         then begin usage; exit(0) end
			when "--verbose"      then $verbose=true
			when "--host"         then $host=arg
			when "--host"         then $host=arg
			when "--username"     then $username=arg
			when "--password"     then $password=arg
			when "--remote_dir"   then $remote_dir=arg
			else errprint "Unsupported option #{opt}\n"
			end
		end
	rescue StandardError => e
		errprint "#{e}\n"; usage; exit(-1)
	end
	
	
	if ARGV.length == 0
		usage
	end
	
	
	print "username=#{$username}\n"
	print "password=#{$password}\n"
	print "host=#{$host}\n"
	print "verbose=#{$verbose}\n"
	print "blocksize=#{$blocksize}\n"
	
	$username = ask_if_not_defined( $username, "Username" )
	if $username != "anonymous"
		$password = ask_if_not_defined( $password, "Password" )
	end
	$host     = ask_if_not_defined( $host, "Host address" )
	
end



def ftpfiles(files)
	ftp = Net::FTP.open($host,$username,$password)
	
        if $remote_dir then
            print "Changing dir to #{$remote_dir}:\n"
            print ftp.chdir($remote_dir)
        end
        
	files.each do |f|
		print "#{f}: "; flush
		ftp.putbinaryfile(f, File.basename(f), $blocksize) {
			print "#"; flush
		}
		print "\n"
	end
	
	ftp.close
end


def ask_if_not_defined( param, paramname )
	if param==""
		$stdout.print "Enter #{paramname}: "
		param = $stdin.readline.chomp!
		if param==""
			errprint "#{paramname} not specified\n"; exit(-1)
		end
	end
	param
end

def usage
	cmd = File.basename($0)
	errprint "\n\n"
	errprint "#{cmd} - sends files via FTP\n"
	errprint "        Version 1.0\n"
	errprint "Usage: #{cmd} [options] files\n"
	errprint "Options:\n"
	$optlist.each do |option|
		$stderr.print "    "
		option.each do |i|
			$stderr.print "#{i} " if i.kind_of? String
		end
		puts "\n"
	end
	errprint "Example: #{cmd} --user JOHN  --host ftp.site.com  FILE1.BIN F2.BIN\n"
	errprint "    Sends .BIN files to the FTP server, logging in as user JOHN\n"
	errprint "    Since password is omitted, you will be prompted to enter it\n"
	errprint "\n\n"
end


def print(args)
	if $verbose
		$stdout.print *args
	end
end

def flush
	$stdout.flush
end

def errprint(args)
	$stderr.print *args
end



#-----------------------------------------------------
#
# Code begins here
#
#-----------------------------------------------------

main


