Index: /trunk/pstamp/scripts/dumpvars.sh
===================================================================
--- /trunk/pstamp/scripts/dumpvars.sh	(revision 16280)
+++ /trunk/pstamp/scripts/dumpvars.sh	(revision 16280)
@@ -0,0 +1,15 @@
+#!/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 16280)
+++ /trunk/pstamp/scripts/ppstamp_run.pl	(revision 16280)
@@ -0,0 +1,121 @@
+#!/bin/env perl
+###
+### ppstamp_run.pl
+###
+###     Run the postage stamp extraction tool to process a given job
+###
+
+use warnings;
+use strict;
+
+if (@ARGV != 1) {
+    die "usage: $0 job_id\n";
+}
+
+my $job_id = $ARGV[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 );
+
+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 = can_run('ppstamp') or (warn "Can't find ppstamp" 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 $psjob;
+#Look up the uri for the given job
+{
+    my $command = "$pstamptool -pendingjob -job_id $job_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 "pending pstamp job id $job_id not found\n";
+        exit 1;
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        die("Unable to parse metdata config doc");
+
+    my $jobs = parse_md_list($metadata);
+
+    $psjob = ${@$jobs}[0];
+}
+
+if (!$psjob) {
+    print STDERR "postage stamp job $job_id not found\n";
+    # TODO: is this always an error, what if the job is no longer pending?
+    exit 1;
+}
+
+my $uri = $psjob->{uri};
+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 $command = "$ppstamp -file $uri $outputBase $argString";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+###
+### update the status of the job with the error
+###
+    unless ($success) {
+        die("Unable to perform $command: $error_code");
+    }
+}
+
+
+{
+    my $command = "$pstamptool -processedjob -job_id $job_id -state stop -status ok";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+    unless ($success) {
+        die("Unable to perform $command: $error_code");
+    }
+}
+
+
+exit 0;
Index: /trunk/pstamp/scripts/pstamp_dorequest.pl
===================================================================
--- /trunk/pstamp/scripts/pstamp_dorequest.pl	(revision 16280)
+++ /trunk/pstamp/scripts/pstamp_dorequest.pl	(revision 16280)
@@ -0,0 +1,105 @@
+#!/bin/env perl
+###
+### pstampdo_req.pl
+###
+###     Run the jobs for a given request
+###     This is intended for running outside pantasks environment
+###
+
+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();
+
+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::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 -pendingjob";
+    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 pending pstamp jobs found\n";
+        exit 1;
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        die("Unable to parse metdata config doc");
+
+    my $jobs = parse_md_list($metadata);
+
+    foreach my $job (@$jobs) {
+        if ($job->{req_id} == $request_id) {
+            # there must be a better way to do this
+            $psjobs[@psjobs] = $job;
+        }
+    }
+}
+
+if (! @psjobs) {
+    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;
+}
+
+foreach my $job (@psjobs) {
+    my $command = "echo $ppstamp_run $job->{job_id}";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbosity);
+    unless ($success) {
+        die("Unable to perform $command: $error_code");
+    }
+    ### print "@$stdout_buf\n";
+}
+
+#
+# set the state of the request to stop
+#
+
+exit 0;
Index: /trunk/pstamp/scripts/pstampparser_run.pl
===================================================================
--- /trunk/pstamp/scripts/pstampparser_run.pl	(revision 16280)
+++ /trunk/pstamp/scripts/pstampparser_run.pl	(revision 16280)
@@ -0,0 +1,102 @@
+#!/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;
Index: /trunk/pstamp/scripts/pstampwebrequest.sh
===================================================================
--- /trunk/pstamp/scripts/pstampwebrequest.sh	(revision 16280)
+++ /trunk/pstamp/scripts/pstampwebrequest.sh	(revision 16280)
@@ -0,0 +1,200 @@
+#!/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.
+#
+###
+
+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
+BASE_DIR=/export/data1/bills/pstamp
+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
+
+#### END LOCAL_CONFIGURATION
+
+WORK_DIR=$BASE_DIR/work
+CMD_DIR=$BASE_DIR/bin
+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
+
+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: Won't it interfere with pantask's job processing?
+###
+### Queue 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
+
+cat $parse.out
+
+### for each line in the output add a psJob to the 
+
+###
+### Ok now it's time to build the stamps
+
+stamp_out=stamp.${out_ext}
+stamp_err=stamp.${err_ext}
+
+echo making stamps > $stamp_err
+
+###
+### Make the stamps
+###
+pstamp_dorequest.pl $request_id 1>$stamp_out 2>>$stamp_err
+if [[ $? != 0 ]]; then
+    echo makestamps.pl failed
+    echo STDERR:
+    cat $stamp_err
+    echo STDOUT:
+    cat $stamp_out
+    cleanup
+    exit 3
+fi
+
+# Success print the output
+# get the stamp urls
+cat $stamp_out
+
+cleanup
+
+exit 0
