#!/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 File::Basename qw( basename );
use PS::IPP::Metadata::Config;
use PS::IPP::Metadata::List qw( parse_md_list );

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_ds_id, $camera, $streaks, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base);
my ($outroot, $recoveryroot);
my ($replace, $remove);
my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile);

GetOptions(
           'magic_ds_id=s'  => \$magic_ds_id,# Magic destreak run identifier
           'camera=s'       => \$camera,     # camera for evaluating file rules
           'streaks=s'      => \$streaks,    # file containing the list of streaks
           'stage=s'        => \$stage,      # raw, chip, warp, or diff
           'stage_id=s'     => \$stage_id,   # exp_id, chip_id, warp_id, or diff_id
           'component=s'    => \$component,  # the class_id or skycell_id
           'uri=s'          => \$uri,        # uri of the input image
           'path_base=s'    => \$path_base,  # path_base of the input
           'cam_path_base=s'=> \$cam_path_base,  # path_base of the associated camera run
           'outroot=s'      => \$outroot,     # "directory" for temporary images (may be nebulous)
           'recoveryroot=s' => \$recoveryroot,# "directory" for saving the images of excised pixels
           'replace=s'        => \$replace,    # replace the input images with the results.
           'remove=s'         => \$remove,     # remove the original images when done YIKES!
           'save-temps'     => \$save_temps, # Save temporary files?
           'dbname=s'       => \$dbname,     # Database name
           '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_ds_id --camera --streaks --stage --stage_id --component --outroot",
           -exitval => 3) unless
    defined $magic_ds_id and
    defined $camera and
    defined $streaks and
    defined $stage and
    defined $stage_id and
    defined $component and
    defined $outroot;
    

my ($skycell_args, $class_id, $skycell_id);

if (($stage eq "raw") or ($stage eq "chip")) {
    $class_id = $component;
    $skycell_args = " -class_id $component";
} elsif ($stage eq "warp") {
    $skycell_id = $component;
    $skycell_args = " -skycell_id $component";
} elsif ($stage eq "diff") {
    $skycell_id = $component;
} else {
    &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
}

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

$ipprc->define_camera($camera);


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

my $basename = basename($path_base);

#
# parse the replace and remove arguments check for errors and set upt
# the appropriate paths
if (defined($replace) and ($replace eq "F")) {
    $replace = 0;
}

if (($stage eq "raw") and $replace and ! $recoveryroot) {
    &my_die("Can not replace raw files without defining recoveryroot.",
        $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
}

if (defined($remove) and ($remove eq "F")) {
    $remove = 0;
}

if ($remove and !$replace) {
    &my_die("cannot remove without replace", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
}

# create the output directories if it is not a nebulous path and it doesn't exist
if (index($outroot, "neb://") != 0) {
    if (! -e $outroot ) {
        my $code = system "mkdir -p $outroot";
        &my_die("cannot create output directory $outroot", $magic_ds_id, $component, 
                $code >> 8) if $code;
    }
}

my $backup_path_base;
if (! $remove) {
    $backup_path_base = "$outroot/$basename";
}

my $recovery_path_base;
if ($recoveryroot) {
    if (index($recoveryroot, "neb://") != 0) {
        if (! -e $recoveryroot ) {
            my $code = system "mkdir -p $recoveryroot";
            &my_die("cannot create output directory $outroot", $magic_ds_id, $component, 
                    $code >> 8) if $code;
        }
    }
    $recovery_path_base = "$recoveryroot/$basename";
}

# get skycell list if needed
my ($sfh, $skycell_list);          
if ($skycell_args) {
    my $command = "$magicdstool -magic_ds_id $magic_ds_id -getskycells $skycell_args";
    $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 magicdstool -diffskyfile $skycell_args: $error_code", $magic_ds_id, $component, $error_code);
    }

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

        my $skycells = parse_md_list($metadata) or
                &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);

        ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps);

        foreach my $skycell (@$skycells) {
            print $sfh "$skycell->{uri}\n"
        }
        close $sfh;
    }
}

my $image = $uri;

my ($mask, $ch_mask, $weight, $astrom);

if ($stage eq "raw") {
    $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
} elsif ($stage eq "chip") {
    # we use the mask output from the camera stage for input and replace
    # the output of the chip stage with that mask as well.
    $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id);
    $ch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
    $weight = $ipprc->filename("PPIMAGE.CHIP.WEIGHT", $path_base, $class_id);
    $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
} elsif ($stage eq "warp") {
    $mask   = $ipprc->filename("PSWARP.OUTPUT.MASK", $path_base);
    $weight = $ipprc->filename("PSWARP.OUTPUT.WEIGHT", $path_base);
} elsif ($stage eq "diff") {
    $mask   = $ipprc->filename("PPSUB.OUTPUT.MASK", $path_base);
    $weight = $ipprc->filename("PPSUB.OUTPUT.WEIGHT", $path_base);
}

{
    my $command = "$streaksremove -stage $stage -tmproot $outroot -streaks $streaks -image $image";
    $command .= " -class_id $class_id" if defined $class_id;
    $command .= " -recovery $recoveryroot" if defined $recoveryroot;
    $command .= " -astrom $astrom" if defined $astrom;
    $command .= " -mask $mask" if defined $mask;
    $command .= " -chip_mask $ch_mask" if defined $ch_mask;
    $command .= " -weight $weight" if defined $weight;
    $command .= " -skycelllist $skycell_list" if defined $skycell_list;
    $command .= " -dbname $dbname" if defined $dbname;
    unless (defined $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 streaksremove: $error_code", $magic_ds_id, $component, $error_code);
        }
    } else {
        print "skipping command $command\n";
    }
}

# if recovery and/or backup files were expected make sure they exist

# Input result into database
{
    my $command = "$magicdstool -adddestreakedfile";
    $command   .= " -magic_ds_id $magic_ds_id";
    $command   .= " -component $component";
    $command   .= " -backup_path_base $backup_path_base" if $backup_path_base;
    $command   .= " -recovery_path_base $recovery_path_base" if $recovery_path_base;
    $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 magicdstool -addresult: $error_code", $magic_ds_id, $component,
                $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_ds_id, $component, $PS_EXIT_SYS_ERROR) unless
        $ipprc->file_exists($file);
}

sub my_die
{
    my $msg = shift;            # Warning message on die
    my $magic_ds_id = shift;    # Magic DS identifier
    my $component = shift;      # class_id or skycell_id
    my $exit_code = shift;      # Exit code to add

    my $command = "$magicdstool -adddestreakedfile";
    $command   .= " -magic_ds_id $magic_ds_id";
    $command   .= " -component $component";
    $command   .= " -code $exit_code";
    $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) {
            carp("failed to update database for $magic_ds_id $component");
        }
    } else {
        print "Skipping command: $command\n";
    }

    carp($msg);
    exit $exit_code;
}

__END__
