#!/usr/bin/perl -w

use warnings;
use strict;
use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use Pod::Usage qw( pod2usage );
use IPC::Cmd 0.36 qw( can_run run );

my $path = undef;
my $list = undef;
my $job = undef;

GetOptions(
        'path|p=s' => \$path,
        'list|l=s' => \$list,
        'job|j=s' => \$job,
        ) or pod2usage( 2 );


pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
pod2usage(
        -msg => "\n   Required options:\n\n".
        "--path <pathToJobsDir>\n".
        "--list <listOfJobNames> | --job <job>\n\n",
        -exitval => 3
        ) unless
defined $path and
(defined $list || defined $job);

if (defined $job) {getExpId($job);}
else {

    open (LIST, "$list") || die "couldn't open the file at '$list'";

    while ($job = <LIST>) {

        getExpId($job);

    }

close(LIST);
}
#################################################################
#
#
#################################################################
sub getExpId {
    my ($job) = @_;

    chomp($job);

    my @cmdOut = `tar -ztf $path/$job/B000.tar.gz`;

    my $line;
    foreach $line (@cmdOut) {

        if ($line =~ m/.*B000\/(\d+)\.FITS/) {

            print "$1\n";

        }

    }
}

