Index: trunk/pstamp/scripts/dquery_finish.pl
===================================================================
--- trunk/pstamp/scripts/dquery_finish.pl	(revision 18588)
+++ trunk/pstamp/scripts/dquery_finish.pl	(revision 18588)
@@ -0,0 +1,104 @@
+#!/bin/env perl
+
+# dquery_finish.pl
+
+use warnings;
+use strict;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+use Sys::Hostname;
+use IPC::Cmd 0.36 qw( can_run run );
+use File::Temp qw( tempfile );
+use File::Copy;
+
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
+use PS::IPP::Metadata::List qw( parse_md_list );
+
+use PS::IPP::Config qw($PS_EXIT_SUCCESS
+		       $PS_EXIT_UNKNOWN_ERROR
+		       $PS_EXIT_SYS_ERROR
+		       $PS_EXIT_CONFIG_ERROR
+		       $PS_EXIT_PROG_ERROR
+		       $PS_EXIT_DATA_ERROR
+		       $PS_EXIT_TIMEOUT_ERROR
+		       metadataLookupStr
+		       metadataLookupBool
+		       caturi
+		       );
+
+
+my ( $req_id, $req_name, $product, $dbname, $verbose, $save_temps );
+
+GetOptions(
+           'req_id=s'   => \$req_id,
+           'req_name=s' => \$req_name,
+           'product=s'  => \$product,
+	   'dbname=s'   => \$dbname,
+	   'verbose'    => \$verbose,
+	   'save-temps' => \$save_temps,
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+
+my $err = "";
+
+$err .= "--req_id is required\n" if !$req_id;
+$err .= "--req_name is required\n" if !$req_name;
+$err .= "--product is required\n" if !$product;
+
+die "$err" if $err;
+
+my $missing_tools;
+
+my $pstamptool  = can_run('pstamptool')  or (warn "Can't find pstamptool"  and $missing_tools = 1);
+my $dsreg  = can_run('dsreg')  or (warn "Can't find dsreg"  and $missing_tools = 1);
+
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit ($PS_EXIT_CONFIG_ERROR);
+}
+
+my $ipprc = PS::IPP::Config->new(); # IPP Configuration
+
+my $outputDataStoreRoot = metadataLookupStr($ipprc->{_siteConfig}, 'DATA_STORE_ROOT');
+exit ($PS_EXIT_CONFIG_ERROR) unless defined $outputDataStoreRoot; # lookup failure outputs a message
+
+my $response_file = "$outputDataStoreRoot/$product/$req_name/response.fits";
+
+my $request_fault = 0;
+if (-e $response_file) {
+
+    # the job generated a response file put it into the Data Store
+    # XXX: for now we have one response file. In the real system we may have multiple jobs with
+    # multiple response files
+
+    my $command = "echo 'response.fits|||table|' | $dsreg --add $req_name --product $product --list -";
+    $command .= " --type MOPS_DETECTABILITY_RESPONSE";
+    $command .= " --dbname $dbname" if $dbname;
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        $request_fault = $error_code >> 8;
+        print STDERR "Unable to perform $command return code: $request_fault";
+    }
+
+} else  {
+    print STDERR "detect_query response file not found\n";
+    $request_fault = $PS_EXIT_UNKNOWN_ERROR;
+}
+
+# set the request's state to stop
+{
+    my $command = "$pstamptool -processedreq -req_id $req_id -state stop -fault $request_fault";
+    $command   .= " -dbname $dbname" if $dbname;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        die("Unable to perform $command error code: $error_code");
+    }
+}
+
Index: trunk/pstamp/scripts/request_finish.pl
===================================================================
--- trunk/pstamp/scripts/request_finish.pl	(revision 18588)
+++ trunk/pstamp/scripts/request_finish.pl	(revision 18588)
@@ -0,0 +1,91 @@
+#!/bin/env perl
+
+# request_finish.pl
+
+use warnings;
+use strict;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+use Sys::Hostname;
+use IPC::Cmd 0.36 qw( can_run run );
+use File::Temp qw( tempfile );
+use File::Copy;
+
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
+use PS::IPP::Metadata::List qw( parse_md_list );
+
+use PS::IPP::Config qw($PS_EXIT_SUCCESS
+		       $PS_EXIT_UNKNOWN_ERROR
+		       $PS_EXIT_SYS_ERROR
+		       $PS_EXIT_CONFIG_ERROR
+		       $PS_EXIT_PROG_ERROR
+		       $PS_EXIT_DATA_ERROR
+		       $PS_EXIT_TIMEOUT_ERROR
+		       metadataLookupStr
+		       metadataLookupBool
+		       caturi
+		       );
+
+
+my ( $req_id, $req_name, $req_type, $product, $dbname, $verbose, $save_temps );
+
+GetOptions(
+           'req_id=s'   => \$req_id,
+           'req_name=s' => \$req_name,
+           'req_type=s' => \$req_type,
+           'product=s'  => \$product,
+	   'dbname=s'   => \$dbname,
+	   'verbose'    => \$verbose,
+	   'save-temps' => \$save_temps,
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+
+my $err = "";
+
+$err .= "--req_id is required\n" if !$req_id;
+$err .= "--req_type is required\n" if !$req_type;
+$err .= "--req_name is required\n" if !$req_name;
+
+die "$err" if $err;
+
+my $missing_tools;
+
+my $pstamptool  = can_run('pstamptool')  or (warn "Can't find pstamptool"  and $missing_tools = 1);
+my $pstamp_finish  = can_run('pstamp_finish.pl')  or (warn "Can't find pstamp_finish.pl"  and $missing_tools = 1);
+my $dquery_finish  = can_run('dquery_finish.pl')  or (warn "Can't find dquery_finish.pl"  and $missing_tools = 1);
+
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit ($PS_EXIT_CONFIG_ERROR);
+}
+
+my $finish_cmd;
+if ($req_type eq 'pstamp') {
+    $finish_cmd = $pstamp_finish;
+} elsif ($req_type eq 'dquery') {
+    $finish_cmd = $dquery_finish;
+}
+if ($finish_cmd) {
+    my $command = $finish_cmd . " --req_id $req_id --req_name $req_name --product $product";
+    $command   .= " --dbname $dbname" if $dbname;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        die("Unable to perform $command error code: $error_code");
+    }
+} else {
+    # Unknown request type.
+    # Since we don't have a req_name there's not much we can do so just the request's state to stop.
+    print STDERR "request  $req_id has unknown reqType $req_type\n" if $verbose;
+    my $command = "$pstamptool -processedreq -req_id $req_id -state stop -fault $PS_EXIT_DATA_ERROR";
+    $command   .= " -dbname $dbname" if $dbname;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        die("Unable to perform $command error code: $error_code");
+    }
+}
