#!/usr/bin/env perl

use warnings;
use strict;

## report the program and machine
use Sys::Hostname;
my $host = hostname();
print "Starting script $0 on $host\n\n";

use DateTime;
my $mjd_start = DateTime->now->mjd;   # MJD of starting script

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 PS::IPP::Config 1.01 qw( :standard );
use File::Temp qw( tempfile );
use File::Basename qw( basename );
use Carp;

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 $pubtool = can_run('pubtool') or (warn "Can't find pubtool" and $missing_tools = 1);
my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
my $difftool = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
my $ppMops = can_run('ppMops') or (warn "Can't find ppMops" and $missing_tools = 1);
my $dsreg = can_run('dsreg') or (warn "Can't find dsreg" and $missing_tools = 1);
if ($missing_tools) {
    warn("Can't find required tools.");
    exit($PS_EXIT_CONFIG_ERROR);
}

# Parse the command-line arguments
my ( $pub_id, $exp_id, $camera, $stage, $stage_id, $format, $product, $workdir );
my ( $dbname, $verbose, $no_update, $save_temps, $redirect );

GetOptions(
    'pub_id=s'          => \$pub_id, # Publish identifier
    'exp_id=s'          => \$exp_id, # Exposure identifier
    'camera=s'          => \$camera, # Camera name
    'stage=s'           => \$stage,       # Stage of interest
    'stage_id=s'        => \$stage_id,    # Stage identifier
    'product=s'         => \$product,     # Datastore product name
    'workdir=s'         => \$workdir,     # Working directory
    'dbname=s'          => \$dbname,    # Database name
    'verbose'           => \$verbose,   # Print to stdout
    'no-update'         => \$no_update, # Don't update the database?
    'save-temps'        => \$save_temps, # Save temporary files?
    'redirect-output'   => \$redirect,   # Redirect output to log file?
    ) or pod2usage( 2 );

pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
pod2usage( -msg => "Required options: --pub_id --exp_id --camera --stage --stage_id --product --workdir",
           -exitval => $PS_EXIT_CONFIG_ERROR) unless
    defined $pub_id and
    defined $exp_id and
    defined $camera and
    defined $product and
    defined $stage and
    defined $stage_id and
    defined $workdir;

my $outroot = "$workdir/$product.$pub_id.$exp_id"; # Output root name

my $ipprc = PS::IPP::Config->new( $camera ) or
    &my_die( "Unable to set up", $pub_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration

$ipprc->outroot_prepare( $outroot );
my $logDest = "$outroot.log";
$ipprc->redirect_output($logDest) or &my_die( "Unable to redirect output", $pub_id, $PS_EXIT_SYS_ERROR ) if $redirect;

my $mdcParser = PS::IPP::Metadata::Config->new;


# Retrieve file name of interest
my %files;                      # Input filenames
my %zp;                         # Zero points
{
    my $command;                # Command to run

    if ($stage eq 'diff') {
        $command =  "difftool -diffskyfile -diff_id $stage_id";
    } elsif ($stage eq 'camera') {
        $command =  "camtool -processedexp -cam_id $stage_id";
    } else {
        &my_die( "Unrecognised stage: $stage", $pub_id, $PS_EXIT_CONFIG_ERROR );
    }
    $command .= " -dbname $dbname" if defined $dbname;

    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        run(command => $command, verbose => $verbose);
    &my_die( "Unable to retrieve filename", $pub_id, $PS_EXIT_SYS_ERROR) unless $success;

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

    my $components = parse_md_list($metadata) or
        &my_die("Unable to parse metadata list", $pub_id, $PS_EXIT_PROG_ERROR);

    foreach my $comp ( @$components ) {
        my $path_base = $comp->{path_base}; # Base name for file
        next if defined $comp->{quality} and $comp->{quality} > 0;

        (carp "Bad zpt_obs or exp_time for component" and next) if not defined $comp->{zpt_obs} or not defined $comp->{exp_time};
        my $zp = $comp->{zpt_obs} + 2.5 * log($comp->{exp_time}) / log(10);

        if ($stage eq 'diff') {
            my $skycell_id = $comp->{skycell_id};
            $files{"$skycell_id.pos"} = $ipprc->filename( "PPSUB.OUTPUT.SOURCES", $path_base );
            $files{"$skycell_id.neg"} = $ipprc->filename( "PPSUB.INVERSE.SOURCES", $path_base ) if
                defined $comp->{bothways} and $comp->{bothways};
            $zp{"$skycell_id.pos"} = $zp;
            $zp{"$skycell_id.neg"} = $zp if defined $comp->{bothways} and $comp->{bothways};
        } elsif ($stage eq 'camera') {
            my $cam_id = $comp->{cam_id};
            $files{$cam_id} = $ipprc->filename( "PSASTRO.OUTPUT", $path_base );
            $zp{$cam_id} = $zp;
        }
    }
}

# Prepare for data store input
my ($listFile, $listFileName) = tempfile("/tmp/publish.$pub_id.list.XXXX", UNLINK => !$save_temps );

# Process each file
foreach my $comp ( keys %files ) {
    my $file = $ipprc->file_resolve( $files{$comp} ) or
        &my_die("Unable to resolve file $files{$comp}", $pub_id, $PS_EXIT_SYS_ERROR);
    my_die("Unable to find file $file", $pub_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $file );

    my $zp = $zp{$comp};
    if ($product eq "IPP-MOPS") {
        my $outuri = "$outroot.$comp.fits";
        my $out = $ipprc->file_resolve( $outuri, 'create' ) or
            &my_die( "Unable to resolve output file $outuri", $pub_id, $PS_EXIT_SYS_ERROR);

        my $command = "$ppMops $file $zp $out";
        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
            run(command => $command, verbose => $verbose);
        &my_die( "Unable to translate $file", $pub_id, $PS_EXIT_SYS_ERROR) unless $success;
        &my_die( "Unable to find translated file $out", $pub_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $out );
        $file = $out;
    }

    # format: filename|filesize|md5sum|filetype|
    # note: since we omit filesize and md5sum, dsreg will calculate them
    print $listFile "$file|||$product|$comp|\n";
}


unless ($no_update) {
    my $command = "$dsreg --add $stage.$stage_id --copy --abspath --product $product --type $product --list $listFileName";

    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        run(command => $command, verbose => $verbose);
    &my_die( "Unable to register with data store", $pub_id, $PS_EXIT_SYS_ERROR) unless $success;
}

unless ($no_update) {
    my $command = "$pubtool -add -pub_id $pub_id -path_base $outroot";
    $command .= " -dbname $dbname" if defined $dbname;

    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
        run(command => $command, verbose => $verbose);
    &my_die( "Unable to register with data store", $pub_id, $PS_EXIT_SYS_ERROR) unless $success;
}



### Pau.


sub my_die
{
    my $msg = shift;            # Exit message
    my $pub_id = shift;         # Publish run identifier
    my $fault = shift;          # Fault code

    $fault = $PS_EXIT_PROG_ERROR unless defined $fault;

    carp($msg);
    if (defined $pub_id and not $no_update) {
        my $command = "$pubtool -add";
        $command .= " -pub_id $pub_id";
        $command .= " -path_base $outroot";
        $command .= " -fault $fault";
        $command .= " -dbname $dbname" if defined $dbname;

        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
            run(command => $command, verbose => $verbose);
    }
    exit $fault;
}


__END__
