Index: trunk/pstamp/scripts/pstamp_parser_run.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 21385)
+++ trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 21410)
@@ -12,13 +12,20 @@
 use Sys::Hostname;
 use Getopt::Long qw( GetOptions );
-
+use File::Basename qw( basename dirname);
+
+my $req_id;
+my $uri;
+my $redirect_output;
+my $product;
 my $verbose;
 my $dbname;
-my $req_id;
 
 GetOptions(
-    'verbose'   =>  \$verbose,
-    'dbname=s'  =>  \$dbname,
-    'req_id=s'  =>  \$req_id,
+    'req_id=s'          =>  \$req_id,
+    'uri=s'             =>  \$uri,
+    'product=s'         =>  \$product,
+    'redirect-output'   =>  \$redirect_output,
+    'verbose'           =>  \$verbose,
+    'dbname=s'          =>  \$dbname,
 );
 
@@ -29,5 +36,8 @@
 }
 
-die "req_id is required" if !defined($req_id);
+die "--req_id --uri --product are required" 
+    if !defined($req_id) or
+       !defined($uri) or
+       !defined($product);
 
 use IPC::Cmd 0.36 qw( can_run run );
@@ -66,4 +76,9 @@
 }
 
+if ($redirect_output) {
+    my $logDest = "$workdir/psparse.$req_id.log";
+    $ipprc->redirect_output($logDest);
+}
+
 my $defaultDSProduct = metadataLookupStr($ipprc->{_siteConfig}, 'PSTAMP_DATA_STORE_PRODUCT');
 exit ($PS_EXIT_CONFIG_ERROR) unless defined $defaultDSProduct;
@@ -83,9 +98,10 @@
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
-my $psrequest;
-#Look up the uri for the given request
-{
-    my $command = "$pstamptool -pendingreq -req_id $req_id";
-    $command .= " -dbname $dbname" if $dbname;
+
+my $fn = basename($uri);
+my $new_uri = "$workdir/$fn";
+if ($uri =~ /^http:/) {
+    # if the uri is an http uri download the file 
+    my $command = "$dsget --uri $uri --filename $new_uri";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => $verbose);
@@ -93,82 +109,21 @@
         die("Unable to perform $command error code: $error_code");
     }
-
-    if (@$stdout_buf == 0) {
-        print STDERR "pending pstamp request id $req_id not found\n";
-        exit 1;
-    }
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-        die("Unable to parse metdata config doc");
-
-    my $requests = parse_md_list($metadata);
-
-    my $num = @$requests;
-    die "unexpected number of requests $num found" if $num > 1;
-
-    $psrequest = $requests->[0];
-}
-
-if (!$psrequest) {
-    print STDERR "pending postage stamp request $req_id not found\n";
-    exit 1;
-}
-
-my $uri = $psrequest->{uri};
-
-# if the uri is from a data store, download it 
-
-my $ds_id = $psrequest->{ds_id};
-
-my $new_uri;
-if ($ds_id && ($uri =~ /^http:/)) {
-    {
-        $new_uri = "$workdir/request.fits";
-        my $command = "$dsget --uri $uri --filename $new_uri";
-        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");
-        }
-
-        $uri = $new_uri;
-    }
-}
-
-#  find the name of the output directory
-my $product;
-if ($ds_id) {
-    my $command = "$pstamptool -datastore -ds_id $ds_id";
-    $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");
-    }
-
-    if (@$stdout_buf == 0) {
-        print STDERR "data store with data store id $ds_id not found\n";
-        exit 1;
-    }
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-        die("Unable to parse metdata config doc");
-
-    my $dataStores = parse_md_list($metadata);
-
-    my $dataStore = $dataStores->[0];
-
-    $product = $dataStore->{outProduct};
-} else {
+} elsif ($uri ne $new_uri) {
+    # put a link to the file into the workdir
+    if (-e $new_uri) {
+        unlink $new_uri or die "failed to unlink $new_uri";
+    }
+    if (! symlink $uri, $new_uri) {
+        die ("failed to link request file $uri to workdir $workdir");
+    }
+}
+$uri = $new_uri;
+
+die "request file $uri not found" if ! -e $uri;
+
+#  if product was not defined (in database), use the default
+if ($product eq "NULL") {
     $product = $defaultDSProduct;
 }
-
-#
-# Here is one of the places that we assume that the output for a request goes directly into
-# the output fileset directory in the data store.
-#
-my $outProductDir = "$outputDataStoreRoot/$product";
-
-die "output product directory $outProductDir does not exist" if (! -e $outProductDir) ;
-die "file with name of output product directory $outProductDir exists but is not a directory" 
-    if (! -d $outProductDir);
 
 my $parse_cmd;
@@ -220,6 +175,7 @@
 }
 
-$parse_cmd .= " --mode queue_job --req_id $req_id --product $product --out_dir $outProductDir --file $uri";
+$parse_cmd .= " --mode queue_job --req_id $req_id --product $product --out_dir $workdir --file $uri";
 $parse_cmd .= " --dbname $dbname" if $dbname;
+$parse_cmd .= " --verbose" if $verbose;
 
 my $newState = "run";
