#!/usr/bin/perl

use lib "$ENV{GLEDSYS}/perllib";
use Gled_ConfCat_Parser;
use ParseConfig;

my $def_cfg = "$ENV{GLEDSYS}/cfg/gled-htmldoc-create.rc";
my $cfg_parser = new ParseConfig(-defcfg=>$def_cfg);
$cfg_parser->parse();

Gled_ConfCat_Parser::import_build_config();

chdir $ENV{GLEDSYS};

`mkdir -p $OUTDIR`;

open HTML, ">$OUTDIR/index.html";
print HTML <<"fnord";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>gled/docs/classdoc</title>
<!--#include virtual="/head-specs.html" -->
</head>
	
<body
  BGCOLOR="#FFFFFF"
  TEXT="#000000"
  LINK="#0000FF"
  VLINK="#000080"
  ALINK="#FF0000"
>

<!--#include virtual="/top-bar.html" -->

<h2><code>gled/docs/classdoc</code></h2>

<div class="body">

fnord

open S, ">$MKDOC_MACRO";
print S "{\n";

for $ls (@{$resolver->{LibSetList}}) {
  print S "  Gled::theOne->AssertLibSet(\"$ls\");\n";
}

print S "  if(gROOT->LoadMacro(\"gled_html_doc.C++\")) gSystem->Exit(1);\n\n";

print S "  gEnv->SetValue(\"Root.Html.Header\", \"\");\n";
print S "  gEnv->SetValue(\"Root.Html.Footer\", \"\");\n";
print S "  gEnv->SetValue(\"Root.Html.HomePage\", \"http://www.gled.org/\");\n";

print S "  ZHtml html;\n";
print S "  html->SetOutputDir(\"$OUTDIR\");\n";
print S "  html->SetSourceDir(\"" .
  join(':', @{$resolver->{LibSetList}}) . "\");\n";

for $ls (@{$resolver->{LibSetList}}) {
  print HTML "\n\n<hr>\n";
  print HTML "<h3><code>$ls</code></h3>\n\n";

  my $ds = `cd $ls; make echo_dict_dirs`;
  chomp $ds;
  my @dirs = split(/\s+/, $ds);

  for $d (@dirs) {
    my @classes;
    open C, "find $ls/$d -name \\*.h | xargs grep -h ClassDef |";
    while($_ = <C>) {
      chomp;
      my $cnt = m/ClassDef\((\w+)\s*,\s*(\d+)\)/;
      unless($cnt) {
	print "SAFR: $_\n";
	next;
      }
      push @classes, $1;
      print S "  html->MakeClass(\"$1\", false);\n";
    }
    close C;
    if($#classes >= 0) {
      print HTML "<h4><code>$d</code></h4>\n\n";
      print HTML join(", ", map { "<a href=\"$_.html\">$_</a>" } @classes) . "\n\n";
    }
  }

}

print S "  html->gen_list_of_types();\n";
print S "  gSystem->Exit(0);\n";
print S "}\n";

close S;

print HTML "</div>\n</body>\n</html>\n";
`chmod a+x $OUTDIR/index.html`;

system "saturn $MKDOC_MACRO";
`rm $MKDOC_MACRO`;

print '*' x 80 . "\n";
print "* Html docs generated in $ENV{GLEDSYS}/$OUTDIR\n";
print '*' x 80 . "\n";
