#!/afs/athena/contrib/perl/perl

%wordcount = ();
$filecount = 0;

require "find.pl";

sub wanted {
    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    -f _ &&
#    print("searching $name . . . \n") &&
    &search ($name);
}

sub search
{
	($file) = @_;
	$ret_val = open (FILE, "<$file");
	if (!defined ($ret_val)) {
		warn "can't open file $file: $!";
		return;
	}
	
	++$filecount;
	print STDERR "$filecount . . .\n" if $filecount % 100 == 0;
	
	$/ = "";                        # Enable paragraph mode.
	$* = 1;                         # Enable multi-line patterns.
	
	<FILE>;				# skip first paragraph
	
	# Now read each paragraph and split into words.  Record each
	# instance of a word in the %wordcount associative array.
	
	while (<FILE>) {
	    s/-\n//g;                   # Dehyphenate hyphenations.
	    tr/A-Z/a-z/;                # Canonicalize to lower case.
	    @words = split(/\W*\s+\W*/, $_);
	    foreach $word (@words) {
		$wordcount{$word}++;    # Increment the entry.
	    }
	}
	
	close FILE;
}

# Traverse desired filesystems

&find($ARGV[0]);

# Now print out all the entries in the %wordcount array.

foreach $word (sort keys(%wordcount)) {
    printf "%20s %d\n", $word, $wordcount{$word};
}
