#!/usr/bin/perl

my $myname = $0;
my $InitFile = "../src/tests.init";
my $verbose, $errors;
my $automap_dir, $auto_client_mntpnt;

sub verbose
{
	my $msg = shift;
	print $msg . "\n" if ($verbose);
}

sub get_init_vars
{
	open INITFILE, $InitFile || die "can't open init file";

	while (<INITFILE>) {
		next if (/(^#|^[:space:]*$)/);
		chomp;
		s/"//g;

		if (/AUTOMAP_DIR=/) {
			$automap_dir = (split /=/)[1];
		}

		if (/AUTO_CLIENT_MNTPNT=/) {
			$auto_client_mntpnt = (split /=/)[1];
		}

		if (/VERBOSE=/) {
			$verbose = (split /=/)[1];
		}
	}
	close INITFILE;
}

get_init_vars;

$error = 0;

open NAMES, $automap_dir . "/dat/" . $myname || die "can't open names file";
while (<NAMES>) {
	my $path;
	my $actual = 0;

	next if (/(^#|^[:space:]*$)/);
	chomp;

	($name, $count) = split;

	verbose "Looking at " . $name . " " . $count;

	$path =  $auto_client_mntpnt . "/badnames/";
	open DUMMY, $path . $name;
	close DUMMY;

	open COUNT, "ls $path 2>/dev/null |";
	while (<COUNT>) {
		$actual++ if (/$name/);
	}

	if ($actual ne $count) {
		print "Expected $name $count times but saw it $actual times\n";
		$error++;
	}
}
close NAMES;

if ($error) {
	print "$myname: FAILED" . "\n";
} else {
	print "$myname: SUCCEEDED" . "\n";
}

