#!/usr/bin/env perl

if (@ARGV != 5) { &escape ("ERROR: USAGE: defringe (imlist) (imstats) (frlist) (frstats) (outlist)"); }

$imlist  = $ARGV[0];
$imstats = $ARGV[1];
$frlist  = $ARGV[2];
$frstats = $ARGV[3];
$output  = $ARGV[4];

# load config info
$confdir = `gconfig CONFDIR`; chop $confdir;
if ($?) { &escape ("ERROR: config failure"); }

# various lists
@input  = split (" ", `cat $imlist`);
@output = split (" ", `cat $output`);
@ccds   = split (" ", `cameraconfig -ccds`);
@ccdn   = split (" ", `cameraconfig -ccdn`);
$Nccd   = @ccds;

# output directories must exist
for ($i = 0; $i < $Nccd; $i++) { &ckdir ($output[$i]); }

# convert image stats to optimal image parameters
$fitpars = &mktemp ("defringe");
vsystem ("fr.frstats -applyfit $imstats $frstats $fitpars");
@fitpars = `cat $fitpars`; foreach $line (@fitpars) { chop $line; }
@fringe  = `cat $frlist`; foreach $line (@fringe) { chop $line; }

# load flips parameter file
$paramfile = `gconfig FLIPS_PARAM_DEFRINGE`; chop $paramfile;
if ($?) { die "ERROR: missing FLIPS_PARAM_DEFRINGE in config\n"; }

open (FILE, "$paramfile");
@rawpars = <FILE>;
close (FILE);

$infile  = &mktemp ("\@defringe");
$parfile = &mktemp ("defringe");
for ($i = 0; $i < $Nccd; $i++) {

    # create input list file
    ($s0, $s1, $RF, $ds) = split (" ", $fitpars[$i]);
    open (FILE, ">$infile");
    print FILE "$input[$i]  $s0 1.0 $ds\n";
    print FILE "$fringe[$i] $s1 $RF $ds\n";
    close (FILE);

    print STDOUT "$fitpars[$i]\n";
    print STDOUT "$input[$i]  $s0 1.0 $ds\n";
    print STDOUT "$fringe[$i] $s1 $RF $ds\n";

    # create input par file
    @params = @rawpars;
    foreach $line (@params) {
	$line =~ s|LISTNAME|$infile|;
	$line =~ s|FILENAME|$output[$i]|;
	$line =~ s|COMMENT|Elixir Defringing|;
    }
    open (FILE, ">$parfile");
    foreach $line (@params) { print FILE "$line"; }
    close (FILE);

    if (-e $output[$i]) { unlink $output[$i]; }
    vsystem ("imcombred $parfile");
    if ($?) { &escape ("ERROR: failure running imcombred"); }
}

&goodbye;

########################################################

sub ckdir {

    my ($file) = $_[0];
    my ($dir, @words);

    @words = split ("/", $file);
    pop (@words);
    $dir = join ("/", @words);
    if ($dir eq "") { $dir = "."; }
    if (! -e $dir) {
	system ("mkdir -p $dir");
	if ($?) { die "ERROR: can't create output directory $dir\n"; }
    }
}

sub loadlist {

    my($command) = $_[0];
    my(@list, $line);

    @list = `$command`;

    foreach $line (@list) { chop ($line); }

    return @list;
}

sub vsystem {
    print STDERR "@_\n";
    $status = system ("@_");
    $status;
}

@temp = ();
sub mktemp {

    my ($base) = $_[0];
    my ($name);
    $name = `mktemp /tmp/$base.XXXXXX`;
    chop $name;

    push @temp, $name;
    return $name;
}

sub escape {
    $message = $_[0];
    foreach $name (@temp) {
	unlink ($name);
    }
    die "$message\n";
}

sub goodbye {
    foreach $name (@temp) { unlink ($name); }
    print STDERR "SUCCESS\n";
    exit 0;
}


