#!/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 1.01 qw( :standard );

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

my $ipprc = PS::IPP::Config->new();

my ($cache, $exp_id, $exp_tag, $dbname, $verbose, $no_update, $no_op, $logfile);
GetOptions(
    'caches'        => \$cache,
    'exp_id|e=s'    => \$exp_id,
    'exp_tag|t=s'   => \$exp_tag,
    'dbname|d=s'    => \$dbname, # Database name
    'verbose'       => \$verbose,   # Print to stdout
    'no-update'     => \$no_update,
    'no-op'         => \$no_op,
    'logfile=s'     => \$logfile,
) or pod2usage( 2 );

$ipprc->redirect_output($logfile) if $logfile;

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", "science", "light" ); # Observation types to NOT mark as detrend
my $DETREND_FLAG = "-end_stage reg"; # Flag to use to mark detrend exposure

# values to extract from output metadata and the stats to calculate
my $STATS =
   [   #          register imfile
       #          label             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 => "comment",         type => "constant",   flag => "-comment",         dtype => "string" }, # ObsComment
       { 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 => "m1_x",            type => "constant",   flag => "-m1_x",            dtype => "float"  }, # M1X
       { name => "m1_y",            type => "constant",   flag => "-m1_y",            dtype => "float"  }, # M1Y
       { name => "m1_z",            type => "constant",   flag => "-m1_z",            dtype => "float"  }, # M1Z
       { name => "m1_tip",          type => "constant",   flag => "-m1_tip",          dtype => "float"  }, # M1TIP
       { name => "m1_tilt",         type => "constant",   flag => "-m1_tilt",         dtype => "float"  }, # M1TILT
       { name => "m2_x",            type => "constant",   flag => "-m2_x",            dtype => "float"  }, # M2X
       { name => "m2_y",            type => "constant",   flag => "-m2_y",            dtype => "float"  }, # M2Y
       { name => "m2_z",            type => "constant",   flag => "-m2_z",            dtype => "float"  }, # M2Z
       { name => "m2_tip",          type => "constant",   flag => "-m2_tip",          dtype => "float"  }, # M2TIP
       { name => "m2_tilt",         type => "constant",   flag => "-m2_tilt",         dtype => "float"  }, # M2TILT
       { name => "env_temperature", type => "constant",   flag => "-env_temperature", dtype => "float"  }, # external temp
       { name => "env_humidity",    type => "constant",   flag => "-env_humidity",    dtype => "float"  }, # external humidity
       { name => "env_wind_speed",  type => "constant",   flag => "-env_wind_speed",  dtype => "float"  }, # external wind speed
       { name => "env_wind_dir",    type => "constant",   flag => "-env_wind_dir",    dtype => "float"  }, # external wind direction

       { name => "teltemp_m1",      type => "constant",   flag => "-teltemp_m1",      dtype => "float"  }, # Primary mirror temps (C)
       { name => "teltemp_m1cell",  type => "constant",   flag => "-teltemp_m1cell",  dtype => "float"  }, # Primary mirror support temps (C)
       { name => "teltemp_m2",      type => "constant",   flag => "-teltemp_m2",      dtype => "float"  }, # Secondary mirror temps (C
       { name => "teltemp_spider",  type => "constant",   flag => "-teltemp_spider",  dtype => "float"  }, # Spider temperatures (C)
       { name => "teltemp_truss",   type => "constant",   flag => "-teltemp_truss",   dtype => "float"  }, # Mid truss temperatures (C
       { name => "teltemp_extra",   type => "constant",   flag => "-teltemp_extra",   dtype => "float"  }, # Miscellaneous temperatures (C)

       { name => "sun_angle",       type => "constant",   flag => "-sun_angle",       dtype => "float"  }, # Angle to sun
       { name => "sun_alt",         type => "constant",   flag => "-sun_alt",         dtype => "float"  }, # Altitude of sun
       { name => "moon_angle",      type => "constant",   flag => "-moon_angle",      dtype => "float"  }, # Angle to moon
       { name => "moon_alt",        type => "constant",   flag => "-moon_alt",        dtype => "float"  }, # Altitude of moon
       { name => "moon_phase",      type => "constant",   flag => "-moon_phase",      dtype => "float"  }, # Phase of moon

       { name => "pon_time",        type => "mean",       flag => "-pon_time",        dtype => "float"  }, # time since last power on
       { 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 => $verbose);
    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 .= " -hostname $host" if defined $host;
$command .= " -dbname $dbname" if defined $dbname;
$command .= $stats->cmdflags();

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

# Add the detrend flag, if needed
{
    my $object = 0;             # Is it an object exposure?
    foreach my $scienceType (@SCIENCE) {
        if (lc($exp_type) =~ /$scienceType/) {
            $object = 1;
            last;
        }
    }
    $command .= " $DETREND_FLAG" unless $object;
}

# Output results to the database
unless ($no_update) {
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        cache_run(command => $command, verbose => $verbose);
    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 (defined $exp_id and not $no_update) {
        my $command = "$regtool -addprocessedexp -exp_id $exp_id -code $exit_code";
        $command .= " -hostname $host" if defined $host;
        $command .= " -dbname $dbname" if defined $dbname;
        system($command);
    }
    exit $exit_code;
}

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