#!/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 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 );

# Parse the command-line arguments
my ($magic_id, $node, $camera, $dbname, $outroot, $save_temps, $verbose, $no_update, $no_op, $redirect);
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?
           'redirect-output' => \$redirect,   # Redirect output?
           ) 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;

$ipprc->define_camera($camera);

my $logDest = $ipprc->filename("LOG.EXP", $outroot, $magic_id) or
    &my_die("Missing entry from camera config", $magic_id, $PS_EXIT_CONFIG_ERROR);
$ipprc->redirect_output($logDest) if $redirect;

# Look for programs we need
my $missing_tools;
my $magictool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
my $streaks = 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);
}

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


### Do the heavy lifting
my @basenames;                  # Base names of inputs
{
    my $command;                # Command to execute
    if (scalar @$inputs == 1 and $node ne "root") {
        # Leaf node: 'detect' stage
        $command = "$streaks --detect";

        my $node = $$inputs[0];     # Input node
        push @basenames, $ipprc->file_resolve( $node->{path_base} );
    } else {
        # Branch node: 'merge' stage
        $command = "$streaks --merge";

        # Concatenate the names
        foreach my $node (@$inputs) {
            push @basenames, $ipprc->file_resolve( $node->{path_base} );
        }
    }

    $command .= ' ' . join(' ', @basenames);

    unless ($no_op) {
        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 $basename (@basenames) {
            file_check( $basename . '.clusters' );
            file_check( $basename . '.streaks' );
            file_check( $basename . '_hough.fits' );
        }
    } 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";
    }
}



### Pau.

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 {
    my $status = $?;
    system("sync") == 0
        or die "failed to execute sync: $!" ;
    $? = $status;
}

__END__
