#!/usr/bin/env perl

# check the usage
if ($ARGV[0] eq "-h") { usage (); }
if ($ARGV[0] eq "-help") { usage (); }
if (@ARGV != 3) { die "ERROR: USAGE: fr.subset (master) (outdir) (subset)\n" }

$inlist = $ARGV[0];
$outdir = $ARGV[1];
$outlist= $ARGV[2];

print STDERR "ERROR: not fixed!!!\n";
exit 1;

# load in stats lines
open (FILE,  "$ARGV[0]");
@list = <FILE>;
close (FILE);

open (FILE, ">$outlist");

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

    # the infile contains (path) (root) (mode) (flag)
    ($path, $root, $mode, $stat) = split (" ", $line);

    if ($stat) {

	$basename = mk_basename ($path, $root, $mode);
	$Nccd = gt_names ($basename, "Nccd", $mode);

	for ($ccd = 0; $ccd < $Nccd; $ccd++) {

	    $outfile = mk_filename ($outdir, $root, "SPLIT", $ccd);

	    print FILE "$outfile\n";

	}
   }      
}    
close (FILE);

print STDERR "SUCCESS\n";

exit 0;

######## cfht MEF/SPLIT name utilities ###########

sub gt_names {
    # in: filename mode type
    # out: word

    my ($value);

    $value = "";
    if ($_[2] eq "MEF")   { $value = mefnames ($_[0], $_[1]); }
    if ($_[2] eq "SPLIT") { $value = splitnames ($_[0], $_[1]); }
    
    return $value;
}

sub mefnames {
    # in: (/path/file.fits) (mode) 
    # out: word

    my($fullname) = $_[0];
    my($mode) = $_[1];
    my($answer);

    # split rootdir and filename:
    my (@words) = split ("/", $fullname);
    $N = @words;

    if ($mode eq "path") {
	$answer = "";
	for ($i = 0; $i < $N - 1; $i++) {
	    $answer = $answer . $words[$i] . "/";
	}
	chop ($answer); # strip off last /
	if ($answer eq "") { $answer = "."; }
	return ($answer);
    }
    
    if ($mode eq "root") {
	$answer = "$words[$N-1]";
	$answer =~ s/.fits$//;
	return ($answer);
    }
    
    if ($mode eq "Nccd") {
	if (! -e $fullname) { return 0; }
	$answer = `echo $fullname | fields NEXTEND`;
	@words = split (" ", $answer);
	if ($words[1] eq "") { return 0; }
	return ($words[1]);
    }
    
    return (0);
}

sub splitnames {
    # in: (/path/file) (mode) 
    # out: word
    # /path/file is directory containing Nccd fits images

    my($fullname) = $_[0];
    my($mode) = $_[1];
    my($answer, $N, $tmpname, @imlist, @words);

    # split rootdir and filename:
    @words = split ("/", $fullname);
    $N = @words;

    if ($mode eq "path") {
	$answer = "";
	for ($i = 0; $i < $N - 1; $i++) {
	    $answer = $answer . $words[$i] . "/";
	}
	chop ($answer); # strip off last /
	if ($answer eq "") { $answer = "."; }
	return ($answer);
    }
    
    if ($mode eq "root") {
	$answer = "$words[$N-1]";
	return ($answer);
    }
    
    if ($mode eq "Nccd") {
	
	$tmpname = "$fullname/$words[$N-1]";
	@imlist = <$tmpname??.fits>;

	$Nccd = @imlist;
	return $Nccd;
    }
    
    return (0);
}

sub ckimtype {
    # in: (/path/file)
    # out: MEF | SPLIT | SINGLE | (NULL)
    
    my ($answer, $name, $value);
    my ($file) = $_[0];

    if (! -e $file) { 
	print STDOUT "can't open file\n";
	return ""; 
    }

    # if /path/file is a directory, it is split:
    if (-d $file) { return "SPLIT"; }

    # check that the file is a FITS image
    $answer = `file -L $file`;
    if ($?) { 
	print STDOUT "can't open file\n";
	return ""; 
    }
    ($name, $value) = split (" ", $answer);
    if ($value ne "FITS") { 
	print STDOUT "not a valid image\n";
	return ""; 
    }
    
    # check for NAXIS = 0 (MEF) 
    $answer = `echo $file | fields NAXIS`;
    # if ($?) { return ""; }
    ($name, $value) = split (" ", $answer);
    if ($value eq 0) { return "MEF"; }
    
    if ($value == 2) { return "SINGLE"; }

    print STDOUT "not a valid image\n";
    return "";
}

sub mk_basename {
    my ($path) = $_[0];
    my ($root) = $_[1];
    my ($mode) = $_[2];

    if ($mode eq "MEF") {
	$name = sprintf "%s/%s.fits", $path, $root;
	return $name;
    }
    if ($mode eq "SPLIT") {
	$name = sprintf "%s/%s", $path, $root;
	return $name;
    }
    return "";
}

sub mk_filename {
    my ($path) = $_[0];
    my ($root) = $_[1];
    my ($mode) = $_[2];
    my ($ccd)  = $_[3];

    if ($mode eq "MEF") {
	$name = sprintf "%s/%s.fits", $path, $root;
	return $name;
    }
    if ($mode eq "SPLIT") {
	$name = sprintf "%s/%s/%s%02d.fits", $path, $root, $root, $ccd;
	return $name;
    }
    return "";
}

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

sub goodbye {
    die "@_\n";
}

sub usage {
    print "\n";
    print "USAGE: fr.detrend (path) (root) (mode) (outdir)\n";
    print " path:   path to MEF image or SPLIT dir\n";
    print " root:   root name of image (MEF = root.fits, SPLIT = root/rootNN.fits)\n";
    print " mode:   MEF or SPLIT\n";
    print " outdir: directory for output results (all split)\n";
    exit 1;
}
