Index: trunk/ippScripts/scripts/dist_queue_runs.pl
===================================================================
--- trunk/ippScripts/scripts/dist_queue_runs.pl	(revision 24031)
+++ trunk/ippScripts/scripts/dist_queue_runs.pl	(revision 24038)
@@ -98,4 +98,18 @@
 }
 
+# queue rcRuns for any distRuns that have completed and have interested destinations
+{
+    my $command = "$disttool -queuercrun";
+    $command .= " -dbname $dbname" if defined $dbname;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform $command error_code: $error_code", $error_code);
+    }
+    # display the output from the command
+    print STDERR join "", @$stdout_buf if $verbose;
+}
+
 exit 0;
 
Index: trunk/ippScripts/scripts/receive_advance.pl
===================================================================
--- trunk/ippScripts/scripts/receive_advance.pl	(revision 24038)
+++ trunk/ippScripts/scripts/receive_advance.pl	(revision 24038)
@@ -0,0 +1,137 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "Starting script $0 on $host\n\n";
+
+use DateTime;
+my $mjd_start = DateTime->now->mjd;   # MJD of starting script
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Config 1.01 qw( :standard );
+use File::Temp qw( tempfile );
+use File::Basename qw( basename );
+use Carp;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $receivetool = can_run('receivetool') or (warn "Can't find receivetool" and $missing_tools = 1);
+my $receive_setstatus = can_run('receive_setstatus.pl') or (warn "Can't find receive_setstatus.pl" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+# Parse the command-line arguments
+my ( $fileset_id, $fileset, $dbinfo_uri, $product, $ds_dbname, $ds_dbhost, $dbname, $verbose, $no_update, $save_temps );
+
+GetOptions(
+           'fileset_id=s'      => \$fileset_id,# database id for the fileset
+           'fileset=s'         => \$fileset,   # fileset name
+           'dbinfo_uri=s'      => \$dbinfo_uri,# uri for the database info file
+           'status_product=s'  => \$product,   # Product for status update
+           'ds_dbname=s'       => \$ds_dbname, # data store host
+           'ds_dbhost=s'       => \$ds_dbhost, # data store dbname
+           'dbname=s'          => \$dbname,    # Database name
+           'verbose'           => \$verbose,   # Print to stdout
+           'no-update'         => \$no_update, # Don't update the database?
+           'save-temps'        => \$save_temps, # Save temporary files?
+           ) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --file_id --source --product --fileset --file --workdir",
+           -exitval => $PS_EXIT_CONFIG_ERROR) unless
+    defined $fileset_id;
+
+my $ipprc = PS::IPP::Config->new() or
+    &my_die( "Unable to set up", $fileset_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
+
+# update the fileset entry
+
+if ($dbinfo_uri and ($dbinfo_uri ne "NULL")) {
+    my $filename = basename($dbinfo_uri);
+    my ($stage) = $filename =~ m|^dbinfo\.(\S+)\.\d+\.mdc$|; # Stage of interest
+    my $tool_name;
+    if ($stage eq "raw") {
+        $tool_name = "regtool";
+    } else {
+        $tool_name = "${stage}tool";
+    }
+    my $tool = can_run("$tool_name") or &my_die("Can't find tool to load $dbinfo_uri\n", $fileset_id, $PS_EXIT_CONFIG_ERROR);
+
+    my $file = $ipprc->file_resolve($dbinfo_uri);
+    &my_die("Unable to resolve $dbinfo_uri\n", $PS_EXIT_UNKNOWN_ERROR) unless $file;
+
+    my $command = "$tool -importrun -infile $file"; # Command to execute
+    $command .= " -dbname $dbname" if defined $dbname;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    &my_die("Unable to load $file\n", $fileset_id, $PS_EXIT_UNKNOWN_ERROR) unless $success;
+
+    # XXX: once the dbinfo file is imported we cannot revert this fileset
+}
+
+if ($product) {
+    my $command = "$receive_setstatus --dbname $ds_dbname --status_product $product";
+    $command .= " --received_fs_name $fileset --fault 0";
+    if ($ds_dbname) {
+        $command .= " --dbname $ds_dbname";
+    } else {
+        # XXX: is falling back to the other database a good idea?
+        $command .= " --dbname $dbname" if defined $dbname;
+    }
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    &my_die("Unable to set transfer status on data store for $fileset_id\n", $fileset_id, $PS_EXIT_UNKNOWN_ERROR) unless $success;
+}
+
+# All done
+{
+    my $command = "$receivetool -updatefileset -fileset_id $fileset_id -set_state full";
+    $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 add result for $fileset_id\n", $fileset_id, $PS_EXIT_CONFIG_ERROR) unless $success;
+    }
+}
+
+# Pau.
+
+
+sub my_die
+{
+    my $msg = shift;            # Exit message
+    my $fileset_id = shift;     # Fileset identifier
+    my $fault = shift;          # Fault code
+
+    $fault = $PS_EXIT_PROG_ERROR unless defined $fault;
+
+    carp($msg);
+    if (defined $fileset_id and 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);
+    }
+    exit $fault;
+}
+
+
+__END__
Index: trunk/ippScripts/scripts/receive_file.pl
===================================================================
--- trunk/ippScripts/scripts/receive_file.pl	(revision 24031)
+++ trunk/ippScripts/scripts/receive_file.pl	(revision 24038)
@@ -36,5 +36,5 @@
 
 # Parse the command-line arguments
-my ( $file_id, $source, $product, $fileset, $file, $workdir, $dbname, $verbose, $no_update, $save_temps );
+my ( $file_id, $source, $product, $fileset, $fileset_id, $file, $workdir, $dbname, $verbose, $no_update, $save_temps );
 
 GetOptions(
@@ -43,4 +43,5 @@
            'product=s'         => \$product, # Product for data
            'fileset=s'         => \$fileset, # Fileset for data
+           'fileset_id=s'      => \$fileset_id, # database id for the fileset
            'file=s'            => \$file, # File to retrieve
            'workdir=s'         => \$workdir, # Working directory for output
@@ -61,4 +62,6 @@
     defined $workdir;
 
+$tempdir .= "/$file_id";
+
 my $ipprc = PS::IPP::Config->new() or
     &my_die( "Unable to set up", $file_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
@@ -80,6 +83,11 @@
     # Load into database
 
+    my $target = "$workdir/$file"; # Target destination for file
+    my $fixName = $ipprc->file_create( $target ); # Target for move
+    my $fixFile;
+
+    open $fixFile, ">$fixName" or die "can't open $fixName\n";
+
     # Need to fix paths to point to new workdir
-    my ($fixFile, $fixName) = tempfile( "$tempdir/$file.XXXX", UNLINK => !$save_temps ); # Fixed file
     open my $inFile, $filename or die "Can't open $filename\n"; # Input file
     my $workdir_old;            # Old workdir
@@ -98,13 +106,13 @@
     }
     close($inFile);
-
-    my ($stage) = $file =~ m|^dbinfo\.(\S+)\.\d+\.mdc$|; # Stage of interest
-    my $tool = can_run("${stage}tool") or die "Can't find tool to load $file\n";
-
-    my $command = "$tool -importrun -infile $fixName"; # Command to execute
+    close($fixFile);
+#{
+    my $command = "$receivetool -updatefileset -fileset_id $fileset_id -dbinfo_uri $fixName"; # Command to execute
     $command .= " -dbname $dbname" if defined $dbname;
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => $verbose);
-    die "Unable to load $fixName\n" unless $success;
+    die "Unable to set dbinfo_uri for $fileset_id to  $fixName\n" unless $success;
+#}
+
 } elsif ($file =~ m|.*\.tgz$|) {
     # Get contents of tarball
Index: trunk/ippScripts/scripts/receive_setstatus.pl
===================================================================
--- trunk/ippScripts/scripts/receive_setstatus.pl	(revision 24031)
+++ trunk/ippScripts/scripts/receive_setstatus.pl	(revision 24038)
@@ -51,10 +51,13 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --dest_id --status_uri",
+pod2usage( -msg => "Required options: --status_fs_name --status_product",
            -exitval => 3) unless
-    defined $status_fs_name and
+    defined $received_fs_name and
     defined $status_product and
     defined $fault;
 #    and ($fault == 0 or defined $status_file);
+
+# if a name for the status fileset is not provided use the received value
+$status_fs_name = $received_fs_name if !defined $status_fs_name;
 
 $ipprc->redirect_output($logfile) if $logfile;
