Index: trunk/pstamp/scripts/pstamp_parser_run.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 18247)
+++ trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 18380)
@@ -67,5 +67,5 @@
 
 my $pstamptool  = can_run('pstamptool')  or (warn "Can't find pstamptool"  and $missing_tools = 1);
-my $pstampparse = can_run('pstampparse') or (warn "Can't find pstampparse" and $missing_tools = 1);
+my $pstampparse = can_run('pstampparse.pl') or (warn "Can't find pstampparse.pl" and $missing_tools = 1);
 my $dqueryparse = can_run('dqueryparse.pl') or (warn "Can't find dqueryparse.pl" and $missing_tools = 1);
 my $dsget = can_run('dsget') or (warn "Can't find dsget" and $missing_tools = 1);
@@ -179,5 +179,5 @@
 my $parse_cmd;
 if ($request_type eq "PS1_PS_REQUEST") {
-    $parse_cmd = $pstampparse . " -mode queue_job -req_id $request_id -out_dir $outdir $uri";
+    $parse_cmd = $pstampparse . " -mode queue_job -req_id $request_id -out_dir $outdir -file $uri";
     $parse_cmd .= " -dbname $dbname" if $dbname;
 } elsif ($request_type eq "MOPS_DETECTABILITY_QUERY") {
Index: trunk/pstamp/scripts/pstampparse.pl
===================================================================
--- trunk/pstamp/scripts/pstampparse.pl	(revision 18380)
+++ trunk/pstamp/scripts/pstampparse.pl	(revision 18380)
@@ -0,0 +1,189 @@
+#!/bin/env perl
+###
+### pstampparse.pl
+###
+###     Run the postage stamp request parser for a given request id
+###
+
+use warnings;
+use strict;
+
+use Sys::Hostname;
+use Getopt::Long qw( GetOptions );
+
+my $verbose;
+my $dbname;
+my $req_id;
+my $request_file_name;
+my $mode = "list_uri";
+my $out_dir = ".";
+
+# values for COORD_MASK
+use constant PSTAMP_CENTER_IN_PIXELS => 1;
+use constant PSTAMP_RANGE_IN_PIXELS  => 2;
+
+# values for OPTION_MASK
+use constant PSTAMP_SELECT_IMAGE => 1;
+use constant PSTAMP_SELECT_MASK  => 2;
+use constant PSTAMP_SELECT_WEIGHT => 4;
+
+GetOptions(
+    'file=s'    =>  \$request_file_name,
+    'req_id=s'  =>  \$req_id,
+    'out_dir=s' =>  \$out_dir,
+    'mode=s'    =>  \$mode,
+    'dbname=s'  =>  \$dbname,
+    'verbose'   =>  \$verbose,
+);
+
+die "req_id is required"   if !defined($req_id);
+die "file is required"     if !defined($request_file_name);
+die "out_dir is required"  if !defined($out_dir);
+
+die "invalid mode '$mode'" unless ($mode eq "list_uri") or ($mode eq "list_job") or ($mode eq "queue_job");
+
+use IPC::Cmd 0.36 qw( can_run run );
+
+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 $ipprc = PS::IPP::Config->new(); # IPP Configuration
+
+my $missing_tools;
+
+my $pstamptool  = can_run('pstamptool')  or (warn "Can't find pstamptool"  and $missing_tools = 1);
+my $pstampparse = can_run('pstampparse') or (warn "Can't find pstampparse" and $missing_tools = 1);
+my $pstampdump  = can_run('pstampdump') or (warn "Can't find pstampdump" and $missing_tools = 1);
+
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit ($PS_EXIT_CONFIG_ERROR);
+}
+
+my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
+#
+# convert the request table to an array of metadata config docs
+#
+# TODO: check the header for the correct EXTNAME
+#
+
+my $rows;
+{
+    my $command = "$pstampdump $request_file_name";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        print STDERR @$stderr_buf;
+    }
+    my $table =  $mdcParser->parse(join "", @$stdout_buf) or
+        die("Unable to parse metdata config doc");
+
+    $rows = parse_md_list($table);
+}
+
+# TODO: Make sure that the output directory exists 
+
+foreach my $row (@$rows) {
+    my $job_type = $row->{JOB_TYPE};
+    my $project  = $row->{PROJECT};
+    my $req_type = $row->{REQ_TYPE};
+    my $img_type = $row->{IMG_TYPE};
+    my $id       = $row->{ID};
+    my $class_id = $row->{CLASS_ID};
+    my $stamp_select = $row->{STAMP_SELECT};
+    my $stamp_name   = $row->{STAMP_NAME};
+    my $filter   = $row->{FILTER};
+    my $date_min = $row->{DATE_MIN};
+    my $date_max = $row->{DATE_MAX};
+    my $x = $row->{CENTER_X};
+    my $y = $row->{CENTER_Y};
+    my $w = $row->{WIDTH};
+    my $h = $row->{HEIGHT};
+    my $coord_mask = $row->{COORD_MASK};
+    my $option_mask= $row->{OPTION_MASK};
+
+    my $roi_string;
+    # XXX we're depending on other code to insure valid values for roi components
+    # this is checked in pstamprequest and ppstamp
+    # arguably I should check here to but not today
+    if ($x && ($x ne "null") && $y && ($y ne "null") && $w && ($w ne "null") && $h && ($h ne "null")) {
+        if ($coord_mask & PSTAMP_CENTER_IN_PIXELS) {
+            $roi_string = "-pixcenter $x $y";
+        } else {
+            $roi_string = "-skycenter $x $y";
+        }
+        if ($coord_mask & PSTAMP_RANGE_IN_PIXELS) {
+            $roi_string .= " -pixrange $w $h";
+        } else {
+            $roi_string .= " -arcrange $w $h";
+        }
+    }
+
+    die "region of interest is required for request type bycoord" 
+        if (($req_type eq "bycoord") && !defined($roi_string));
+
+    die "region of interest is required to make postage stamps"
+        if (($job_type eq "stamp") && !defined($roi_string));
+
+    my $args = $roi_string ? $roi_string : "";
+
+    # find the uris for this request
+
+    # XXX: replace the invokcation of the hacked pstampparse with a new ippTool that finds
+    # images/runs and prints out the uri and other metadata as a metadata config doc
+
+    my $command = "$pstampparse -dbname $project -$req_type $img_type $id $class_id";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+    unless ($success) {
+        print STDERR @$stderr_buf;
+    }
+    my @uris = split "^", join("", @$stdout_buf);
+
+    foreach my $uri (@uris) {
+        chomp $uri;
+        if ($mode eq "list_uri") {
+            print "$uri\n";
+        } else {
+
+            # TODO: here is where we need to check whether or not the source image still exists
+            # and queue a regeneration job. Also remove streaks here ???
+
+            $command = "$pstamptool -dbname $dbname -addjob -req_id $req_id -job_type $job_type"
+                . " -uri $uri -outputBase $out_dir/$stamp_name -args '$args'";
+            if ($mode eq "list_job") { 
+                # this is a debugging mode, just print the pstamptool that would have run
+                # this is sort of like the mode -noupdate that some other tools support
+                print "$command\n";
+            } else {
+                # mode eq "queue_job"
+                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                    run(command => $command, verbose => $verbose);
+                unless ($success) {
+                    print STDERR @$stderr_buf;
+                    # XXX TODO: now what? Should we mark the error state for the request?
+                    # should we keep going for other uris? If so how do we report that some
+                    # of the work that the request wanted isn't going to get done
+                    die "failed to queue job";
+                }
+            }
+        }
+    }
+}
+
+exit 0;
+
