#! /usr/bin/env perl

=head1 NAME

B<globus-update-doxygen-index> - Replace the share/doc/index.html file
with one which contains links to all of the installed package documentation.

=head1 SYNOPSIS

B<globus-update-doxygen-index> [path to documentation]

=head1 DESCRIPTION

B<globus-update-doxygen-index> creates an index.html file suitable for
browsing all globus-related documentation located in a documentation
directory.  By default, it builds an index in $GLOBUS_LOCATION/share/doc;
an option command line argument should point to a similarly structured
directory structure in another location.

=cut

use strict;
use Getopt::Long;
use IO::File;

my $gpath = $ENV{GPT_LOCATION};

if (!defined($gpath))
{
  $gpath = $ENV{GLOBUS_LOCATION};

}

if (!defined($gpath))
{
   die "GPT_LOCATION or GLOBUS_LOCATION needs to be set before running this script"
}

my @pkgs = ();

my $docpath="$gpath/share/doc";
if(defined($ARGV[0]))
{
    $docpath = $ARGV[0];
}

print "Updating documentation tree $docpath\n";

foreach (glob("$docpath/*"))
{
    if(-r "$_/html/main.html")
    {
	push(@pkgs, $_);
    }
}

my $file = new IO::File(">$docpath/index.html");
print $file <<EOF;

<html>
<head>
<title>API Documentation</title>
<link href="globus_common/html/globus.css" title="Globus" rel="stylesheet" type="text/css">
</head>
<body>
<h1>API Documentation</h1>

EOF

foreach (@pkgs)
{
    my $pkg = $_;
    $pkg =~ s|.*/||;
    print "    $pkg\n";
    print $file <<EOF;
    <ul>
	<li> <a href='$pkg/html/index.html' target='_top'>$pkg</a>
	    [<a href='$pkg/html/main.html' target='_top'>no frames</a>]
	</li>
    </ul>
EOF
}
print "Done\n";

print $file "</body></html>\n";

$file->close();
