#!/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 IPC::Cmd 0.36 qw( can_run run );
use File::Temp qw( tempfile );
use PS::IPP::Metadata::Config;
use PS::IPP::Metadata::List qw( parse_md_list );

use Astro::FITS::CFITSIO qw( :constants );
Astro::FITS::CFITSIO::PerlyUnpacking(1);

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

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

# Parse the command-line arguments
my ($magic_id, $node, $camera, $dbname, $outroot, $save_temps, $verbose, $no_update, $no_op, $logfile);

GetOptions(
           'magic_id=s'      => \$magic_id,   # Magic identifier
           'node=s'          => \$node,       # Node name
           'camera=s'        => \$camera,     # Camera name
           'dbname=s'        => \$dbname,     # Database name
           'outroot=s'       => \$outroot,    # Output root name
           'save-temps'      => \$save_temps, # Save temporary files?
           'verbose'         => \$verbose,    # Print stuff?
           'no-update'       => \$no_update,  # Don't update the database?
           'no-op'           => \$no_op,      # Don't do any operations?
           'logfile=s'       => \$logfile,
           ) or pod2usage( 2 );

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

# Unhandled exceptions should be passed on to my_die so they get pushed into the database
$SIG{__DIE__} = sub { die @_ if $^S;
                      my_die( $_[0], $magic_id, $node, $PS_EXIT_UNKNOWN_ERROR ); };

$ipprc->define_camera($camera);

# RemoveStreaks doesn't know about nebulous. It expects to be able to append strings to outroot
# to form valid file names.
# So forbid nebulous path in outroot. We could relax this by change RemoveStreaks to take all
# of the file names as arguments
if ($outroot =~ 'neb:/') {
    &my_die("RemoveStreaks does not support nebulous paths in outroot", $magic_id, $node,
        $PS_EXIT_CONFIG_ERROR);
}

# resolve any path:// or file:// in outroot
$outroot = $ipprc->file_resolve($outroot);

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

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

### Get a list of inputs
my $inputs;                     # List of inputs
{
    my $command = "$magictool -inputs -magic_id $magic_id -node $node"; # Command to run
    $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 magictool -inputs: $error_code", $magic_id, $node, $error_code);
    }

    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
        &my_die("Unable to parse metadata config doc", $magic_id, $node, $PS_EXIT_PROG_ERROR);

    $inputs = parse_md_list($metadata) or
        &my_die("Unable to parse metadata list", $magic_id, $node, $PS_EXIT_PROG_ERROR);
}


my @outputs;
### Do the heavy lifting
{
    my $command;                # Command to execute
    $command = "$removestreaks --outroot $outroot";
    $command .= " --verbose" if $verbose;

    # added per Paul Sydney's Jan 6, 2009
    $command .= " -D 3";


    # added per Paul Sydney's Jan 8, 2009 to help solve the STS problem
    # removed because it didn't help the STS problem and it quadrupled the number of false streaks for m31
    # Paul asked me to remove this parameter Jan 20,2009
#    $command .= " -C 0";


#    $command .= " -u 6";
#    $command .= " --test";

    if (scalar @$inputs == 1 and $node ne "root") {
        #
        #  RemoveStreak --detect --image filename --mask maskname --weight weightname --outroot path_base
        #
        # Leaf node: 'detect' stage
        my $innode = $$inputs[0];     # Input node

        # expected outputs for detect stage
        @outputs = ("${outroot}.clusters", "${outroot}_hough.fits", "${outroot}.streaks");

        my $diff_id = $innode->{diff_id};
        if (!$diff_id) {
            &my_die("input for node has null diff_id", $magic_id, $node, $PS_EXIT_UNKNOWN_ERROR);
        }
        my ($image, $mask, $weight) = resolve_inputs($innode);

        $command .= " --detect --image $image --mask $mask --weight $weight";

        # set threshold to 4 sigma
        $command .= ' -t 4';

        # create the list of inputs used at this stage. At higher levels the
        # these files will get catenated together to create the file for the subsquent stage
        # this causes major file pollution, but avoids multi-level queries
        # at higher level nodes.

        my ($in_fh, $input_list)  = open_list_file($outroot, "input.list");
        print $in_fh "$outroot\n";
        close $in_fh;
        my ($ifh, $image_list)  = open_list_file($outroot, "image.list");
        print $ifh "$image\n";
        close $ifh;
        my ($mfh, $mask_list)   = open_list_file($outroot, "mask.list");
        print $mfh "$mask\n";
        close $mfh;
        my ($wfh, $weight_list) = open_list_file($outroot, "weight.list");
        print $wfh "$weight\n";
        close $wfh;
    } else {
        #
        # RemoveStreak --merge --inputs input.list --images image0_1.list \
        #                      --masks mask0_1.list --weight weights0_1.list
        #                      --outroot outroot

        # Branch node: 'merge' stage

        # expected outputs from merge stage
        @outputs = ("${outroot}.streaks");

        my ($in_fh, $input_list) = open_list_file($outroot, "input.list");
        my ($ifh, $image_list)   = open_list_file($outroot, "image.list");
        my ($mfh, $mask_list)    = open_list_file($outroot, "mask.list");
        my ($wfh, $weight_list)  = open_list_file($outroot, "weight.list");
        my ($sfh, $streaks_list) = open_list_file($outroot, "streaks.list");

        # do this in eval so we can fault the exposure without
        # passing the $magic_id and $node everywhere
        eval {
            foreach my $innode (@$inputs) {
                # root for inputs from previous stage
                my $in_uri = $innode->{uri};
                print $sfh "$in_uri\n";

                cat_list_to_list($in_fh, $in_uri, "input.list");
                # build input lists by combining the lists from
                # previous stages
                cat_list_to_list($ifh, $in_uri, "image.list");
                cat_list_to_list($mfh, $in_uri, "mask.list");
                cat_list_to_list($wfh, $in_uri, "weight.list");
            }
            close $in_fh;
            close $ifh;
            close $mfh;
            close $wfh;
            close $sfh;

            $command .= " --merge --inputs $input_list";
            $command .= " --images $image_list --masks $mask_list --weights $weight_list" ;
            $command .= " --inputstreaks $streaks_list";
        };
        if ($@) {
            &my_die("failed to create file lists: $@", $magic_id, $node, $PS_EXIT_UNKNOWN_ERROR);
        }
    }

    unless ($no_op) {
        # RemoveStreaks fails if an output file already exists
        foreach my $output (@outputs) {
            unlink($output) if -e $output;
        }

        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 RemoveStreaks: $error_code", $magic_id, $node, $error_code);
        }

        foreach my $output (@outputs) {
            file_check( $output );
        }
    } else {
        print "Skipping command: $command\n";
    }
}


### Input result into database
{
    my $command = "$magictool -addresult";
    $command   .= " -magic_id $magic_id";
    $command   .= " -node $node";
    $command   .= " -uri $outroot";
    $command   .= " -dbname $dbname" if defined $dbname;

    # Add the processed file to the database
    unless ($no_update) {
        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 magictool -addresult: $error_code", $magic_id, $node, $error_code);
        }
    } else {
        print "Skipping command: $command\n";
    }
}
if ($node eq "root") {
    my $streaks_file = "$outroot.streaks";
    my $resolved = $ipprc->file_resolve($streaks_file);

    my $fh;
    open $fh, "<$resolved" or
        &my_die("failed to open streaks file $streaks_file", $magic_id, $node, $PS_EXIT_UNKNOWN_ERROR);
    # the first line in the streaks file contains the number of streaks found
    my $num_streaks = <$fh>;
    chomp $num_streaks;
    close $fh;
    print "$num_streaks streaks found on magicRun $magic_id\n" if $verbose;

    my $command = "$magictool -addmask";
    $command   .= " -magic_id $magic_id";
    $command   .= " -uri $streaks_file";
    $command   .= " -streaks $num_streaks";
    $command   .= " -dbname $dbname" if defined $dbname;

    # Add the processed file to the database
    unless ($no_update) {
        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);
            # This isn't going to work because our result was already entered.
            &my_die("Unable to perform magictool -addmask: $error_code", $magic_id, $node, $error_code);
        }
    } else {
        print "Skipping command: $command\n";
    }
}



### Pau.

sub open_list_file {
    my $outroot = shift;
    my $extension = shift;

    my $filename = "$outroot.$extension";

    my $fh;
    open $fh, ">$filename" or die "failed to open list file $filename";

    return ($fh, $filename);
}

sub cat_list_to_list   {
    my $out = shift;        # output file handle
    my $uri = shift;        # uri to append ...
    my $extension = shift;  # ... the extension to

    my $filename = "$uri.$extension";

    my $in;
    open $in, "<$filename" or die "failed to open list file: $filename";
    foreach my $line (<$in>) {
        print $out $line;
    }
}

sub resolve_inputs {

    my $node = shift;
    my $input_base = $node->{path_base};

    my $image = $ipprc->file_resolve($ipprc->filename("PPSUB.OUTPUT", $input_base));
    my $mask = $ipprc->file_resolve($ipprc->filename("PPSUB.OUTPUT.MASK", $input_base));
    my $weight= $ipprc->file_resolve($ipprc->filename("PPSUB.OUTPUT.VARIANCE", $input_base));

    return ($image, $mask, $weight);
}

sub file_check
{
    my $file = shift;           # Name of file
    &my_die("Unable to find output file: $file", $magic_id, $node, $PS_EXIT_SYS_ERROR) unless
        $ipprc->file_exists($file);
}

sub my_die
{
    my $msg = shift;            # Warning message on die
    my $magic_id = shift;       # Magic identifier
    my $node = shift;           # Node name
    my $exit_code = shift;      # Exit code to add

    carp($msg);
    if (defined $magic_id and defined $node and not $no_update) {
        my $command = "$magictool -addresult";
        $command .= " -magic_id $magic_id";
        $command .= " -node $node";
        $command .= " -code $exit_code";
        $command .= " -dbname $dbname" if defined $dbname;
        system($command);
    }
    exit $exit_code;
}

__END__
