#!/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, $camera, $dbname, $outroot, $save_temps, $verbose, $no_update, $no_op, $logfile);
GetOptions(
           'magic_id=s'      => \$magic_id,   # Magic identifier
           '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,   # Redirect output?
           ) or pod2usage( 2 );

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

$ipprc->define_camera($camera);

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

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

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

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

### Do the heavy lifting
{
    my $command = "$streaks --streaks"; # Command to execute
    my @basenames;              # List of base names

    # 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, $error_code);
        }

        foreach my $basename (@basenames) {
            file_check( $basename . '.mask' );
        }
    } else {
        print "Skipping command: $command\n";
    }
}



### Input mask into database
{
    my $command = "$magictool -addmask";
    $command   .= " -magic_id $magic_id";
    $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 -addmask: $error_code", $magic_id, $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, $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 $exit_code = shift;      # Exit code to add

    carp($msg);
    if (defined $magic_id and not $no_update) {
        my $command = "$magictool -addmask";
        $command .= " -magic_id $magic_id";
        $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__
