#!/usr/bin/perl -w
#---------#---------#---------#---------#---------#---------#---------#---------
#
# Date:         11 Oct 00
# Title:        copy a file from another host using Net::FTP
# Creator:      idc@planetlarg.net
# Description:
#   first go with the Net::FTP module.
#   if you want to put files, put their absolute locations in the put list
#   eg.
#   my @get_files = (
#      '/dir/file01',
#      '/dir/file02',
#      '/dir/file03'
#   );
#   if you have no files to put, leave the list empty.
#
# Modifications:
#
#---------#---------#---------#---------#---------#---------#---------#---------


# what's a script to do?


# files to get
   # copy files to this directory on the local host
   my $local_dest = '/home/isp01/bin/ftp';
   my @get_files = (
      '/export/home/isp01/ldapsearch/ldap.conf',
      '/export/home/isp01/ldapscripts.tar'
   );                               # remote files to copy to the local machine

# files to put
   # copy files to this directory on the remote host
   my $remote_dest = '/export/home/isp01';
   my @put_files = (
   );                               # local files to copy to the remote machine

# hosts and accounts
   my %hosts      = (
#      "ldp01"  => "10.216.4.28",
#      "ldp02"  => "10.216.4.53",
#      "lds01"  => "10.216.4.34",
#      "lds02"  => "10.216.4.59",
#      "lds03"  => "10.216.4.35",
      "lds04"  => "10.216.4.60"
   );                               # the magnificent six

   my $username01 = "isp01";       # account is common to all isp01 hosts
   my $password01 = "passw0rd";

# rock and roll
   # why roll your own sockets when you can use a rolling machine?
   use Net::FTP;
   my $ftp;


   # files and directories
   use File::Basename;
   my ($name, $suffix);                   # file to transfer
   my ($remote_source, $local_source);    # directories where the files are

   my $copy_wanted;

   # status message indent system
   my $indent = '   ';
   my $spaces = 1;

my $p;
my @l;

#---------#---------#---------#---------#---------#---------#---------#---------
   foreach $host ( keys %hosts ) {

      # status message
      print $indent x $spaces . "logging into $hosts{$host} ... \n";
      $spaces++;

      $ftp = Net::FTP->new("$hosts{$host}", Debug => 5);
      $ftp->login("$username01", "$password01");

      $ftp->type('binary'); # can be ascii, binary, ebcdic or byte

# get: copy files from remote to local host
      $copy_wanted = @get_files; # $... = @... gives number of elements in list
      if ( $copy_wanted ) {

         # status message
         print $indent x $spaces . "getting files to $local_dest ... \n";
         $spaces++;

         foreach $get_file ( @get_files ) {
            ($name, $remote_source, $suffix) = fileparse( "$get_file", '');

            # status message
            print $indent x $spaces . "getting $name from $remote_source ... \n";

            chdir "$local_dest" or die "can't cd to $local_dest: $!";
            $ftp->cwd("$remote_source");



# testing
$p = $ftp->pwd();
print "3 \$p \n";
print "$p \n\n";

print "1 \n";
$ftp->ls("$remote_source");
print "2 \n";
print $ftp->ls("$remote_source");
print "3 \n";
@l = $ftp->ls("$remote_source");
print "\@l \n";
print "@l \n\n";
#            $ftp->get("$name");


         } # foreach
         $spaces--;
      } # if


# put: copy files from local to remote host
      $copy_wanted = @put_files; # $... = @... gives number of elements in list
      if ( $copy_wanted ) {
         print "putting files ... \n";
         foreach $put_file ( @put_files ) {
            ($name, $local_source, $suffix) = fileparse( "$put_file", '');
            print "   putting $name from $local_source to $remote_dest ... \n";
            chdir "$local_source" or die "can't cd to $local_source: $!";
            $ftp->cwd("$remote_dest");



#            $ftp->put("$name");




         } # foreach
      } # if

# clean up
      $ftp->quit;
      $spaces--;
   }

