add -b flag for binary mode, RT#23382
[freeside.git] / FS / bin / freeside-cdr-sftp_and_import
index 039941c..77b0834 100755 (executable)
@@ -12,8 +12,8 @@ use FS::cdr;
 # parse command line
 ###
 
-use vars qw( $opt_m $opt_p $opt_r $opt_e $opt_d $opt_v $opt_P $opt_a $opt_c $opt_g );
-getopts('c:m:p:r:e:d:v:P:ag');
+use vars qw( $opt_m $opt_p $opt_r $opt_e $opt_d $opt_v $opt_P $opt_a $opt_c $opt_g $opt_b );
+getopts('c:m:p:r:e:d:v:P:agb');
 
 $opt_e ||= 'csv';
 #$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
@@ -105,10 +105,12 @@ foreach my $filename ( @$ls ) {
       }
   }
 
+  my $file_timestamp = $filename.'-'.time2str('%Y-%m-%d', time);
+
   my $import_options = {
     'file'            => "$cachedir/$ungziped",
     'format'          => $format,
-    'batch_namevalue' => $filename.'-'.time2str('%Y-%m-%d', time),
+    'batch_namevalue' => $file_timestamp,
     'empty_ok'        => 1,
   };
   $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
@@ -123,7 +125,7 @@ foreach my $filename ( @$ls ) {
   if ( $opt_d ) {
     if($opt_m eq 'ftp') {
       my $ftp = ftp();
-      $ftp->rename($filename, "$opt_d/$filename")
+      $ftp->rename($filename, "$opt_d/$file_timestamp")
         or do {
           unlink "$cachedir/$filename";
           unlink "$cachedir/$ungziped" if $opt_g;
@@ -132,7 +134,7 @@ foreach my $filename ( @$ls ) {
     }
     else {
       my $sftp = sftp();
-      $sftp->rename($filename, "$opt_d/$filename")
+      $sftp->rename($filename, "$opt_d/$file_timestamp")
         or do {
           unlink "$cachedir/$filename";
           unlink "$cachedir/$ungziped" if $opt_g;
@@ -166,6 +168,7 @@ sub ftp {
     or die "FTP connection to '$hostname' failed.";
   $ftp->login($ftp_user, $ftp_pass) or die "FTP login failed: ".$ftp->message;
   $ftp->cwd($opt_r) or die "can't chdir to $opt_r\n" if $opt_r;
+  $ftp->binary or die "can't set BINARY mode: ". $ftp->message if $opt_b;
   return $ftp;
 }
 
@@ -211,6 +214,8 @@ or FTP and then import them into the database.
 
 -a: use ftp passive mode
 
+-b: use ftp binary mode
+
 -v: set verbosity level; this script only has one level, but it will 
     be passed as the 'debug' argument to the transport method