#!/usr/bin/env perl

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 IPC::Cmd qw( can_run );
use IPC::Run qw ( run );
use PS::IPP::Metadata::Config;
use PS::IPP::Metadata::List qw( parse_md_list );
use Data::Dumper;

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

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


# Parse command-line arguments
my ($det_id,			# Detrend id
    $iter,			# Iteration
    $detType,			# Detrend type
    $workdir,			# Working directory for output files
    $dbname,			# Database name
    $no_update,			# Don't update the database?
    $no_op,			# Don't do operations
    );
GetOptions(
	'det_id|d=s'	=> \$det_id,
	'iteration|i=s'	=> \$iter,
	'det_type|t=s'  => \$detType,
	'workdir|w=s'   => \$workdir,
 	'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 --det_id --iteration --det_type",
	   -exitval => 3,
	   ) unless defined $det_id
    and defined $iter
    and defined $detType;


use constant STATISTIC => 'bg'; # Background statistic to use from the database
# Define which detrend types we normalise
use constant NORMALIZE => {
    'bias'     => 0,
    'dark'     => 0,
    'shutter'  => 0,
    'flat'     => 1,
    'domeflat' => 1,
    'skyflat'  => 1,
    'fringe'   => 0,
    }; 


# Look for programs we need
my $missing_tools;
my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
my $ppNormCalc = can_run('ppNormCalc') or (warn "Can't find ppNormCalc" and $missing_tools = 1);
if ($missing_tools) { 
    warn("Can't find required tools.");
    exit($PS_EXIT_CONFIG_ERROR); 
}

&my_die("Unrecognised detrend type: $detType", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless
    exists NORMALIZE()->{lc($detType)};

my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files

# Get the list of inputs
my @files;			# The input files
{
    my $command = "$dettool -processedimfile -det_id $det_id"; # Command to run
    $command .= " -dbname $dbname" if defined $dbname;
    my @command = split /\s+/, $command;
    my ( $stdin, $stdout, $stderr ); # Buffers for running program
    print "Running [$command]...\n";
    if (not run(\@command, \$stdin, \$stdout, \$stderr)) {
	&my_die("Unable to perform dettool -processedimfile on detrend $det_id/$iter: $?",
		$det_id, $iter, $PS_EXIT_SYS_ERROR);
    }
#    print $stdout . "\n";
    
    # Because of the length, need to split into individual metadatas --- it parses SO much quicker!
    my @whole = split /\n/, $stdout;
    my @single = ();
    while ( scalar @whole > 0 ) {
	my $value = shift @whole;
	push @single, $value;
	if ($value =~ /^\s*END\s*$/) {
	    push @single, "\n";

	    my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) );
	    &my_die("Unable to parse output from dettool", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless
		defined $list;
	    push @files, @$list;
	    @single = ();
	}
    }
}

my $norms;			# MDC with normalisations
if (NORMALIZE()->{lc($detType)} and not $no_op) {

    my %matrix; # Matrix of statistics as a function of exposures and classes
    foreach my $file (@files) {
	my $exp_tag = $file->{'exp_tag'}; # Exposure ID
	my $class_id = $file->{'class_id'}; # Class ID
	my $stat = $file->{STATISTIC()}; # Statistic of interest
	
	# Create matrix elements
	$matrix{$exp_tag} = {} if not defined $matrix{$exp_tag};
	$matrix{$exp_tag}->{$class_id} = $stat;
    }
    
    # Generate the input for ppNormCalc
    my $normData;			# Normalisation data
    foreach my $exp_tag (keys %matrix) {
	$normData .= "$exp_tag\tMETADATA\n";
	foreach my $class_id (keys %{$matrix{$exp_tag}}) {
	    $normData .= "\t" . $class_id . "\tF32\t" . $matrix{$exp_tag}->{$class_id} . "\n";
	}
	$normData .= "END\n\n";
    }

    # Run ppNormCalc
    {
	my ( $stdout, $stderr ); # Buffers for running program
	my @command = split /\s+/, $ppNormCalc;
	print "Running [$ppNormCalc]...\n";
	if (not run(\@command, \$normData, \$stdout, \$stderr)) {
	    &my_die("Unable to perform ppNormCalc: $?", $det_id, $iter, $PS_EXIT_SYS_ERROR);
	}
	print $stdout . "\n";
	
	# Parse the output
	$norms = $mdcParser->parse($stdout);
	&my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless $norms;
    }

} else {
    # It's something that doesn't need normalisation --- just push in a normalisation of 1
    my %classes;		# List of unique classes
    foreach my $file (@files) {
	my $class_id = $file->{'class_id'}; # Class Id
	$classes{$class_id} = 1;
    }
    
    foreach my $class_id (keys %classes) {
	my %mdValue;	# Metadata value for this class id
	$mdValue{name} = $class_id;
	$mdValue{value} = 1.0;
	push @$norms, \%mdValue;
    }
}


# Process output normalisations
unless ($no_update) {
    foreach my $normItem (@$norms) {
	my $className = $normItem->{name}; # Name of component
	my $normalisation = $normItem->{value}; # Normalisation for component

	my $command = "$dettool -addnormalizedstat -det_id $det_id -iteration $iter -class_id $className ".
	    "-norm $normalisation"; # Command to run
	$command .= " -dbname $dbname" if defined $dbname;
	my @command = split /\s+/, $command;

	my ( $stdin, $stdout, $stderr ); # Buffers for running program
	print "Running [$command]...\n";
	if (not run \@command, \$stdin, \$stdout, \$stderr) {
	    warn("Unable to perform dettool -addnormstat for $className: $?");
	    exit($PS_EXIT_SYS_ERROR);
	}
	print $stdout . "\n";
    }
}

sub my_die
{
    my $msg = shift;		# Warning message on die
    my $det_id = shift;		# Detrend identifier
    my $iter = shift;		# Iteration
    my $exit_code = shift;	# Exit code to add

    warn($msg);
    if ($det_id and $iter and not $no_update) {
	my $command = "$dettool -addnormalizedstat -det_id $det_id -iteration $iter -code $exit_code";
	$command .= " -dbname $dbname" if defined $dbname;
        system ($command);
    }
    exit $exit_code;
}


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

__END__
