Index: trunk/pstamp/scripts/dqueryparse.pl
===================================================================
--- trunk/pstamp/scripts/dqueryparse.pl	(revision 18540)
+++ trunk/pstamp/scripts/dqueryparse.pl	(revision 18587)
@@ -5,6 +5,4 @@
 # Note: this file is currently only a placeholder which creates a fake response file
 # and adds a completed job to the database
-#
-# TODO: split off the actual generation of the response file into a standalone program
 #
 
@@ -28,5 +26,5 @@
 		       );
 
-my ($uri, $out_dir, $mode, $req_id, $verbose, $save_temps);
+my ($req_file, $req_id, $out_dir, $product, $mode, $dbname, $verbose, $save_temps);
 
 #
@@ -35,25 +33,29 @@
 
 GetOptions(
-        'uri=s'           =>      \$uri,
+        'file=s'          =>      \$req_file,
+        'req_id=s'        =>      \$req_id,
         'out_dir=s'       =>      \$out_dir,
+        'product=s'       =>      \$product,
+        'mode=s'          =>      \$mode,
+        'dbname=s'        =>      \$dbname,
         'verbose'         =>      \$verbose,
-        'mode=s'          =>      \$mode,
-        'req_id=s'        =>      \$req_id,
         'save-temps'      =>      \$save_temps,
-        'verbose'         =>      \$verbose,
 ) or pod2usage(2);
 
 my $err = "";
 
-if (!$uri) {
-    $err .= "--uri is required\n";
+if (!$req_file) {
+    $err .= "--file is required\n";
+}
+if (!$req_id) {
+    $err .= "--req_id is required\n";
 }
 if (!$out_dir) {
     $err .="--out_dir is required\n";
 }
+if (!$product) {
+    $err .="--product is required\n";
+}
 
-if (!$req_id) {
-    $err .= "--req_id is required\n";
-}
 
 die $err if ($err);
@@ -62,4 +64,5 @@
 my $pstamptool = can_run('pstamptool') or (warn "Can't find pstamptool" and $missing_tools =1);
 my $fakedresponse = can_run('fakedresponse.pl') or (warn "Can't find fakedresponse.pl" and $missing_tools =1);
+my $fields = can_run('fields') or (warn "Can't find fields" and $missing_tools =1);
 
 if ($missing_tools) {
@@ -68,8 +71,38 @@
 }
 
+# get the query id and check the extname and version from the header
+my $fields_output;
+{
+    my $command = "echo $req_file | $fields -x 0 EXTNAME EXTVER QUERY_ID";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    
+#   fields doesn't return zero when it succeeds
+#    unless ($success) {
+#        print STDERR @$stderr_buf;
+#    }
+    $fields_output = join "", @$stdout_buf;
+}
+my (undef, $extname, $extver, $req_name) = split " ", $fields_output;
+
+die "$req_file has EXTNAME $extname not MOPS_DETECTABILITY_QUERY table"
+                if $extname ne         "MOPS_DETECTABILITY_QUERY";
+die "$req_file does not have a QUERY_ID" if ! $req_name;
+die "$req_file is version $extver expecting 1" if $extver ne 1;
+
+$out_dir .= "/$req_name";
+if (! -e $out_dir ) {
+    mkdir $out_dir or die "cannot create output directory $out_dir";
+} elsif (! -d $out_dir ) {
+    die "output fileset directory $out_dir exists but is not a directory";
+}
+
+#
+# we don't parse the file here, pass it to fakedresponse it will create a response for each
+# row in the file
 my $response_file = "$out_dir/response.fits";
 my $fault;
 {
-    my $command = "$fakedresponse --input $uri --output $response_file --workdir $out_dir";
+    my $command = "$fakedresponse --input $req_file --output $response_file --workdir $out_dir";
     $command .= " --save-temps" if $save_temps;
     $command .= " --verbose" if $verbose;
@@ -84,7 +117,10 @@
 
 my $job_id;
+my $result;
 {
-    my $command = "$pstamptool -addjob -req_id $req_id -uri $uri -outputBase $out_dir -job_type detect_query";
-    $command .= " -state stop -fault $fault";
+    my $command = "$pstamptool -addjob -req_id $req_id -uri $req_file -outputBase $out_dir";
+    $command .= " -job_type detect_query -state stop -fault $fault";
+    $command .= " -rownum 1";
+    $command .= " -dbname $dbname" if $dbname;
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -101,17 +137,15 @@
 }
 
-exit $result;
+{
+    my $command = "$pstamptool -processedreq -req_id $req_id -name $req_name -outProduct $product";
+    $command .= " -fault $result" if $result;
+    $command .= " -dbname $dbname" if $dbname;
 
-
-
-
-sub fake_dquery_response {
-    my $line = shift;
-    my ($rownum, $ra1, $dec1, $ra2, $dec2, $mag) = split " ", $line;
-
-    # todo perhaps think more about these values
-    my $n = rand(16);
-    my $f = rand(1);
-
-    return ($rownum, $n, $f);
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        die "$command failed";
+    }
 }
+ 
+exit 0;
