#!/usr/bin/env perl

if (@ARGV != 1) {
    print STDERR "USAGE: cfht.names (file)\n";
    exit 2;
}

# given (filename) : return (path) (name) (mef)

if (! -e $ARGV[0]) {
    print STDERR "file $ARGV[0] not found\n";
    exit 1;
}

$fullname = $ARGV[0];

# if we get a relative path, add cwd:
@words = split ("/", $ARGV[0]);
if ($words[0] ne "") {
    $cwd=`pwd`;
    chop ($cwd);
    $fullname = "$cwd/$ARGV[0]";
}
# we are now guaranteed to have a /complete/path/filename

# if file is a directory, we assume it is split:
if (-d $fullname) {
    $mef = "off";
} else {
    $mef = "on";
}

# split path and filename:
$path = "/";
@words = split ("/", $fullname);
$N = @words;
for ($i = 1; $i < $N - 1; $i++) {
    $path = $path . $words[$i] . "/";
}
chop ($path);   # strip off last /

$file = $words[$N-1];

if ($mef eq "on") {
    $file =~ s/.fits$//;
}

print STDOUT "$path $file $mef\n";
exit 0;
