#!/usr/bin/env perl

#
# Creating contents.hhc ...
#

use warnings;

if(!defined $ARGV[0]){
    die "create_index: No name of the file containing the index was Specified!\n";
}

open(CONTENTS, ">./contents.hhc") || die "Can't create contents.hhc";
open(MAXIMA_TOC, $ARGV[0]) || die "Can't open " . $ARGV[0];

while (<MAXIMA_TOC>)  {

    if ( m#.*<ul class="no-bullet"># ) {
        print CONTENTS "<ul>\n";
    }

    if ( m#.*</ul># ) {
        print CONTENTS "</ul>\n";
    } 

    if ( m#.*?<li><a\sname="(.*?)"\shref="(.*?)">.*?\s(.*?)</a># ) {
        print CONTENTS "<li><object type=\"text/sitemap\">\n";
        print CONTENTS "   <param name=\"Local\" value=\"$2\">\n";
        print CONTENTS "   <param name=\"Name\" value=\"$3\"></object>\n";
    } 
}

close(MAXIMA_TOC);
close(CONTENTS);

#
# Creating index.hhk ...
#

open(INDEX, ">./index.hhk") || die "Can't create index.hhk";

$i = 1;
%index_content = ();

while (open(MAXIMA_N,"./maxima_$i.html")) {
    while (<MAXIMA_N>)  {

        if ( m#.*?"top"><a\shref="maxima_(.*?)"><code>(.*?)</code></a># ||
             m#.*?"top"><a\shref="maxima_(.*?)">(.*?)</a># ) {
            unless ( $index_content{$2} ) {
                print INDEX "<li><object type=\"text/sitemap\">\n";
                print INDEX "   <param name=\"Local\" value=\"maxima_$1\">\n";
                print INDEX "   <param name=\"Name\" value=\"$2\"></object>\n";
                $index_content{$2} = 1;
            }
        }

    }
    close(MAXIMA_N);
    $i++;
}

close(INDEX);
