my @tars;
my @vects;

@tars = </home/us/TKWorking/vects/*.tar.gz>;
@vects = </home/us/TKWorking/vects/*.vect>;

print "Found ".scalar @tars." tar files.\n";
print "Found ".scalar @vects." vect files.\n";

my @forgotten;
my @ok;

print "Building hash of tar contents...";

my %tarcontents;

foreach (@tars) {

    $tarcontents{$_} = `tar -tzf $_`;

}

print "done.\n";

print "Checking vects against tar contents...";

my $count;
$count = 0;

my %tarmatches;

foreach (@vects) {

    $count++;

    my $ktype;
    my $tag;

    $_ =~ /\/home\/us\/TKWorking\/vects\/(.+)\_(.+)\.vect/;

    $ktype = $1;
    $tag = $2;
    
    my $fname;
    $fname = "./${ktype}/${ktype}_${tag}.vect";

    my $thistar;
    $thistar = "/home/us/TKWorking/vects/${ktype}.tar.gz";

    my @results; 
    @results = grep(/${fname}/,$tarcontents{$thistar});

    unless (defined @results) {

	die("Couldn't find $fname in $thistar.\n");

    } else {

	$tarmatches{$thistar}++;

    }

}
    
print "All vect files found in a tar file.\n";
print "Counting total number of vects in each tar.\n";

my %tarsizes;
my $totalfilesintars;

foreach (@tars) {

    my $size;
    while ($tarcontents{$_} =~ /vect/g) { $size++ };
    $tarsizes{$_} = $size;
    $totalfilesintars += $size;

}

if ($totalfilesintars == scalar @vects) {

    print "Total files in tars ($totalfilesintars) MATCHES total vect files (".scalar @vects.")\n";

} else {

   die("Total files in tars ($totalfilesintars) DOES NOT MATCH total vect files (".scalar @vects.")\n");

}

print "Now comparing matchcounts with totals for each individual tar file.\n";

foreach (@tars) {

    if ($tarmatches{$_} != $tarsizes{$_}) { 

	die("Match count ($tarmatches{$_}) DOESN'T EQUAL size ($tarsizes{$_}) of tarfile $_.\n");

    }

}

print "Every file in each individual tar was matched with a corresponding VECT file.\n";

