#! /usr/bin/perl
##
## A code check script that checks for errors in documentation
## generated by doxygen.
##

if (! @ARGV) {
   print "docs <logfile> <target-dirs> <ignore-dirs>\n";
   exit(1);
}

open(LOGFILE,">$ARGV[0]") || die "Error opening $ARGV[0]";
#
# Find the packages that are source directories
#
$make_errors=`make docs > /dev/null 2>&1`;
  print "    <DoxygenErrors>$make_errors</DoxygenErrors>\n";

#
# Find the packages that are source directories
#
my @dirs = glob($ARGV[1]);
my @ignore = ();
my $dir = '';

while ($dir = $ARGV[2]) {
  push @ignore, glob($dir);
  shift;
}
#
# For each source directory, collect the source lines info.
#
foreach $dir (@dirs) {
  $ignore=0;
  foreach $d (@ignore) {
    if ($dir eq $d) {
       $ignore=1;
       next;
    }
  }
  if ($ignore == 1) {
     next;
  }
  if (-d "$dir") {

     $tmpfiles=`find $dir -name \"doxygen.out\"`;
     @ofiles = split(/\n/,$tmpfiles);
     #
     # For each file, count the number of warning lines
     #
     foreach $ofile (@ofiles) {
       print LOGFILE "\n";
       print LOGFILE "DOCS SUMMARY: $ofile\n";
       print LOGFILE "\n";
       $tmp = `cat $ofile`;
       print LOGFILE "$tmp\n";
       print LOGFILE "\n";

       open(INFILE,"$ofile") || die "Error opening $ofile";
       @lines = <INFILE>;
       close(INFILE);
       @warnings = grep(/Warning:/,@lines);

       print "    <Measurement>\n";
       print "      <Directory>$dir</Directory>\n";
       print "      <Name>$ofile</Name>\n";
       print "      <Value>" . ($#warnings + 1) . "</Value>\n";
       print "    </Measurement>\n";
    }
  }
}

close(LOGFILE);
