Index: unk/pstamp/scripts/dumpvars.sh
===================================================================
--- /trunk/pstamp/scripts/dumpvars.sh	(revision 16357)
+++ 	(revision )
@@ -1,15 +1,0 @@
-#!/bin/bash
-
-echo "    Command Line Arguments:"
-
-echo $*
-
-echo " "
-echo "    Environment:"
-
-env
-
-echo " "
-echo "    Shell Variables:"
-
-set
Index: /trunk/pstamp/scripts/ppstamp_run.pl
===================================================================
--- /trunk/pstamp/scripts/ppstamp_run.pl	(revision 16357)
+++ /trunk/pstamp/scripts/ppstamp_run.pl	(revision 16358)
@@ -15,5 +15,5 @@
 my $job_id = $ARGV[0];
 
-my $verbosity = 1;
+my $verbosity = 0;
 
 use Sys::Hostname;
@@ -125,3 +125,7 @@
 
 
-exit $jobStatus;
+if ($jobStatus eq "ok") {
+    exit 0;
+} else {
+    exit $jobStatus;
+}
Index: /trunk/pstamp/scripts/pstamp_dorequest.pl
===================================================================
--- /trunk/pstamp/scripts/pstamp_dorequest.pl	(revision 16357)
+++ /trunk/pstamp/scripts/pstamp_dorequest.pl	(revision 16358)
@@ -1,8 +1,8 @@
 #!/bin/env perl
 ###
-### pstampdo_req.pl
+### pstamp_dorequest.pl
 ###
-###     Run the jobs for a given request
-###     This is intended for running outside pantasks environment
+###     Excecute the jobs for a given request.
+###     Note: This is intended for testing outside of pantasks environment
 ###
 
@@ -16,5 +16,5 @@
 my $request_id = $ARGV[0];
 
-my $verbosity = 1;
+my $verbosity = 0;
 
 use Sys::Hostname;
@@ -77,6 +77,5 @@
     foreach my $job (@$jobs) {
         if ($job->{req_id} == $request_id) {
-            # there must be a better way to do this
-            print STDERR "adding $job->{job_id} to the list\n";
+            # print STDERR "adding $job->{job_id} to the list\n";
             $psjobs[@psjobs] = $job;
         }
@@ -85,6 +84,6 @@
 
 if (! @psjobs) {
+    # TODO: is this always an error, what if the job is no longer pending?
     print STDERR "no pending postage stamp jobs for request $request_id found\n";
-    # TODO: is this always an error, what if the job is no longer pending?
     exit 1;
 }
@@ -103,4 +102,13 @@
 # Update the state of the request
 #
+{
+    ## TODO: what about request status
+    my $command = "$pstamptool -processedreq -req_id $request_id -state stop"; 
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+    unless ($success) {
+        die("Unable to perform pstamptool -processedreq: $error_code");
+    }
+}
 
 exit 0;
Index: /trunk/pstamp/scripts/pstamp_listjobs.pl
===================================================================
--- /trunk/pstamp/scripts/pstamp_listjobs.pl	(revision 16358)
+++ /trunk/pstamp/scripts/pstamp_listjobs.pl	(revision 16358)
@@ -0,0 +1,89 @@
+#!/bin/env perl
+###
+### pstamp_listjobs.pl
+### list the job_id, state, and uri for the postage stamp jobs queued for a given 
+### postage stamp request id
+###
+
+use warnings;
+use strict;
+
+if (@ARGV != 1) {
+    die "usage: $0 request_id\n";
+}
+
+my $request_id = $ARGV[0];
+
+my $verbosity = 0;
+
+use Sys::Hostname;
+my $host = hostname();
+
+## This isn't a script to be launched by pantasks we probably want go get rid of this
+if ($verbosity) {
+    print STDERR "\n\n";
+    print STDERR "Starting script $0 on $host\n\n";
+}
+
+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 $ppstamp_run = can_run('ppstamp_run.pl') or (warn "Can't find ppstamp_run.pl" 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 @psjobs;
+#Look up the jobs for the given request_id
+{
+    my $command = "$pstamptool -listjob -req_id $request_id";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+    unless ($success) {
+        die("Unable to perform pstamptool -pendingreq: $error_code");
+    }
+
+    if (@$stdout_buf == 0) {
+        print STDERR "no pstamp jobs for request $request_id\n";
+        exit 1;
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        die("Unable to parse metdata config doc");
+
+    my $jobs = parse_md_list($metadata);
+
+    @psjobs = @$jobs;
+}
+
+if (! @psjobs) {
+    print STDERR "no postage stamp jobs for request $request_id found\n";
+    exit 1;
+}
+
+foreach my $job (@psjobs) {
+    print "$job->{job_id} $job->{state} $job->{outputBase}.fits\n";
+}
+
+exit 0;
Index: /trunk/pstamp/scripts/pstamp_runcommand.sh
===================================================================
--- /trunk/pstamp/scripts/pstamp_runcommand.sh	(revision 16358)
+++ /trunk/pstamp/scripts/pstamp_runcommand.sh	(revision 16358)
@@ -0,0 +1,70 @@
+#!/bin/sh
+###
+#
+# pstampwrapper.sh: 
+# Set up the ipp environment to run a ipp commands on behalf of hte postage stamp
+# server and execute the command given by the argument list 
+
+if [[ $# == 0 ]] ;then
+    echo "usage $0 command" >&2
+    #  EINVAL = 22
+    exit 22
+fi
+
+#### BEGIN LOCAL_CONFIGURATION
+
+# These variables need to be customized for a particular installation
+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
+CMD_DIR=/export/data0/bills/src/ipp/pstamp/scripts
+
+#### END LOCAL_CONFIGURATION
+
+export HOME=$WORK_DIR
+
+cd $WORK_DIR
+status=$?
+if [[ ! $status ]] ; 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
+###
+#export MANPATH=""
+
+source $PSCONFDIR/psconfig.bash default
+status=$?
+if [[ ! $status ]] ; then
+    echo error setting up IPP environment >&2
+    exit $status
+fi
+
+###
+### Add our command directory to the path. 
+### TODO: 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
+if [[ ! -x `which $1 2> /dev/null` ]] ;then
+    status=$?
+    echo command $1 not found >&2
+    exit $status
+fi
+
+#### Finally, execute the command given by the command line arguments
+
+$*
Index: /trunk/pstamp/scripts/pstamp_webrequest.pl
===================================================================
--- /trunk/pstamp/scripts/pstamp_webrequest.pl	(revision 16358)
+++ /trunk/pstamp/scripts/pstamp_webrequest.pl	(revision 16358)
@@ -0,0 +1,124 @@
+#!/bin/env perl
+###
+#
+# pstampwebrequest.sh: take a postage stamp request command line and process it
+#
+# The arguments are the command line parameters for the program pstamprequest
+#
+# The output is the list of uris for the resulting postage stamp files 
+# unless the argument -list is provided.
+#
+# In that case the selected "input" uris are listed on stdout
+#
+# Note: there's nothing really web specific about this program.
+#       with the exception that we set up the environment to allow a 
+#       random user (in this case apache ) to run ipp programs
+#
+#
+###
+
+use warnings;
+use strict;
+
+my $verbosity = 0;
+
+use Sys::Hostname;
+my $host = hostname();
+
+if ($verbosity) {
+    print "\n\n";
+    print "Starting script $0 on $host\n\n";
+}
+
+use IPC::Cmd 0.36 qw( can_run run );
+
+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 $pstamprequest = can_run('pstamprequest')  or (warn "Can't find pstamprequest"  and $missing_tools = 1);
+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 $pstampparser_run = can_run('pstampparser_run.pl')  or (warn "Can't find pstampparser_run.pl"  and $missing_tools = 1);
+
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit ($PS_EXIT_CONFIG_ERROR);
+}
+
+
+# if -list is used, it must be the first argument
+my $listMode;
+if ($ARGV[0] eq "-list" ) {
+    $listMode=1;
+} else  {
+    $listMode=0;
+}
+
+my $request_file = "request_file.$$.fits";
+{
+    my $command = "$pstamprequest $request_file @ARGV";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+    unless ($success) {
+        print STDERR "@$stderr_buf\n";
+        die("Unable to perform pstamprequest: $error_code");
+    }
+}
+
+
+# ok at this point we have a request file add it to the database (unless we're in listMode)
+if ($listMode == 1 ) {
+    ###
+    ### In list mode just parse the file print the output and we're done
+    ###
+    my $command = "$pstampparse $request_file";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+    unless ($success) {
+        die("Unable to perform pstampparse: $error_code");
+    }
+    ### print "Matching Images:\n";
+    print "@$stdout_buf";
+    exit 0;
+}
+
+# Queue the request
+my $request_id = 0;
+{
+
+    my $command = "$pstamptool -addreq -uri $request_file";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+    unless ($success) {
+        print STDERR "@$stderr_buf\n";
+        die("Unable to perform pstamptool -addreq: $error_code");
+    }
+    $request_id = ${$stdout_buf}[0];
+}
+
+# parse the file to queue the jobs
+{
+    my $command = "$pstampparser_run $request_id";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+    unless ($success) {
+        print STDERR "@$stderr_buf\n";
+        die("Unable to perform pstamparser_run -addreq: $error_code");
+    }
+}
+
+#                   note: $request_id still has a newline
+print "$request_id";
+
+exit 0;
Index: unk/pstamp/scripts/pstampwebrequest.sh
===================================================================
--- /trunk/pstamp/scripts/pstampwebrequest.sh	(revision 16357)
+++ 	(revision )
@@ -1,209 +1,0 @@
-#!/bin/bash
-###
-#
-# pstampwebrequest.sh: take a postage stamp request command line and process it
-#
-# The arguments are the command line parameters for the program pstamprequest
-#
-# The output is the list of uris for the resulting postage stamp files 
-# unless the argument -list is provided.
-#
-# In that case the selected "input" uris are listed on stdout
-#
-# Note: there's nothing really web specific about this program.
-#       with the exception that we set up the environment to allow a 
-#       random user (in this case apache ) to run ipp programs
-#
-# TODO: re-write this in perl
-#
-###
-
-function cleanup() {
-    if [[ $do_cleanup != 0 ]] ; then
-        rm -f $request_out $request_err $parse_out $parse_err $stamp_err $stamp_out
-        ### rm -f $request_file
-    fi
-}
-
-function dump_variables {
-    if [[ $do_vardump != 0 ]] ; then
-        echo $1':' >> $variable_dump
-        $CMD_DIR/dumpvars.sh >> $variable_dump
-        echo '-----------------------------------------------------------------------' >> $variable_dump
-        echo ""
-    fi
-}
-    
-
-# if do_cleanup is true we delete the temporary files that we create
-do_cleanup=0
-
-# if do_vardump is true we print enviornment and shell variables to a
-# file for debugging purposes
-do_vardump=0
-
-#### BEGIN LOCAL_CONFIGURATION
-
-# These variables need to be customized for a particular installation
-export PSCONFDIR=/export/data0/bills/psconfig
-
-# TODO shouldn't need to do this but there seems to be a psconfig problem.
-export LD_LIBRARY_PATH=/usr/local/lib
-
-WORK_DIR=/export/data1/bills/pstamp/work
-CMD_DIR=/export/data0/bills/src/ipp/pstamp/scripts
-
-#### END LOCAL_CONFIGURATION
-
-export HOME=$WORK_DIR
-
-# TODO: make sure this succeeds before proceeding
-cd $WORK_DIR
-
-# TODO: I do this so that I have permissions to modify the files created by 
-# the web server user apache.
-umask 0
-
-variable_dump=variable_dump.$$
-rm -f $variable_dump
-
-dump_variables BEFORE
-
-
-###
-### configure IPP
-###
-export MANPATH=""
-
-source $PSCONFDIR/psconfig.bash default
-
-###
-### Add our command directory to the path. This won't be necessary once the scripts get
-### installed in the IPP
-###
-PATH=${CMD_DIR}:${PATH}
-
-# if -list is used, it must be the first argument
-if [[ "$1" == "-list" ]] ; then
-    #echo List Mode
-    listMode=1
-else 
-    listMode=0
-fi
-
-dump_variables AFTER
-
-out_ext=$$.out
-err_ext=$$.err
-
-request_file=request_file.$$.fits
-request_out=req.${out_ext}
-request_err=req.${err_ext}
-
-###
-### Well after all that setup we're ready to get to work.
-### Pass the command line parameters to pstamprequest to build the request file
-
-pstamprequest $* $request_file 1> $request_out 2> $request_err
-if [[ $? != 0 ]] ;then
-    echo pstamprequest failed
-    cat $request_err
-    cleanup
-    exit 1;
-fi
-
-rm $request_out $request_err
-
-# ok at this point we have a request file add it to the database (unless we're in listMode
-if [[ $listMode == 0 ]]; then
-    pstamptool -addreq -uri ${PWD}/$request_file 1> $request_out 2> $request_err
-    if [[ $? != 0 ]] ;then
-        echo pstamptool failed
-        cat $request_err
-        cleanup
-        exit 1
-    fi
-
-    # pstamptool's only output is the request id
-    tmp_id=`cat $request_out`
-    # zap the newline
-    request_id=${tmp_id% \n}
-fi
-
-parse_out=parse.${out_ext}
-parse_err=parse.${err_ext}
-
-if [[ $listMode == 1 ]]; then
-    ###
-    ### In list mode just parse the file print the output and we're done
-    ###
-    pstampparse $request_file 1> $parse_out 2> $parse_err
-    if [[ $? != 0 ]] ;then
-        echo ppstampparse failed
-        echo ""
-        echo STDERR:
-        cat $parse_err
-        echo ""
-        echo STDOUT:
-        cat $parse_out
-        cleanup
-        exit 2;
-    fi
-
-    # in list mode so just cat the output file and we're done
-    echo Matching Images:
-    cat $parse_out
-    cleanup
-    exit 0
-fi
-
-###
-### TODO: Until we have pantasks monitoring the reuest and job table
-### run things directly
-
-
-###
-### Queue the jobs for the request
-###
-pstampparser_run.pl $request_id  1> $parse_out 2> $parse_err
-if [[ $? != 0 ]] ;then
-    echo ppstampparse failed
-    echo ""
-    echo STDERR:
-    cat $parse_err
-    echo ""
-    echo STDOUT:
-    cat $parse_out
-    cleanup
-    exit 2
-fi
-
-###
-### Ok now it's time to build the stamps
-###
-
-stamp_out=stamp.${out_ext}
-stamp_err=stamp.${err_ext}
-
-echo making stamps for request> $stamp_err
-
-###
-### Make the stamps by executing each of the jobs
-###
-pstamp_dorequest.pl $request_id 1>$stamp_out 2>>$stamp_err
-if [[ $? != 0 ]]; then
-    echo pstamp_do_request.pl failed
-    echo STDERR:
-    cat $stamp_err
-    echo STDOUT:
-    cat $stamp_out
-    cleanup
-    exit 3
-fi
-
-# Success print the urls
-pstamptool -listjob -req_id $request_id -simple | awk '{ print $6 ".fits" }'
-
-cleanup
-
-exit 0
Index: /trunk/pstamp/web/request.php
===================================================================
--- /trunk/pstamp/web/request.php	(revision 16357)
+++ /trunk/pstamp/web/request.php	(revision 16358)
@@ -1,23 +1,23 @@
-<html>
- <head>
-  <title>Postage Stamp Request Form</title>
- </head>
-<form method="post">
-
-<body>
-
-<H1 align=center>
-Postage Stamp Request Form
-</h1>
-
-<!--- for dumping output from the following code for debug TODO: remove this--->
-<pre>
 <?php 
 
+// A php program that generates a postage stamp request form
+
+// There are two modes on the form List Images simply lists the input images that match
+// the image selection critera
+
+// When make stamps is selected when the page is posted, a postage stamp request is queued.
+// 
+// Then we respond to http posts by listing the status of the jobs.
+// Once all of the jobs complete we set the page back to submission mode again
+// The mode for the page is given by $request_id != 0
+
 $COMMAND_DIR = "/export/data0/bills/src/ipp/pstamp/scripts";
-$STAMPS_DIR = "stamps";
+$STAMPS_DIR = "/export/data1/bills/pstamp/work/stamps";
 
 // Initialize variables
 $output_array = array();
+
+$request_id = 0;
+$last_request_id = 0;
 
 $raw_selected = "";
@@ -61,4 +61,6 @@
 
 $rvar_cmd_mode = "";
+$rvar_request_id = 0;
+$rvar_last_request_id = 0;
 
 // now get the values from this post
@@ -110,5 +112,12 @@
 }
 
-if ($rvar_cmd_mode == "Make Stamps") {
+// When request_id is non-zero we respond to posts by check the status of that request
+// request_id gets set to zero when the status of all jobs for the request is 'stop'
+$request_id = $rvar_request_id;
+$last_request_id = $rvar_last_request_id;
+
+
+// default to list mode, unless we are waiting for a request to finish
+if (($rvar_cmd_mode == "Make Stamps") || ($request_id != 0)) {
     $pstamp_checked = "checked";
     $list_checked = "";
@@ -118,26 +127,267 @@
 }
 
-echo "\n\n\n";
-
-// If the data is from a Post run the command
-// How do we know? Well if it's the initial get of the web page, rvar_img_type is not set
-// TODO: find a better way to decide whether to proceed
+
+// echo "rvar_request_id: $rvar_request_id\n";
+
+// HERE is the logic for running the various commands
+
+// How do we know? Well if we aren't running the initial get of the web page?
+// Well, in that case rvar_img_type is not set so key off of that
+// TODO: find a better way to decide whether or not to run commands
+
 if ($rvar_img_type) {
 
-    try {
-        $command_line = build_command_line();
+    if ($request_id == 0) {
+        try {
+            $command_line = build_command_line();
+            $error_line = "";
+            run_command();
+            if (! $list_checked) {
+                // The only output from a successful run is the request_id
+                $request_id = Array_pop($output_array);
+                $last_request_id = $request_id;
+                #echo "The request id is $request_id\n";
+                if (count($output_array) != 0) {
+                    throw new Exception("unexpected output returned by pstampwebrequest.");
+                }
+
+                $jobRunning = getRequestStatus();
+                if (!$jobRunning) {
+                    $request_id = 0;
+                }
+
+            }
+        } catch (Exception $e) {
+            $error_line = $e->getMessage();
+        }
+    } else {
+        try {
+            // get the list of jobs for the request
+            // echo "calling getRequestStatus\n";
+            $jobRunning = getRequestStatus();
+
+            if (!$jobRunning) {
+                $request_id = 0;
+            }
+
+        } catch (Exception $e) {
+            $error_line = $e->getMessage();
+        }
+    }
+}
+
+function build_command_line()
+{
+    global $rvar_project, $rvar_user_tag;
+    global $sky_checked, $rsky_checked;
+    global $list_checked;
+    global $rvar_RA, $rvar_DEC;
+    global $rvar_dRA, $rvar_dDEC;
+    global $rvar_X, $rvar_Y;
+    global $rvar_W, $rvar_H;
+    global $exp_checked, $file_checked, $coord_checked;
+    global $rvar_img_type;
+    global $rvar_id, $rvar_class_id;
+    global $command_line;
+    global $STAMPS_DIR;
+
+    $cmd = "pstamp_runcommand.sh pstamp_webrequest.pl";
+    
+    if ($list_checked) {
+        $cmd .= " -list";
+    }
+
+    if (! $rvar_project ) {
+        throw new Exception('project must be specified.');
+    }
+    $cmd .= " -project $rvar_project";
+    if (! $rvar_user_tag ) {
+        throw new Exception('Stamp Name must be specified.');
+    }
+    $cmd .= " -user_tag $STAMPS_DIR/$rvar_user_tag";
+
+    // Set up the ROI parameters
+    if ($sky_checked) {
+        if (! $rvar_RA || ! $rvar_DEC) {
+            throw new Exception('RA and DEC must be specified.');
+        }
+        $cmd .= " -skycenter $rvar_RA $rvar_DEC";
+    } else {
+        if (! $rvar_X || ! $rvar_Y) {
+            throw new Exception('X and Y must be specified.');
+        }
+        $cmd .= " -pixcenter $rvar_X $rvar_Y";
+    }
+
+    if ($rsky_checked) {
+        if (! $rvar_dRA || ! $rvar_dDEC) {
+            throw new Exception('dRA and dDEC must be specified.');
+        }
+        $cmd .= " -arcrange $rvar_dRA $rvar_dDEC";
+    } else {
+        if (! $rvar_W || ! $rvar_H) {
+            throw new Exception('width and height must be specified.');
+        }
+        $cmd .= " -pixrange $rvar_W $rvar_H";
+    }
+
+
+    // Note: in the following block we assume that image type has been
+    // which is true since it comes from the menu
+    if ($exp_checked) {
+        if (! $rvar_id ) {
+            throw new Exception('Must set Exposure ID.');
+        }
+        $cmd .= " -byexp $rvar_img_type $rvar_id";
+    } else if ($file_checked) {
+        if (! $rvar_id ) {
+            throw new Exception('Must set ID specified.');
+        }
+        $cmd .= " -byid $rvar_img_type $rvar_id";
+    } else if ($coord_checked) {
+        // $cmd .= " -bycoord $rvar_img_type";
+        $coord_checked = "";
+        $exp_checked = "checked";
+        throw new Exception("Image selection by coordinate not implemented yet.");
+    }
+
+    if (($rvar_img_type == "raw") || ($rvar_img_type == "chip")) {
+        if (! $rvar_class_id ) {
+            throw new Exception("must specify Class ID with Image Type $rvar_img_type.");
+        }
+        $cmd .= " $rvar_class_id";
+    }
+
+    return escapeshellcmd($cmd);
+}
+
+function run_command()
+{
+    global $command_line;
+    global $output_array;
+    global $error_line;
+    global $command_status;
+    global $COMMAND_DIR;
+
+
+    exec ("$COMMAND_DIR/$command_line", $output_array, $command_status);
+
+    $size = sizeof($output_array);
+    // echo "output_array  contains $size lines\n";
+    if ($command_status == 0) {
+        // On success we just remember the results
+        $dump_results = 0;
+        if ($dump_results) {
+            echo "Output: $size lines\n";
+            for ($i = 0; $i < $size; $i++) {
+                echo "$output_array[$i]\n";
+            }
+        }
+    } else {
+        // copy the output to the error_line
         $error_line = "";
-        run_command();
-    } catch (Exception $e) {
-        $error_line = $e->getMessage();
+        for ($i = 0; $i < $size; $i++) {
+             $error_line .= "$output_array[$i]\n";
+        }
+    }
+
+}
+
+function printURL($line)
+{
+    global $request_id;
+    global $last_request_id;
+
+    echo "<tr><td>";
+    $doURL = 1;
+    if ($doURL) {
+        // echo "output_line: $line\n";
+        $elements = explode(" ", $line);
+        if (count($elements) == 3) {
+            $job_id   = $elements[0];
+            $state    = $elements[1];
+            $path     = $elements[2];
+            $fileName = basename($path);
+            if ($state == "stop") {
+                // this job is done, list the url as a link
+                echo "<a href=\"http:$path\" target=\"_blank\" type=\"image/fits\">";
+                echo $fileName;
+                echo "</a>";
+                echo "&nbsp;&nbsp;&nbsp; request_id: $last_request_id &nbsp;&nbsp;";
+                echo "job_id: $job_id &nbsp;&nbsp;&nbsp; state: $state";
+            } else {
+                // TODO: refine this output
+                echo "$fileName&nbsp;&nbsp;&nbsp; request_id: $request_id &nbsp;&nbsp;&nbsp;";
+                echo "job_id: $job_id &nbsp;&nbsp;&nbsp; state: $state";
+            }
+        }
+    } else {
+        print "$line";
+    }
+
+    echo "</td></tr>";
+}
+
+function countRunningJobs()
+{
+    global $output_array;
+
+    $runningJobs = 0;
+    $size = sizeof($output_array);
+    for ($i = 0; $i < $size; $i++) {
+        $elements = explode(" ", $output_array[$i]);
+        if (count($elements) == 3) {
+            $state    = $elements[1];
+            if ($state != "stop") {
+                $runningJobs++;
+            }
+        } else {
+            throw new Exception ("incorrect data in job status: $output_array[$i]");
+        }
+    }
+    return $runningJobs;
+}
+
+function getRequestStatus()
+{
+    global $request_id;
+    global $command_line;
+    global $command_status;
+
+    $command_line = "pstamp_runcommand.sh pstamp_listjobs.pl $request_id";
+    run_command();
+
+    if ($command_status == 0) {
+        return countRunningJobs();
+    } else {
+        return 0;
     }
 }
 
 ?>
-</pre>
-<!-- end of preformatted area for debug printing TODO: remove this -->
-
+
+<!-- Beginning of the HTML --------------------------------------------- -->
+<html>
+ <head>
+  <title>Postage Stamp Request Form</title>
+    <?php
+        if ($request_id != 0) {
+            // This doesn't do what I want. It does a get not a post
+
+            // echo '<META HTTP-EQUIV="refresh" CONTENT="5">';
+
+        }
+    ?>
+ </head>
+<form method="post">
+
+<body>
+
+<H1 align=center>
+Postage Stamp Request Form
+</h1>
 
 <!-- Whole page is a single column table -->
+
 
 <table width=90% align=center>
@@ -327,4 +577,7 @@
   <table width=40%>
   <tr>
+
+<?php
+  if ($request_id == 0): ?>
     <td><input type=submit value="Submit"></td>
     <td><b>Mode:</b>&nbsp;&nbsp;
@@ -332,4 +585,11 @@
     <input type=radio name="cmd_mode" value="List Images" <?php echo $list_checked; ?> >List Images
     </td>
+<?php 
+  else: ?>
+    <td><input type=submit value="Get Status"></td>
+<?php
+  echo "<td><b>Request Id: $request_id";
+  endif; ?>
+
   </tr>
   </table>
@@ -361,6 +621,7 @@
     </td>
 </tr>
-<!-- a blank row for space-->
-<tr height=20><td></td></tr>
+
+<!-- a blank row for space -->
+<tr height=20><td> </td></tr>
 
 <tr>
@@ -374,8 +635,11 @@
     if ($command_status == 0) {
         if ($list_checked) {
+            // in list mode the output is a list of image files, just list them
+            // later we might add links to cause a stamp to be made from a selected file
             for ($i = 0; $i < $size; $i++)  {
                 echo "<tr><td>$output_array[$i]</td></tr>";
             }
         } else {
+            // output the list of urls
             for ($i = 0; $i < $size; $i++)  {
                 printURL($output_array[$i]);
@@ -387,4 +651,22 @@
 </td>
 </tr>
+<!-- a blank row for our hidden element here-->
+
+<!-- request_id being non-zero causes us to issue status requests instead of new requsts -->
+<!--
+
+need a way to cancel a request There's probably no reason to have the value hidden, but we do
+need to set it as the last thing that we do
+
+-->
+
+<tr height=20>
+<td>
+    <input type="hidden" name="request_id" value=<?php echo $request_id ?> >
+</td>
+<td>
+    <input type="hidden" name="last_request_id" value=<?php echo $last_request_id ?> >
+</td>
+</tr>
 </table>
 
@@ -398,139 +680,7 @@
 
 <?php 
-// dump parameters 
-
-   // phpinfo(32);
-
-?>
-
-<?php
-
-function build_command_line()
-{
-    global $rvar_project, $rvar_user_tag;
-    global $sky_checked, $rsky_checked;
-    global $list_checked;
-    global $rvar_RA, $rvar_DEC;
-    global $rvar_dRA, $rvar_dDEC;
-    global $rvar_X, $rvar_Y;
-    global $rvar_W, $rvar_H;
-    global $exp_checked, $file_checked, $coord_checked;
-    global $rvar_img_type;
-    global $rvar_id, $rvar_class_id;
-    global $command_line;
-    global $STAMPS_DIR;
-
-    $cmd = "pstampwebrequest.sh";
-    
-    if ($list_checked) {
-        $cmd .= " -list";
-    }
-
-    if (! $rvar_project ) {
-        throw new Exception('project must be specified.');
-    }
-    $cmd .= " -project $rvar_project";
-    if (! $rvar_user_tag ) {
-        throw new Exception('Stamp Name must be specified.');
-    }
-    $cmd .= " -user_tag $STAMPS_DIR/$rvar_user_tag";
-
-    // Set up the ROI parameters
-    if ($sky_checked) {
-        if (! $rvar_RA || ! $rvar_DEC) {
-            throw new Exception('RA and DEC must be specified.');
-        }
-        $cmd .= " -skycenter $rvar_RA $rvar_DEC";
-    } else {
-        if (! $rvar_X || ! $rvar_Y) {
-            throw new Exception('X and Y must be specified.');
-        }
-        $cmd .= " -pixcenter $rvar_X $rvar_Y";
-    }
-
-    if ($rsky_checked) {
-        if (! $rvar_dRA || ! $rvar_dDEC) {
-            throw new Exception('dRA and dDEC must be specified.');
-        }
-        $cmd .= " -arcrange $rvar_dRA $rvar_dDEC";
-    } else {
-        if (! $rvar_W || ! $rvar_H) {
-            throw new Exception('width and height must be specified.');
-        }
-        $cmd .= " -pixrange $rvar_W $rvar_H";
-    }
-
-
-    // Note: in the following block we assume that image type has been
-    // which is true since it comes from the menu
-    if ($exp_checked) {
-        if (! $rvar_id ) {
-            throw new Exception('Must set Exposure ID.');
-        }
-        $cmd .= " -byexp $rvar_img_type $rvar_id";
-    } else if ($file_checked) {
-        if (! $rvar_id ) {
-            throw new Exception('Must set ID specified.');
-        }
-        $cmd .= " -byid $rvar_img_type $rvar_id";
-    } else if ($coord_checked) {
-        // $cmd .= " -bycoord $rvar_img_type";
-        $coord_checked = "";
-        $exp_checked = "checked";
-        throw new Exception("Image selection by coordinate not implemented yet.");
-    }
-
-    if (($rvar_img_type == "raw") || ($rvar_img_type == "chip")) {
-        if (! $rvar_class_id ) {
-            throw new Exception("must specify Class ID with Image Type $rvar_img_type.");
-        }
-        $cmd .= " $rvar_class_id";
-    }
-
-    return escapeshellcmd($cmd);
-}
-
-function run_command()
-{
-    global $command_line;
-    global $output_array;
-    global $error_line;
-    global $command_status;
-    global $COMMAND_DIR;
-
-
-    exec ("$COMMAND_DIR/$command_line", $output_array, $command_status);
-
-    $size = sizeof($output_array);
-    // echo "output_array  contains $size lines\n";
-    if ($command_status == 0) {
-        // echo "Output: $size lines\n";
-        for ($i = 0; $i < $size; $i++) {
-          //  echo "$output_array[$i]\n";
-        }
-    } else {
-        // copy the output to the error_line
-        $error_line = "";
-        for ($i = 0; $i < $size; $i++) {
-             $error_line .= "$output_array[$i]\n";
-        }
-    }
-
-}
-
-function printURL($path)
-{
-    echo "<tr><td>";
-    $doURL = 1;
-    if ($doURL) {
-        $fileName = basename($path);
-        echo "<a href=\"http:$path\" target=\"_blank\" type=\"image/fits\">";
-        echo $fileName;
-        echo "</a>";
-    } else {
-        print "$path";
-    }
-    echo "</td></tr>";
-}
+    // dump parameters 
+
+    // phpinfo(32);
 
 ?>
@@ -538,3 +688,4 @@
 </body>
 </form>
+
 </html>
