#
# Transfer directory trees by ftp, is much faster than WYSIWYG tools.
#
# $Revision: 1.8 $
#
# Usage: 
#
# perl wput.pl hostname login passwd <list of directories>
#
# perl wput.pl ftp.hello.org gael 123hello my_dir1 my_dir2

use strict;
use Net::FTP;
use File::Find;

if (@ARGV<4) {
  # Not enough parameters, exit
  print STDERR "Usage:\n\n";
  print STDERR "perl wput.pl hostname login passwd <list of directories>\n\n";
  die;
}

# list of directories where to enforce lower case filenames
my $enforce_regexp = '^(players|results)';


# get parameters from command line 
my ($remote_host, $remote_login, $remote_pwd, @dirs) = @ARGV; 

$| = 1;			# make stdout unbuffered

my ($remote_home) = '~';

# Open connection to remote host
my $ftp = Net::FTP->new( $remote_host) or die "Unable to connect to $remote_host\n";

# login 
$ftp->login( $remote_login, $remote_pwd);

# set transfer to binary to avoid problem with pictures or other binary files
$ftp->binary();

# process all files and directories under the directories in arguments
File::Find::find(\&wanted, @dirs);

# close connection
$ftp->close();


#
# This function is called for each file and directory found under
# the directories in arguments
#
sub wanted {
  # get destination name
  my $dst_name = $File::Find::name;
  $dst_name =~ s/^\.\\//;
  $dst_name =~ s/\\/\//g;

  # enforce lower case filenames for UNIX system
  if ($dst_name =~ /$enforce_regexp/i) {
      $dst_name = lc $dst_name;
  }

  if (-d $_) {
    # it's a directory, create it without checking if it exists
    $ftp->mkdir($dst_name);
    print "mkdir ($dst_name)\n";
  }
  else {
    # it's a file
    # get source name
    my $src_name = $_;

    # skip homesite backup files
    return if /^~hs.*\.htm$/;

    $ftp->put ($src_name, $dst_name) or die "Failed to put ($src_name, $dst_name): $!\n";
    print "put ($src_name, $dst_name)\n";
  }
}

