#!/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 Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use Pod::Usage qw( pod2usage );

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 ($cache, $exp_id, $exp_tag, $workdir, $dbname, $no_update, $no_op);
GetOptions(
    'caches'        => \$cache,
    'exp_id|e=s'    => \$exp_id,
    'exp_tag|t=s'   => \$exp_tag,
    '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 --exp_tag",
	   -exitval => 3) unless
    defined $exp_id and
    defined $exp_tag;

# add -detrend UNLESS type is one of SCIENCE listed below (eg, OBJECT)
my @SCIENCE = ( "object" ); # Observation types to NOT mark as detrend
my $DETREND_FLAG = "-detrend"; # Flag to use to mark detrend exposure

# values to extract from output metadata and the stats to calculate
my $STATS = 
   [   
       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
       { name => "exp_name",       type => "constant",   flag => "-exp_name",       dtype => "string" }, # File level
       { name => "telescope",      type => "constant",   flag => "-telescope",      dtype => "string" }, # File level
       { name => "camera",         type => "constant",   flag => "-inst",           dtype => "string" }, # File level
       { name => "filelevel",      type => "constant",   flag => "-filelevel",      dtype => "string" }, # File level
       { name => "object",         type => "constant",   flag => "-object",         dtype => "string" },
       { name => "exp_type",       type => "constant",   flag => "-exp_type",       dtype => "string" }, # File level
       { name => "filter",         type => "constant",   flag => "-filter",         dtype => "string" }, # File level
       { name => "dateobs",        type => "constant",   flag => "-dateobs",        dtype => "string" }, # File level
       { name => "ccd_temp",       type => "mean",       flag => "-ccd_temp",       dtype => "float"  }, # CCD temperature
       { name => "exp_time",       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 => "airmass",        type => "mean",       flag => "-airmass",        dtype => "float"  }, # Airmass
       { name => "ra",             type => "mean",       flag => "-ra",             dtype => "float"  }, # Right ascension
       { name => "decl",           type => "mean",       flag => "-decl",           dtype => "float"  }, # Declination
       { name => "posang",         type => "mean",       flag => "-posang",         dtype => "float"  }, # Position angle
       { name => "alt",            type => "mean",       flag => "-alt",            dtype => "float"  }, # Altitude
       { name => "az",             type => "mean",       flag => "-az",             dtype => "float"  }, # Azimuth
       { name => "bg",             type => "mean",       flag => "-bg",             dtype => "float"  }, # background
       { name => "bg",             type => "stdev",      flag => "-bg_mean_stdev",  dtype => "float"  }, # Azimuth
       { name => "bg_stdev",       type => "rms",        flag => "-bg_stdev",       dtype => "float"  }, # Azimuth
       ];
my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser

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

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

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

# Get the list of imfiles & their stats
{
    my $command = "$regtool -processedimfile -exp_id $exp_id";
    $command .= " -dbname $dbname" if defined $dbname;
    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);
        warn ("Unable to perform regtool -processedimfile on exposure id $exp_id: $error_code");
        exit ($error_code);
    }

    # Parse the output
    my $mdcParser = PS::IPP::Metadata::Config->new;        # Parser for metadata config files
    my $metadata = $mdcParser->parse(join "", @$stdout_buf);
    unless ($metadata) {
        &my_die ("Unable to parse metadata config doc", $exp_id, $PS_EXIT_PROG_ERROR);
    }

    # extract the stats from the metadata
    unless ($stats->parse($metadata)) {
        &my_die ("Unable to find all values", $exp_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, $PS_EXIT_CONFIG_ERROR); }
if (uc($stats->value_for_flag ("-inst"))      eq "NULL") { &my_die ("inst      not found", $exp_id, $PS_EXIT_CONFIG_ERROR); }
if (uc($stats->value_for_flag ("-filelevel")) eq "NULL") { &my_die ("filelevel not found", $exp_id, $PS_EXIT_CONFIG_ERROR); }
if (uc($stats->value_for_flag ("-exp_type"))  eq "NULL") { &my_die ("exp_type  not found", $exp_id, $PS_EXIT_CONFIG_ERROR); }
if (uc($stats->value_for_flag ("-exp_name"))  eq "NULL") { &my_die ("exp_name  not found", $exp_id, $PS_EXIT_CONFIG_ERROR); }

my $command = "$regtool -addprocessedexp";
$command .= " -exp_id $exp_id";
$command .= " -exp_tag $exp_tag";
$command .= " -dbname $dbname" if defined $dbname;
$command .= $stats->cmdflags();

my $exp_type = $stats->value_for_flag ("-exp_type");

# Add the detrend flag, if needed
foreach my $scienceType (@SCIENCE) {
    if (lc($exp_type) =~ /$scienceType/) {
	last;
    }
    $command .= " $DETREND_FLAG";
}

# Output results to the database
unless ($no_update) {
    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);
        warn ("Unable to run regtool -addprocessedexp for $exp_id: $error_code");
        exit($error_code);
    }
} else {
    print "skipping command: $command\n";
}

### Pau.

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 $exit_code = shift;

    carp($msg);
    if ($exp_id and not $no_update) {
	my $command = "$regtool -addprocessedexp -exp_id $exp_id -code $exit_code";
	$command .= " -dbname $dbname" if defined $dbname;
###        system($command);
    }
    exit $exit_code;
}

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