#!/bin/env perl
###
#
# pstampwebrequest.pl: take a postage stamp request command line and process it
#
# The arguments are the command line parameters for the program pstamprequest
#
# Unless the argument -list is provided the output is the request id for the resulting request
#
# If -list is the first argument, the request file is built and parsed and the
# the selected "input" uris are listed on stdout.
#
# Note: Despite the name there nothing really web specific about this program.
#
###

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
		       );

use Cwd;

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('pstamp_parser_run.pl')  or (warn "Can't find pstamp_parser_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 $cur_dir = getcwd();
my $request_file = "$cur_dir/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;
        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);

    if ($success) {
        ### print "Matching Images:\n";
        print @$stdout_buf;
    } else {
        print @$stderr_buf;
    }
    exit 0;
}

# Queue the request
my $request_id = 0;
{

    my $command = "$pstamptool -addreq -uri $request_file -out_fileset \'pstampresults/web\$REQ_ID\'";
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        run(command => $command, verbose => $verbosity);
    unless ($success) {
        print STDERR @$stderr_buf;
        die("Unable to perform pstamptool -addreq: $error_code");
    }
    $request_id = ${$stdout_buf}[0];
}

print "$request_id";

exit 0;
