+
+ 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 $file_timestamp = $filename.'-'.time2str('%Y-%m-%d', time);
+
+ my $import_options = {
+ 'file' => "$cachedir/$ungziped",
+ 'format' => $format,
+ 'batch_namevalue' => $file_timestamp,
+ 'empty_ok' => 1,
+ };
+ $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
+
+ my $error = FS::cdr::batch_import($import_options);
+
+ if ( $error ) {
+
+ if ( $opt_s ) {
+ warn "$ungziped: $error\n";
+ } else {
+ unlink "$cachedir/$filename";
+ unlink "$cachedir/$ungziped" if $opt_g;
+ die $error;
+ }
+
+ } else {
+
+ if ( $opt_d ) {
+ if ( $opt_m eq 'ftp') {
+ my $ftp = ftp();
+ $ftp->rename($filename, "$opt_d/$file_timestamp")
+ or do {
+ unlink "$cachedir/$filename";
+ unlink "$cachedir/$ungziped" if $opt_g;
+ die "Can't move $filename to $opt_d: ".$ftp->message . "\n";
+ };
+ } else {
+ my $sftp = sftp();
+ $sftp->rename($filename, "$opt_d/$file_timestamp")
+ or do {
+ unlink "$cachedir/$filename";
+ unlink "$cachedir/$ungziped" if $opt_g;
+ die "can't move $filename to $opt_d: ". $sftp->error . "\n";
+ };
+ }
+ }