Index: branches/pap/ippScripts/scripts/receive_fileset.pl
===================================================================
--- branches/pap/ippScripts/scripts/receive_fileset.pl	(revision 23948)
+++ branches/pap/ippScripts/scripts/receive_fileset.pl	(revision 25027)
@@ -21,4 +21,6 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+use File::Temp qw( tempfile );
+use Carp;
 
 # Look for programs we need
@@ -32,5 +34,5 @@
 
 # Parse the command-line arguments
-my ( $fileset_id, $source, $product, $fileset, $dbname, $verbose, $no_update );
+my ( $fileset_id, $source, $product, $fileset, $dbname, $verbose, $no_update, $save_temps );
 
 GetOptions(
@@ -42,4 +44,5 @@
            'verbose'           => \$verbose,   # Print to stdout
            'no-update'         => \$no_update, # Don't update the database?
+           'save-temps'        => \$save_temps, # keep temp files
     ) or pod2usage( 2 );
 
@@ -52,6 +55,7 @@
     defined $fileset;
 
-# Get list of files
-my @files = ();                 # Files to add
+# Get list of files, sanity check and then write it to a temporary file
+my $numFiles = 0;
+my ($listFile, $listName) = tempfile("/tmp/$product.$fileset.list.XXXX", UNLINK => !$save_temps);
 {
     my $uri = "$source/$product/$fileset"; # URI for datastore fileset
@@ -61,5 +65,5 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => $verbose);
-    die "Unable to get fileset listing from $uri\n" unless $success;
+    &my_die( "Unable to get fileset listing from $uri\n", $fileset_id, $error_code) unless $success;
 
     # Get files
@@ -69,14 +73,28 @@
         next unless $line =~ /\S+/;
         my @fields = split(/\s+/, $line); # Fields in line
-        my $file = $fields[1];      # Name of file
-        push @files, $file if defined $file;
+        &my_die( "too few columns in fileset list entry: $line\n", $fileset_id, $PS_EXIT_DATA_ERROR) unless scalar @fields >= 6;
+
+        print $listFile "FILE$numFiles\tMETADATA\n";
+
+        print $listFile "\turi\t\tSTR\t" . $fields[0] .  "\n";
+        print $listFile "\tfile\t\tSTR\t" . $fields[1] .  "\n";
+        print $listFile "\tbytes\t\tS64\t" . $fields[2] .  "\n";
+        print $listFile "\tmd5sum\t\tSTR\t" . $fields[3] .  "\n";
+        print $listFile "\tfile_type\tSTR\t" . $fields[4] .  "\n";
+        print $listFile "\tcomponent\tSTR\t" . $fields[5] .  "\n";
+
+        print $listFile "END\n";
+        $numFiles++;
     }
+    close $listFile;
 }
 
 # Add files
-if (scalar @files > 0) {
+my $new_state;
+if ($numFiles > 0) {
+    $new_state = "listed";
     my $command = "receivetool -addfile"; # Command to execute
     $command .= " -fileset_id $fileset_id";
-    $command .= " -file " . join(' -file ', @files);
+    $command .= " -file_list $listName";
     $command .= " -dbname $dbname" if defined $dbname;
 
@@ -84,7 +102,43 @@
         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
             run(command => $command, verbose => $verbose);
-        die "Unable to add file for fileset $fileset\n" unless $success;
+        &my_die( "Unable to add file list for fileset $fileset\n", $fileset_id, $error_code) unless $success;
+    }
+} else {
+    print STDERR "fileset $fileset has no files\n" if $verbose;
+    $new_state = "full";
+}
+
+{
+    my $command = "receivetool -updatefileset"; # Command to execute
+    $command .= " -fileset_id $fileset_id";
+    $command .= " -set_state $new_state";
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    unless (defined $no_update) {
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        &my_die( "Unable to set state to $new_state for fileset $fileset\n", $fileset_id, $error_code) unless $success;
     }
 }
 
+sub my_die {
+    my $msg = shift;            # Exit message
+    my $fileset_id = shift;     # File identifier
+    my $fault = shift;          # Fault code
+
+    $fault = $PS_EXIT_PROG_ERROR unless defined $fault;
+
+    carp($msg);
+    if (not $no_update) {
+        my $command = "$receivetool -updatefileset";
+        $command .= " -fileset_id $fileset_id";
+        $command .= " -fault $fault";
+        $command .= " -dbname $dbname" if defined $dbname;
+
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        die "Unable to set fault to $fault for fileset $fileset\n" unless $success;
+    }
+    exit $fault;
+}
 __END__
