view/set svc_phone forwardst and email from self-service, RT13622
[freeside.git] / FS / bin / freeside-cdr-sftp_and_import
index ba9d6f3..1d052ea 100755 (executable)
@@ -11,8 +11,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 );
-getopts('m:p:r:e:d:v:P:a');
+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');
 
 $opt_e ||= 'csv';
 #$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
@@ -20,6 +20,8 @@ $opt_e =~ s/^\.//;
 
 $opt_p ||= '';
 
+die "invalid cdrtypenum" if $opt_c && $opt_c !~ /^\d+$/;
+
 my %options = ();
 
 my $user = shift or die &usage;
@@ -91,13 +93,26 @@ foreach my $filename ( @$ls ) {
   }
 
   warn "Processing $filename\n" if $opt_v;
+  my $ungziped = $filename;
+  $ungziped =~ s/\.gz$//;
+  if ( $opt_g ) {
+      if(system('gunzip', "$cachedir/$filename") != 0) {
+        warn "gunzip of '$cachedir/$filename' failed" if $opt_v;
+        unlink "$cachedir/$filename";
+        next;
+      }
+  }
 
-  my $error = FS::cdr::batch_import( {
-    'file'            => "$cachedir/$filename",
+  my $import_options = {
+    'file'            => "$cachedir/$ungziped",
     'format'          => $format,
     'batch_namevalue' => $filename,
     'empty_ok'        => 1,
-  } );
+  };
+  $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
+  
+  my $error = FS::cdr::batch_import($import_options);
   die $error if $error;
 
   if ( $opt_d ) {
@@ -114,6 +129,7 @@ foreach my $filename ( @$ls ) {
   }
 
   unlink "$cachedir/$filename";
+  unlink "$cachedir/$ungziped" if $opt_g;
 
 }
 
@@ -155,13 +171,13 @@ sub sftp {
 
 =head1 NAME
 
-cdr.sftp_and_import - Download CDR files from a remote server via SFTP
+freeside-cdr-sftp_and_import - Download CDR files from a remote server via SFTP
 
 =head1 SYNOPSIS
 
   cdr.sftp_and_import [ -m method ] [ -p prefix ] [ -e extension ] 
     [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
-    [ -a ] user format [sftpuser@]servername
+    [ -a ] [ -c cdrtypenum ] user format [sftpuser@]servername
 
 =head1 DESCRIPTION
 
@@ -185,6 +201,8 @@ or FTP and then import them into the database.
 -v: set verbosity level; this script only has one level, but it will 
     be passed as the 'debug' argument to the transport method
 
+-c: cdrtypenum to set, defaults to none
+
 user: freeside username
 
 format: CDR format name