#!@PERL_PATH@
# -*- perl -*-
#
# Script that checks which functions are documented in the manual.
#
# $Id: checkdoc.in,v 1.7 1998/07/16 15:55:21 ssb Exp $
#
# Author: Stig Bakken <ssb@guardian.no>
#

$dir = "functions";
$funclist = "funclist.txt";

@sgml_files = &glob($dir, "\\.sgml\$");
&check_functions(@sgml_files);

if ($ARGV[0] eq '-m') {
    foreach $function (sort @functions) {
	next if ($func_in_file{$function});
	print "$function\n";
    }
} else {
    printf("%-32s    %s\n", "FUNCTION NAME", "DOCUMENTED IN");
    print "-" x 70, "\n";
    foreach $function (sort @functions) {
	printf("%-32s    %s\n", $function, $func_in_file{$function});
    }
    @fif = keys %func_in_file;
    $n_functions = $#functions + 1;
    $n_documented = $#fif + 1;
    $percent_done = int(($n_documented * 100) / $n_functions);

    printf("\n%d of %d functions documented (%d%% done, %d%% remaining).\n",
	   $n_documented, $n_functions, $percent_done, 100-$percent_done);
}

foreach $func (@functions) {
    $in_funclist{$func}++;
}
foreach $func (sort keys %func_in_file) {
    next if $in_funclist{$func};
    print STDERR "possibly missing in $funclist: $func (documented in ".
	$func_in_file{$func} . ")\n";
}

### Subroutines:

sub glob {
    my $dir = shift;
    my $pattern = shift;
    my @f = ();
    if (opendir(DIR, $dir)) {
	foreach $file (readdir(DIR)) {
	    next unless ($file =~ m/$pattern/);
	    push(@f, $file);
	}
	close DIR;
    }
    return @f;
}

sub check_functions {
    foreach $sgml_file (@_) {
	$path = "$dir/$sgml_file";
	if (open(SGML, $path)) {
	    while (<SGML>) {
		next unless (m!(^|\s+)id\s*=\s*(['\"])([^\2]*?)\2\s*!);
		$func = $3;
		next unless ($func =~ s!^(function|class)\.!!);
		$func =~ s/-/_/g;
		$func_in_file{$func} = $sgml_file;
	    }
	} else {
	    warn "Could not open $path\n";
	}
    }
    
    if (open(FUNCLIST, $funclist)) {
	while (<FUNCLIST>) {
	    next if /^#/;
	    s!\s*!!g;
	    $len = length($_);
	    $longest = $len if ($len > $longest);
	    push(@functions, $_);
	}
    }
}
