#!/usr/bin/env perl

use warnings;
use strict;
use Carp;

## 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 IPC::Cmd 0.36 qw( can_run run );
use PS::IPP::Metadata::Config;
use PS::IPP::Metadata::Stats;
use PS::IPP::Metadata::List qw( parse_md_list );
use Data::Dumper;
use File::Temp qw( tempfile );
use File::Basename;
use PS::IPP::Config 1.01 qw( :standard );

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

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

my ($stack_id, $dbname, $outroot, $debug, $verbose, $no_update, $no_op, $save_temps);
GetOptions(
    'stack_id|d=s'      => \$stack_id, # Stack identifier
    'dbname|d=s'        => \$dbname, # Database name
    'outroot=s'         => \$outroot, # Output root name
    'debug'             => \$debug,   # Print to stdout
    'verbose'           => \$verbose,   # Print to stdout
    'no-update'         => \$no_update, # Don't update the database?
    'no-op'             => \$no_op, # Don't do any operations?
    'save-temps'        => \$save_temps, # Save temporary files?
) or pod2usage( 2 );

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

my $STATS =
   [
       #          PPSTATS KEYWORD         STATISTIC          STACKTOOL FLAG
       { name => "ROBUST_MEDIAN",   type => "mean", flag => "-bg",          dtype => "float" },
       { name => "ROBUST_STDEV",    type => "rms",  flag => "-bg_stdev",    dtype => "float" },
#      { name => "DT_STACK",        type => "sum",  flag => "-dtime_stack", dtype => "float" },
       { name => "GOOD_PIXEL_FRAC", type => "mean", flag => "-good_frac",   dtype => "float" },
   ];
my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser

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

# Get list of components for stacking
my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
my $files;
{
    my $command = "$stacktool -inputskyfile -stack_id $stack_id";
    $command .= " -dbname $dbname" if defined $dbname;
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        run(command => $command, verbose => $verbose);
    unless ($success) {
        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
        &my_die("Unable to perform stacktool -inputskyfile: $error_code", $stack_id, $error_code);
    }

    if (@$stdout_buf == 0) {
        &my_die("No input skyfiles selected", $stack_id, $PS_EXIT_PROG_ERROR);
    }
    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
        &my_die("Unable to parse metadata config doc", $stack_id, $PS_EXIT_PROG_ERROR);
    $files = parse_md_list($metadata) or
        &my_die("Unable to parse metadata list", $stack_id, $PS_EXIT_PROG_ERROR);
}

&my_die("Stack list contains less than two elements", $stack_id, $PS_EXIT_SYS_ERROR) unless
    scalar @$files >= 2;

# Parse the list of input files to get the tesselation, skycell identifiers and camera
my $skycell_id;                 # Skycell identifier
my $tess_id;                    # Tesselation identifier
my $camera;                     # Camera
foreach my $file (@$files) {
    # skip warps which are speicified as 'ignored'
    if ($file->{ignored}) { next; }
    if (defined $tess_id) {
        &my_die("Tesselation identifiers don't match", $stack_id, $PS_EXIT_SYS_ERROR) unless
            $file->{tess_id} eq $tess_id;
    } else {
        $tess_id = $file->{tess_id};
    }
    if (defined $skycell_id) {
        &my_die("Skycell identifiers don't match", $stack_id, $PS_EXIT_SYS_ERROR) unless
            $file->{skycell_id} eq $skycell_id;
    } else {
        $skycell_id = $file->{skycell_id};
    }
    if (defined $camera) {
        &my_die("Cameras don't match", $stack_id, $PS_EXIT_SYS_ERROR) unless $file->{camera} eq $camera;
    } else {
        $camera = $file->{camera};
    }
}

&my_die("Can't find camera", $stack_id, $PS_EXIT_SYS_ERROR) unless defined $camera;
$ipprc->define_camera($camera);

# Generate MDC file with the inputs
my $tess_base = basename($tess_id);
my ($listFile, $listName) = tempfile("/tmp/$tess_base.$skycell_id.stk.$stack_id.list.XXXX", UNLINK => !$save_temps );
my $num = 0;
my $inputSources;               # Sources to use as stamps
foreach my $file (@$files) {
    if ($file->{ignored}) { next; }

    print $listFile "INPUT$num\tMETADATA\n";
    $num++;

    my $image = $file->{uri};   # Image name
    my $mask = $ipprc->filename( "PSWARP.OUTPUT.MASK", $file->{path_base} ); # Mask name
    my $weight = $ipprc->filename( "PSWARP.OUTPUT.WEIGHT", $file->{path_base} ); # Weight name
    my $psf = $ipprc->filename( "PSPHOT.PSF.SKY.SAVE", $file->{path_base} ); # PSF name
    my $sources = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $file->{path_base}); # Sources name

    &my_die("Image $image does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $image );
    &my_die("Mask $mask does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $mask );
    &my_die("Weight $weight does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $weight );
    &my_die("PSF $psf does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $psf );
    &my_die("Sources $sources does not exist", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $sources );

    print $listFile "\tIMAGE\tSTR\t" . $image . "\n";
    print $listFile "\tMASK\tSTR\t" . $mask . "\n";
    print $listFile "\tWEIGHT\tSTR\t" . $weight . "\n";
    print $listFile "\tPSF\tSTR\t" . $psf . "\n";
    print $listFile "\tSOURCES\tSTR\t" . $sources . "\n";

    ### XXX NEED TO UPDATE THESE appropriately
    print $listFile "\tWEIGHTING\tF32\t" . 1.0 . "\n";

    print $listFile "END\n\n";
}

# Get the output filenames
my $outputName = $ipprc->filename("PPSTACK.OUTPUT", $outroot);
my $outputMask = $ipprc->filename("PPSTACK.OUTPUT.MASK", $outroot);
my $outputWeight = $ipprc->filename("PPSTACK.OUTPUT.WEIGHT", $outroot);
my $outputSources = $ipprc->filename("PSPHOT.OUT.CMF.MEF", $outroot);
#my $bin1Name =  $ipprc->filename("PPSTACK.BIN1", $outroot);
#my $bin2Name =  $ipprc->filename("PPSTACK.BIN2", $outroot);
my $outputStats = $ipprc->filename("SKYCELL.STATS", $outroot);
my $traceDest = $ipprc->filename("TRACE.EXP", $outroot);
my $logDest = $ipprc->filename("LOG.EXP", $outroot);

# Perform stacking
unless ($no_op) {
    my $command = "$ppStack $listName $outroot";
    $command .= " -stats $outputStats";
    $command .= " -recipe PPSUB STACK";
    $command .= " -recipe PPSTATS WARPSTATS";
    $command .= " -F PSPHOT.PSF.SAVE PSPHOT.PSF.SKY.SAVE";
    $command .= " -F PSPHOT.OUTPUT PSPHOT.OUT.CMF.MEF";
    $command .= " -F PSPHOT.BACKMDL PSPHOT.BACKMDL.MEF";
    $command .= " -photometry";
    $command .= " -debug-stack" if defined $debug;
    $command .= " -tracedest $traceDest -log $logDest";
    $command .= " -dbname $dbname" if defined $dbname;

    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        run(command => $command, verbose => $verbose);
    unless ($success) {
        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
        &my_die("Unable to perform ppStack: $error_code", $stack_id, $error_code);
    }
    &my_die("Couldn't find expected output file: $outputName", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputName);
    &my_die("Couldn't find expected output file: $outputMask", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
    &my_die("Couldn't find expected output file: $outputWeight", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputWeight);
    &my_die("Couldn't find expected output file: $outputSources", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
#   &my_die("Couldn't find expected output file: $bin1Name",    $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin1Name);
#   &my_die("Couldn't find expected output file: $bin2Name",    $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin2Name);
    &my_die("Couldn't find expected output file: $outputStats", $stack_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputStats);

    # Get the statistics on the stacked image
    my $statsFile;              # File handle
    open $statsFile, $ipprc->file_resolve($outputStats) or &my_die("Can't open statistics file $outputStats: $!", $stack_id, $PS_EXIT_SYS_ERROR);
    my @contents = <$statsFile>; # Contents of file
    close $statsFile;
    my $metadata = $mdcParser->parse(join "", @contents) or
        &my_die("Unable to parse metadata config doc", $stack_id, $PS_EXIT_PROG_ERROR);
    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $stack_id, $PS_EXIT_PROG_ERROR);
}

unless ($no_update) {

    # Add the stack result
    {
        my $command = "$stacktool -addsumskyfile -stack_id $stack_id -uri $outputName -path_base $outroot";
        $command .= $stats->cmdflags();
        $command .= " -hostname $host" if defined $host;
        $command .= " -dbname $dbname" if defined $dbname;

        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
            run(command => $command, verbose => $verbose);
        unless ($success) {
            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
            &my_die("Unable to perform stacktool -addsumskyfile: $error_code", $stack_id, $error_code);
        }
    }

    # Register the run as completed
    # XXX why is this needed?  the stackRun is set to 'full' by the stacktool -addsumskyfile command...
    if (0) {
        my $command = "$stacktool -updaterun -stack_id $stack_id -state full"; # Command to run stacktool
        $command .= " -dbname $dbname" if defined $dbname;

        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
            run(command => $command, verbose => $verbose);
        unless ($success) {
            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
            warn("Unable to perform stacktool -updaterun: $error_code\n");
            exit($error_code);
        }
    }
}


sub my_die
{
    my $msg = shift;            # Warning message on die
    my $stack_id = shift;       # Stack identifier
    my $exit_code = shift;      # Exit code to add

    carp($msg);
    if (defined $stack_id and not $no_update) {
        my $command = "$stacktool -addsumskyfile -stack_id $stack_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;
}

__END__
