Index: /trunk/pstamp/scripts/ppstamp_run.pl
===================================================================
--- /trunk/pstamp/scripts/ppstamp_run.pl	(revision 16590)
+++ /trunk/pstamp/scripts/ppstamp_run.pl	(revision 16591)
@@ -15,10 +15,12 @@
 my $job_id = $ARGV[0];
 
-my $verbosity = 0;
+my $verbosity = 1;
 
 use Sys::Hostname;
 my $host = hostname();
+
 print "\n\n";
 print "Starting script $0 on $host\n\n";
+
 use IPC::Cmd 0.36 qw( can_run run );
 
@@ -58,10 +60,11 @@
         run(command => $command, verbose => $verbosity);
     unless ($success) {
-        die("Unable to perform pstamptool -pendingreq: $error_code");
+        die("Unable to perform pstamptool -pendingjob: $error_code");
     }
 
     if (@$stdout_buf == 0) {
         print STDERR "pending pstamp job id $job_id not found\n";
-        exit 1;
+        # exit 1;
+        exit 0;
     }
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
@@ -82,17 +85,4 @@
 my $outputBase = $psjob->{outputBase};
 my $argString = $psjob->{args};
-#my $request_id = $psjob->{req_id};
-
-#
-# update the state of this job from 'new' to 'run'
-#
-{
-    my $command = "$pstamptool -processedjob -job_id $job_id -state run";
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => $verbosity);
-    unless ($success) {
-        die("Unable to perform $command: $error_code");
-    }
-}
 
 my $jobStatus;
@@ -103,18 +93,16 @@
 
     if ($success) {
-        $jobStatus = "ok";
+        $jobStatus = $PS_EXIT_SUCCESS;
     } else {
-        $jobStatus = $error_code;
-        print STDERR "ppstamp failed with error code $error_code\n";
+        # run shifts the unix exit status up by 8 bits for some reason
+        $jobStatus = $error_code >> 8;
+        print STDERR "ppstamp failed with error code: $jobStatus\n";
     }
-#    unless ($success) {
-#        die("Unable to perform $command: $error_code");
-#    }
 }
 
 
-# stop the job and set the error status
+# stop the job and set the result value
 {
-    my $command = "$pstamptool -processedjob -job_id $job_id -state stop -status $jobStatus";
+    my $command = "$pstamptool -processedjob -job_id $job_id -state stop -result $jobStatus";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => $verbosity);
@@ -124,8 +112,3 @@
 }
 
-
-if ($jobStatus eq "ok") {
-    exit 0;
-} else {
-    exit $jobStatus;
-}
+exit $jobStatus;
Index: /trunk/pstamp/scripts/pstamp_dorequest.pl
===================================================================
--- /trunk/pstamp/scripts/pstamp_dorequest.pl	(revision 16590)
+++ /trunk/pstamp/scripts/pstamp_dorequest.pl	(revision 16591)
@@ -4,5 +4,7 @@
 ###
 ###     Excecute the jobs for a given request.
-###     Note: This is intended for testing outside of pantasks environment
+###
+###     Note: This program is not part of the postage stamp server
+###     It is intended for testing outside of pantasks environment
 ###
 
Index: /trunk/pstamp/scripts/pstamp_parser_run.pl
===================================================================
--- /trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 16591)
+++ /trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 16591)
@@ -0,0 +1,169 @@
+#!/bin/env perl
+###
+### pstampparser_run.pl
+###     Run the postage stamp request parser for a given request id
+###
+
+use warnings;
+use strict;
+
+use Sys::Hostname;
+my $host = hostname();
+print "\n\n";
+print "Starting script $0 on $host\n\n";
+
+if (@ARGV != 1) {
+    die "usage: $0 request_id\n";
+}
+
+my $request_id = $ARGV[0];
+
+# XXX: CONFIGURATION: get these from a configuration file
+my $inputWorkdir = "/export/data1/bills/pstamp/work/$request_id";
+my $outputDataStoreRoot = "/var/www/html/ds/dsroot";
+my $defaultOutputRoot = $outputDataStoreRoot;
+
+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 $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 $dsget = can_run('dsget') or (warn "Can't find dsget" 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
+
+my $psrequest;
+#Look up the uri for the given request
+{
+    my $command = "$pstamptool -pendingreq -req_id $request_id";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => 1);
+    unless ($success) {
+        die("Unable to perform $command error code: $error_code");
+    }
+
+    if (@$stdout_buf == 0) {
+        print STDERR "pending pstamp request id $request_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);
+
+    $psrequest = ${@$requests}[0];
+}
+
+if (!$psrequest) {
+    print STDERR "postage stamp request $request_id not found\n";
+    # TODO: is this always an error, what if the request is no longer pending?
+    exit 1;
+}
+
+my $uri = $psrequest->{uri};
+
+# if the uri is remote, download it 
+# XXX how can we tell?
+# perhaps assume remote if the uri begins with http: 
+# or perhaps assume it is remote if it doesn't begin with '/' or path://
+# actually since we're using dsget which is an http program the former is the
+# way to go. Now it's easier than that, we can assume that dsget is needed if
+# request->{ds_id} != 0
+
+my $ds_id = $psrequest->{ds_id};
+
+if ($ds_id) {
+    mkdir $inputWorkdir;
+    {
+        my $new_uri = "$inputWorkdir/request.fits";
+        my $command = "$dsget --uri $uri --filename $new_uri";
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => 1);
+        unless ($success) {
+            die("Unable to perform $command error code: $error_code");
+        }
+
+        $uri = $new_uri;
+    }
+}
+
+#  create the output diredtory
+my $outdir;
+if ($ds_id) {
+    my $command = "$pstamptool -datastore -ds_id $ds_id";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => 1);
+    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];
+
+    $outdir = "$outputDataStoreRoot/$psrequest->{outFileset}";
+} else {
+    $outdir = "$defaultOutputRoot/$psrequest->{outFileset}";
+}
+
+if (! -e $outdir ) {
+    mkdir $outdir or die("unable to create output directory $outdir");
+} elsif (! -d $outdir) {
+    die("file with name of output directory $outdir exists but is not a directory");
+}
+
+# run pstampparse to parse the queue the jobs for this request
+{
+    my $command = "$pstampparse -mode queue_job -req_id $request_id -out_dir $outdir $uri";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => 1);
+    unless ($success) {
+        die("Unable to perform $command error code: $error_code");
+    }
+}
+
+# XXX make sure that a job got actually got queued. If not set the request result and set state to
+# run so that the "request finisher" can build the results file.
+
+#
+# update the state of this request from 'new' to 'run'
+#
+{
+    my $command = "$pstamptool -processedreq -req_id $request_id -state run";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => 1);
+    unless ($success) {
+        die("Unable to perform $command error code: $error_code");
+    }
+}
+
+exit 0;
Index: /trunk/pstamp/scripts/pstamp_runcommand.sh
===================================================================
--- /trunk/pstamp/scripts/pstamp_runcommand.sh	(revision 16590)
+++ /trunk/pstamp/scripts/pstamp_runcommand.sh	(revision 16591)
@@ -2,7 +2,9 @@
 ###
 #
-# pstampwrapper.sh: 
-# Set up the ipp environment to run a ipp commands on behalf of hte postage stamp
+# pstamp_runcommand.sh: 
+#
+# Set up the ipp environment to run a ipp commands on behalf of the postage stamp
 # server and execute the command given by the argument list 
+# This script is used by cgi or php scripts.
 
 if [[ $# == 0 ]] ;then
@@ -15,9 +17,6 @@
 
 # These variables need to be customized for a particular installation
+# XXX: why not pass these on on the command line ?
 export PSCONFDIR=/export/data0/bills/psconfig
-
-# TODO shouldn't need to do this but there seems to be a psconfig problem.
-# Gene fixed this recently
-#export LD_LIBRARY_PATH=/usr/local/lib
 
 WORK_DIR=/export/data1/bills/pstamp/work
@@ -30,21 +29,16 @@
 cd $WORK_DIR
 status=$?
-if [[ ! $status ]] ; then
+if [[ $status != 0 ]] ; then
     echo $0 cannot cd to WORK_DIR: $WORK_DIR status: $status >&2
     exit $status
 fi
 
-# TODO: I do this so that I have permissions to modify the files created by 
-# the web server user apache.
-#umask 0
-
 ###
-### configure IPP
+### configure the IPP
 ###
-#export MANPATH=""
 
 source $PSCONFDIR/psconfig.bash default
 status=$?
-if [[ ! $status ]] ; then
+if [[ $status != 0 ]] ; then
     echo error setting up IPP environment >&2
     exit $status
@@ -53,11 +47,12 @@
 ###
 ### Add our command directory to the path. 
-### TODO: This won't be necessary once the scripts get installed as part of the IPP
+### XXX: This won't be necessary once the scripts get installed as part of the IPP
 ###
 PATH=${CMD_DIR}:${PATH}
 
-## This test is sort of redundant. 
-## We'll get a more useful error by just going ahead and trying the command
-## or by not suppressing the output from which
+## make sure we are able to run the desired command
+## XXX: This test is sort of redundant. 
+## We'd get a more useful error by just going ahead and trying the command
+## or by not suppressing the error output of which
 if [[ ! -x `which $1 2> /dev/null` ]] ;then
     status=$?
Index: unk/pstamp/scripts/pstampparser_run.pl
===================================================================
--- /trunk/pstamp/scripts/pstampparser_run.pl	(revision 16590)
+++ 	(revision )
@@ -1,102 +1,0 @@
-#!/bin/env perl
-###
-### run_pstampparser.pl
-###     Run the postage stamp request parser for a given request id
-###
-
-use warnings;
-use strict;
-
-use Sys::Hostname;
-my $host = hostname();
-print "\n\n";
-print "Starting script $0 on $host\n\n";
-
-if (@ARGV != 1) {
-    die "usage: $0 request_id\n";
-}
-
-my $request_id = $ARGV[0];
-
-
-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 $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);
-
-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
-
-my $psrequest;
-#Look up the uri for the given request
-{
-    my $command = "$pstamptool -pendingreq -req_id $request_id";
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 0);
-    unless ($success) {
-        die("Unable to perform pstamptool -pendingreq: $error_code");
-    }
-
-    if (@$stdout_buf == 0) {
-        print STDERR "pending pstamp request id $request_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);
-
-    $psrequest = ${@$requests}[0];
-}
-
-if (!$psrequest) {
-    print STDERR "postage stamp request $request_id not found\n";
-    # TODO: is this always an error, what if the request is no longer pending?
-    exit 1;
-}
-
-my $uri = $psrequest->{uri};
-{
-    my $command = "$pstampparse -mode queue_job -req_id $request_id $uri";
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 0);
-    unless ($success) {
-        die("Unable to perform pstampparse -pendingreq: $error_code");
-    }
-}
-
-#
-# update the state of this request from 'new' to 'run'
-#
-{
-    my $command = "$pstamptool -processedreq -req_id $request_id -state run";
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 0);
-    unless ($success) {
-        die("Unable to perform $command: $error_code");
-    }
-}
-
-exit 0;
