#! /usr/bin/perl

use Net::FTP;
use IO::File;
my $coderetour=0;

### TYPE TRANSFERT FTP ###
##########################
sub set_ftp_mode {
   my $ok;
   if ($_[1] eq "" || $_[1] eq "ASCII" || $_[1] eq "ASC") {
     $ok = $_[0]->ascii();
#     print "ASCII mode $cr\n";
   }
   else {
     $ok = $_[0]->binary();
#     print "BINARY mode $cr\n";
   }
   return $ok;
}

### PUT FILE ###
################
sub put_file {
  my $ok = 0;
  my $ftp = $_[0];
  my $line_cmd = $_[1];
  $line_cmd =~ s/\/\//\//g;
  my @tmp = split(' ',$line_cmd);
  set_ftp_mode($ftp,uc($tmp[2]));
  print "\nput $tmp[0] -> $tmp[1]\n";
  my $res = $ftp->put($tmp[0], $tmp[1]);
  if (! $res) {
    print " KO\n".$ftp->message() . "\n";
    $ok=1;
  }
  else {
    print " OK.\n";
  }
  return $ok;
}
### GET FILE ###
################
sub get_file {
  my $ok = 0;
  my $ftp = $_[0];
  my $line_cmd = $_[1];
  $line_cmd =~ s/\/\//\//g;
  my @tmp = split(' ',$line_cmd);
  set_ftp_mode($ftp,uc($tmp[2]));
  print "\nget $tmp[0] -> $tmp[1]\n";
  my $res = $ftp->get($tmp[0], $tmp[1]);
  if (! $res) {
    print " KO\n".$ftp->message() . "\n";
    $ok=1;
  }
  else {
    print " OK.\n";
  }
  return $ok;
}



### LECTURE FICHIER INI ###
###########################
my $inifile = $ARGV[0] || 'ftp.ini';
my $ini = new IO::File;
my $ok = $ini->open($inifile,"r");
if (! $ok) {
  die "Impossible d'ouvrir $inifile\n";
}
my @inidata = grep { !/^ *#/ && !/^$/ } $ini->getlines;
$i=0;
while ($i <= $#inidata) {
  chomp($inidata[$i]);
  $inidata[$i] =~ s/\\/\//g;
  $i++;
}
$ini->close();


### TRAITEMENT DES PARAMETRES  ###
##################################
my $i=0;
my $FTPhost;
my $FTPusr;
my $FTPpwd;
my @putf;
my $pf=0;
my @getf;
my $gf=0;
my @putd;
my $pd=0;
my @getd;
my $gd=0;

while ($i <= $#inidata) {
  my @tmp = split(' ',$inidata[$i]);
  ### PARAMETRE HOST ###
  if (uc($tmp[0]) eq "HOST") {
    $FTPhost = $tmp[1];
  }
  ### PARAMETRE LOGIN ###
  elsif (uc($tmp[0]) eq "LOGIN") {
    $FTPusr = $tmp[1];
  }
  ### PARAMETRE PASSWORD ###
  elsif (uc($tmp[0]) eq "PASSWORD") {
    $FTPpwd = $tmp[1];
  }
  ### PARAMETRES PUT FILES ###
  elsif (uc($tmp[0]) eq "PUTFILE") {
    $i++;
    @tmp = split(' ',$inidata[$i]);
    while (uc($tmp[0]) ne "ENDPUTFILE" && $i <= $#inidata) {
      $putf[$pf] = $inidata[$i];
      $pf++;
      $i++;
      @tmp = split(' ',$inidata[$i]);
    }
  }
  ### PARAMETRES GET FILES ###
  elsif (uc($tmp[0]) eq "GETFILE") {
    $i++;
    @tmp = split(' ',$inidata[$i]);
    while (uc($tmp[0]) ne "ENDGETFILE" && $i <= $#inidata) {
      $getf[$gf] = $inidata[$i];
      $gf++;
      $i++;
      @tmp = split(' ',$inidata[$i]);
    }
  }
  ### PARAMETRES PUT DIRS ###
  elsif (uc($tmp[0]) eq "PUTDIR") {
    $i++;
    @tmp = split(' ',$inidata[$i]);
    while (uc($tmp[0]) ne "ENDPUTDIR" && $i <= $#inidata) {
      $putd[$pd] = $inidata[$i];
      $pd++;
      $i++;
      @tmp = split(' ',$inidata[$i]);
    }
  }
  ### PARAMETRES GET DIRS ###
  elsif (uc($tmp[0]) eq "GETDIR") {
    $i++;
    @tmp = split(' ',$inidata[$i]);
    while (uc($tmp[0]) ne "ENDGETDIR" && $i <= $#inidata) {
      $getd[$gd] = $inidata[$i];
      $gd++;
      $i++;
      @tmp = split(' ',$inidata[$i]);
    }
  }
  $i++;
}


print "\n#############################\n";
print "######## CONNECT FTP ########\n";
print "#############################\n";
### CONNECT FTP ###
###################
my $ftp = Net::FTP->new($FTPhost, Debug => 0);
if (! $ftp) {
  die "Connection au serveur FTP $FTPhost KO.\n";
}
print "Connection au serveur FTP $FTPhost OK.\n";

### LOGIN FTP ###
#################
$ok = $ftp->login($FTPusr,$FTPpwd);
if (! $ok) {
  print $ftp->message();
  die "Login/password KO.\n";
}
print "Login/password OK.\n";

if ($#putf >= 0) {
  print "\n#############################\n";
  print "######## PUT FICHIER ########\n";
  print "#############################\n";
  $i=0;
  while ($i <= $#putf) {
    $coderetour = $coderetour + put_file($ftp,$putf[$i]);
    $i++;
  }
}

if ($#getf >= 0) {
  print "\n#############################\n";
  print "######## GET FICHIER ########\n";
  print "#############################\n";
  $i=0;
  while ($i <= $#getf) {
    $coderetour = $coderetour + get_file($ftp,$getf[$i]);
    $i++;
  }
}

if ($#putd >= 0) {
  print "\n################################\n";
  print "######## PUT REPERTOIRE ########\n";
  print "################################\n";
  $i=0;
  while ($i <= $#putd) {
    my @tmp = split(' ',$putd[$i]);
    my $ok = opendir(DIR,$tmp[0]);
    if (! $ok) {
      print "CAN'T OPEN DIR $tmp[0] !!!";
      $coderetour++;
    }
    else {
      my @mif = grep {!/^\.\.$/} grep {!/^\.$/} readdir(DIR);
      closedir(DIR);
      my $j=0;
      while ($j <= $#mif) {
        my $cmd = "$tmp[0]/$mif[$j] $tmp[1]/$mif[$j] $tmp[2]";
        $coderetour = $coderetour + put_file($ftp,$cmd);
        $j++;
      }
    }
    $i++;
  }
}


if ($#getd >= 0) {
  print "\n################################\n";
  print "######## GET REPERTOIRE ########\n";
  print "################################\n";
  $i=0;
  while ($i <= $#getd) {
    my @tmp = split(' ',$getd[$i]);
    my $rep_deb = $ftp->pwd();
    $ftp->cwd($tmp[0]);
    my @files = $ftp->ls();
    $ftp->cwd($rep_deb);
    my $j=0;
    while ($j<=$#files) {
      my $cmd = "$tmp[0]/$files[$j] $tmp[1]/$files[$j] $tmp[2]";
      $coderetour = $coderetour + get_file($ftp,$cmd);
      $j++;
    }
    $i++;
  }
}


if ($coderetour == 0) {
  print "\n\nTous le(s) transfert(s) sont OK.\n";
}
else {
  print "\n\nErreur(s) lors du transfert (KO).\n";
  $coderetour = 1;
}

$ftp->quit;
exit $coderetour;
