#!/usr/bin/perl
#
#########################################################################
#                                                                       #
# Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       #
# All rights reserved.  Email: russ@q12.org   Web: www.q12.org          #
#                                                                       #
# This library is free software; you can redistribute it and/or         #
# modify it under the terms of EITHER:                                  #
#   (1) The GNU Lesser General Public License as published by the Free  #
#       Software Foundation; either version 2.1 of the License, or (at  #
#       your option) any later version. The text of the GNU Lesser      #
#       General Public License is included with this library in the     #
#       file LICENSE.TXT.                                               #
#   (2) The BSD-style license that is included with this library in     #
#       the file LICENSE-BSD.TXT.                                       #
#                                                                       #
# This library is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    #
# LICENSE.TXT and LICENSE-BSD.TXT for more details.                     #
#                                                                       #
#########################################################################

# check ode.doc and make sure that all the function definitions match what's
# in the headers. run this from the ode/doc directory!


# read in all header files into $h
$h='';
$incdir = '../../include/ode';
opendir (DIR,$incdir) or die "can't open directory \"$incdir\", stopped";
while ($i = readdir(DIR)) {
  next if ($i eq '.' or $i eq '..');
  next if ! -f "$incdir/$i";
  open (FILE,"$incdir/$i") or die "can't open file \"$incdir/$i\", stopped";
  $h .= join('',<FILE>);
  close (FILE);
}
closedir DIR;


sub checkFunc
{
  my $fn;
  foreach $fn (split (/;/,$_[0])) {	# split input on semicolons
    return if $fn =~ /NOT YET IMPLEMENTED/;
    $fn =~ s/^\s*//;		# strip whitespace at start
    $fn =~ s/\s;\s*$//;		# strip semicolon and whitespace at end
    next if $fn eq '';		# do next one if nothing left
    $fn = " $fn; ";		# put back whitespace and semicolon

    # protect some characters with backslash
    $fn =~ s/([.*+?\\\/\$\^()\[\]])/\\$1/g;

    # look for the function - if its there, remove it from the header
    printf "looking for \"$fn\"\n";
    if ($h !~ /$fn/) {
      print "\n*** COULD NOT FIND: \"$fn\"\n\n";
      exit 1;
    }
    $h =~ s/$fn/ /;
  }
  return '';
}

sub printFunc
{
  my $fn = $_[0];
  print "Undocumented: $fn\n";
}


# read the documentation
open (FILE,"ode.doc");
$a = join('',<FILE>);
close (FILE);

# strip whitespace out of $h and $a
$h =~ s/\s+/ /g;
$a =~ s/\s+/ /g;

# check that all documentation function definitions exist in the headers
$a =~ s/\@funcdef{([^}]*)}/&checkFunc($1)/ge;

# print out what's left - see if there are any functions that aren't documented
# first attempt to remove comments
print "\n\n";
$h =~ s/\/\*.+?\*\// /g;
$h =~ s/;\s*([^\(]+\([^\)]+\))\s*;/&printFunc($1)/ge;


print "\n\nOK\n";
