#!/usr/bin/perl

my $gzline;

if ($#ARGV < 0) {
	print STDERR "usage: $0 file ...\n";
	exit 1;
}

while (defined($ARGV[0])) {
	open(GZREAD, "gzcat $ARGV[0]|");
	while (chomp($gzline = <GZREAD>)) {
		if ($gzline =~ /^\.Xr (.+?) (.+?).*$/) {
			print STDERR "Checking for $1($2)...";
			if (&check_man($1, $2)) {
				print STDERR "found\n";
			} else {
				print STDERR "not found!\n";
				print "$1($2) in $ARGV[0]\n";
			}
		}
	}
	close(GZREAD);
	shift(@ARGV);
}

sub check_man {
	my ($man, $section) = @_;

	system("man -w $section $man >/dev/null 2>&1");
	if ($? != 0) {
		return 0;
	}

	return 1;
}
