#!/usr/bin/perl
use strict;

use HTML::TokeParser;
use Getopt::Long;

my ($help) = (0,0,0);
&GetOptions( "help"     => \$help);

sub get_all_files {
    my $dir="../../loncom/html/adm/help/tex/";
    opendir(DIR,$dir);
    my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
    return @files;
}

sub get_usage {
    my ($which)=@_;
    my @files;
    my $p = HTML::TokeParser->new("$which.manual.texxml");
    while (my $token = $p->get_token()) {
	if ($token->[0] eq 'S' && $token->[1] eq 'file') {
	    push(@files,$token->[2]{'name'});
	}
    }
    return @files;
}

sub print_unused {
    my ($all,$usage)=@_;
    my %all;
    my %no_exist;
    foreach my $file (@{ $all }) {$all{$file}=0;}
    foreach my $list (@{ $usage }) {
	foreach my $file (@{ $list }) {
	    if (exists($all{$file})) { $all{$file}++; } else { $no_exist{$file}++; }
	}
    }
    print("Usage count for existing Files:\n");
    foreach my $file (sort {uc($a) cmp uc($b)} (keys(%all))) {
	printf("%-50s: %s\n",$file,$all{$file});
    }
    print("Usage count for nonexistint Files:\n");
    foreach my $file (sort {uc($a) cmp uc($b)} (keys(%no_exist))) {
	printf("%-50s: %s\n",$file,$no_exist{$file});
    }
}

sub main {
    my @all_files=&get_all_files();
    my @author_usage=&get_usage('author');
    my @course_usage=&get_usage('course');
    my @developer_usage=&get_usage('developer');

    &print_unused(\@all_files,[\@author_usage,\@course_usage,
			       \@developer_usage]);
}

&main;
