#!/usr/bin/env perl

use Carp;
use warnings;
use strict;

## report the program and machine
use Sys::Hostname;
my $host = hostname();
print "\n\n";
print "Starting script $0 on $host\n\n";

use vars qw( $VERSION );
$VERSION = '0.01';

use Cache::File;
use Storable qw(freeze thaw);
use File::Basename qw( basename);
use IPC::Cmd 0.36 qw( can_run run );
use PS::IPP::Metadata::Config;
use PS::IPP::Metadata::Stats;

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

my $ipprc = PS::IPP::Config->new(); # IPP configuration
use File::Spec;

use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use Pod::Usage qw( pod2usage );

my ($cache, $exp_id, $tmp_class_id, $tmp_exp_name, $uri, $workdir, $dbname, $no_update, $no_op);
GetOptions(
    'caches'           => \$cache,
    'exp_id|e=s'       => \$exp_id,
    'tmp_class_id|i=s' => \$tmp_class_id,
    'tmp_exp_name|n=s' => \$tmp_exp_name,
    'uri|u=s'          => \$uri,
    'workdir|w=s'      => \$workdir, # Working directory for output files
    'dbname|d=s'       => \$dbname,# Database name
    'no-update'        => \$no_update,
    'no-op'            => \$no_op,
) or pod2usage( 2 );

pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
pod2usage( -msg => "Required options: --exp_id --tmp_class_id --tmp_exp_name --uri",
           -exitval => 3) unless
    defined $exp_id and
    defined $tmp_class_id and
    defined $tmp_exp_name and
    defined $uri;

my $RECIPE = "REGISTER"; # Recipe to use for ppStats

# values to extract from output metadata and the stats to calculate
my $STATS = 
   [   
       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
       { name => "FILE.LEVEL",     type => "constant", flag => "-filelevel",      dtype => "string" }, # File level
       { name => "CLASS.ID",       type => "constant", flag => "-class_id",       dtype => "string" }, # Real Class ID
       { name => "FPA.OBJECT",     type => "constant", flag => "-object",         dtype => "string" }, # Object
       { name => "FPA.OBSTYPE",    type => "constant", flag => "-exp_type",       dtype => "string" }, # Exposure type
       { name => "FPA.FILTER",     type => "constant", flag => "-filter",         dtype => "string" }, # Filter used
       { name => "FPA.AIRMASS",    type => "constant", flag => "-airmass",        dtype => "float"  }, # Airmass
       { name => "FPA.RA",         type => "constant", flag => "-ra",             dtype => "float"  }, # Right ascension
       { name => "FPA.DEC",        type => "constant", flag => "-decl",           dtype => "float"  }, # Declination
       { name => "FPA.ALT",        type => "constant", flag => "-alt",            dtype => "float"  }, # Altitude
       { name => "FPA.AZ",         type => "constant", flag => "-az",             dtype => "float"  }, # Azimuth
       { name => "FPA.POSANGLE",   type => "constant", flag => "-posang",         dtype => "float"  }, # Position angle
       { name => "FPA.TIME",       type => "constant", flag => "-dateobs",        dtype => "string" }, # Date of observation (UTC)
       { name => "FPA.TELESCOPE",  type => "constant", flag => "-telescope",      dtype => "string" }, # Telescope
       { name => "FPA.INSTRUMENT", type => "constant", flag => "-inst",           dtype => "string" }, # Instrument 
       { name => "CHIP.TEMP",      type => "mean",     flag => "-ccd_temp",       dtype => "float"  }, # CCD temperature
       { name => "CELL.EXPOSURE",  type => "mean",     flag => "-exp_time",       dtype => "float"  }, # Exposure time
       { name => "SAT_PIXEL_FRAC", type => "mean",     flag => "-sat_pixel_frac", dtype => "float"  }, # fraction of saturated pixels
       { name => "ROBUST_MEDIAN",  type => "mean",     flag => "-bg",             dtype => "float"  },
       { name => "ROBUST_MEDIAN",  type => "stdev",    flag => "-bg_mean_stdev",  dtype => "float"  },
       { name => "ROBUST_STDEV",   type => "rms",      flag => "-bg_stdev",       dtype => "float"  },
   ];
my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser

# Look for programs we need
my $missing_tools;
my $regtool = can_run('regtool')
    or (warn "Can't find regtool" and $missing_tools = 1);
my $ppStats = can_run('ppStats')
    or (warn "Can't find ppStats" and $missing_tools = 1);

if ($missing_tools) { 
    warn ("Can't find required tools");
    exit($PS_EXIT_CONFIG_ERROR); 
}

# setup cache interface
# XXX why is this being cached?
my $c = Cache::File->new(
    cache_root => File::Spec->catdir($ENV{'HOME'}, '.pxtools', basename($0)),
    default_expires => '7200 sec',
);

# Run ppStats on the input file
{
    # extract the data from the image header; -level is used to get FILE.LEVEL and CLASS.ID
    my $command = "$ppStats $uri -recipe PPSTATS $RECIPE -level"; # Command to run ppStats
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        cache_run(command => $command, verbose => 1);
    unless ($success) { 
        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
        &my_die ("Unable to perform ppStats on exposure id $exp_id: $error_code", $exp_id, $tmp_exp_name, $tmp_class_id, $error_code);
    }
    
    # Parse the output
    my $mdcParser = PS::IPP::Metadata::Config->new;        # Parser for metadata config files
    my $metadata = $mdcParser->parse(join "", @$stdout_buf); # XXX is this join necessary?
    unless ($metadata) {
        &my_die ("Unable to parse metadata config doc", $exp_id, $tmp_exp_name, $tmp_class_id, $PS_EXIT_PROG_ERROR);
    }

    # extract the stats from the metadata
    unless ($stats->parse($metadata)) {
        &my_die ("Unable to find all values", $exp_id, $tmp_exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR);
    }
}

# we require at a minimum: -telescope, -inst, -filelevel, -class_id, -exp_type
if (uc($stats->value_for_flag ("-telescope")) eq "NULL") { &my_die ("telescope not found", $exp_id, $tmp_exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
if (uc($stats->value_for_flag ("-inst"))      eq "NULL") { &my_die ("inst      not found", $exp_id, $tmp_exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
if (uc($stats->value_for_flag ("-filelevel")) eq "NULL") { &my_die ("filelevel not found", $exp_id, $tmp_exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
if (uc($stats->value_for_flag ("-class_id"))  eq "NULL") { &my_die ("class_id  not found", $exp_id, $tmp_exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }
if (uc($stats->value_for_flag ("-exp_type"))  eq "NULL") { &my_die ("exp_type  not found", $exp_id, $tmp_exp_name, $tmp_class_id, $PS_EXIT_CONFIG_ERROR); }

my $command = "$regtool -addprocessedimfile";
$command .= " -exp_id $exp_id";
$command .= " -exp_name $tmp_exp_name"; # keep the supplied exp_name (could be derived from the file)
$command .= " -tmp_class_id $tmp_class_id"; # the original class_id supplied by the user, replace by ppStats CLASS.ID
$command .= " -dbname $dbname" if defined $dbname;
$command .= $stats->cmdflags();

# Push the results into the database
unless ($no_update) {
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        run(command => $command, verbose => 1);
    unless ($success) {
        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
        warn ("Unable to perform regtool -addprocessedimfile: $error_code");
        exit($error_code);
    }
} else {
    print "skipping command: $command\n";
}

sub cache_run
{
    my %p = @_;

    my $cmd_output = $c->get($p{command}) if $cache;
    if (defined $cmd_output) {
        return @{thaw $cmd_output};
    } else {
        my @output = run(%p);
        $c->set($p{command}, freeze \@output) if $cache;
        return @output;
    }
}

sub my_die
{
    my $msg = shift; # Warning message on die
    my $exp_id = shift;
    my $exp_name = shift;
    my $tmp_class_id = shift;
    my $exit_code = shift;

    # for failed imfiles, we insert UNKNOWN for inst, telescope, class_id

    carp($msg);
    if ($exp_id && $tmp_class_id and not $no_update) {
        my $command = "$regtool -addprocessedimfile";
        $command .= " -exp_id $exp_id";
        $command .= " -exp_name $exp_name";
        $command .= " -tmp_class_id $tmp_class_id";
        $command .= " -telescope UNKNOWN";
        $command .= " -inst UNKNOWN";
        $command .= " -class_id $tmp_class_id";
        $command .= " -code $exit_code";
        $command .= " -dbname $dbname" if defined $dbname;
        system ($command);
    }
    exit $exit_code;
}

# Pau.

END {
    my $exit = $?;
    system("sync") == 0 or die "failed to execute sync: $!";
    $? = $exit;
}

__END__
