#! /usr/bin/perl

use FileHandle;
use Getopt::Std;

# Local Variables
my $install_path;
my %opts = ();
my %tags = ();
my @dirs = ();
my $js_func = <<'EOF';
    function gExternalLnk(description, tagName, linkData)
    {
      fullLink = ""

      if (linkData!="")
      {
        fullLink = "'"+linkData+"' target=\"_top\""
      }
    
      linkItem = new Item(description, tagName, fullLink)
      return linkItem
    }
EOF

getopts('i', \%opts);
$install_path=$ARGV[0];

# Main stuff
%tags = &construct_tagfile_list($install_path);
@dirs = &construct_dir_list($install_path);

foreach my $dir (@dirs)
{
    &install_html_doc("$install_path/$dir", %tags);
    if(!exists($opts{i}))
    {
        &install_pdf_doc("$install_path/$dir");
    }
}

&create_all_apis_page(@dirs);
# End of main

sub construct_tagfile_list
{
    my(%tags);
    foreach (split(/\s+/, `ls -1 $_[0]`))
    {
        if(/.tag$/)
        {
            my $base;
	    ($base = $_) =~ s/.tag//;
	    $tags{$_}="../../$base/html";
	}
        if(/.tagi$/)
        {
            my $base;
	    ($base = $_) =~ s/.tagi//;
	    $tags{$_}="../../$base/html";
	}
    }
    %tags;
}

sub construct_dir_list
{
    my $base = $_[0];
    my @dirs;

    foreach (split(/\s+/, `ls -1 $base`))
    {
        if(-d "$base/$_")
        {
	    push(@dirs, $_);
	}
    }
    @dirs;
}

sub install_html_doc
{
    my $doc_path = shift;
    my %tags = @_;
    my ($tree_handle) = (new FileHandle);
    my $html_path = "$doc_path/html";
    my $installdox = "$html_path/installdox";
    
    # Create a doxygen search index for the search engine
    system("(cd $html_path && doxytag -s search.idx > /dev/null 2>&1)");

    # Perform tag substitution to relative locations of other
    # relevant documentation trees
    if(-r "$installdox")
    {
	$tagsubsts="";
	
	eval `grep '^%subst' $html_path/installdox`;
	
	foreach (keys %subst)
	{
	    $tagsubsts .= " -l $_\@" . $tags{$_};
	}
	system "$installdox $tagsubsts $html_path/*.html $html_path/tree.js";
    }

    # Update the left panel to include a link to the main tree,
    # as well as the PDF version of the documentation group.
    open($tree_handle, ">>$html_path/tree.js");
    # Append a function to create a link that replaces both frames to go
    # to other API documentation.
    print $tree_handle $js_func;
    print $tree_handle <<'EOF';
        insDoc(foldersTree,
               gExternalLnk("All API Pages", "", "../../index.html"))
EOF
    if(!exists($opts{i}))
    {
        print $tree_handle <<'EOF';
        insDoc(foldersTree,
               gLnk("Printable Version", "", "../latex/refman.pdf"))
EOF
    }

    print $tree_handle <<'EOF';
        insDoc(foldersTree,
               gExternalLnk("No Frames", "", "main.html"))
EOF
    close $tree_handle;

    # Bug in Doxygen 1.2.5--uses doxygen.css instead of user defined
    # one.
    symlink('globus.css', "$html_path/doxygen.css");
}

sub install_pdf_doc
{
    my $doc_path = shift;
    my %tags = @_;
    my $pdf_path="$doc_path/latex";

    system "(cd $pdf_path && make clean ps && rm *.dvi && make pdf)";
}

# Create the "All APIs" pages
sub create_all_apis_page
{
    my @dirs = @_;
    my $source_path="$dirs[0]/html";
    my $main = new FileHandle;

    symlink("$source_path/globus.css", "$install_path/globus.css");

    # Create the main documentation page
    open($main, ">$install_path/index.html");

    print $main <<EOF;
          <html>
	  <head>
	  <title>API Documentation</title>
	  <link rel="stylesheet" href="globus.css">
	  </head>
	  <body><h1>All APIs</h1>
		<ul>
EOF
    foreach (@dirs)
    {
        print $main <<EOF;
	<li>
	    <a href='$_/html/index.html' target='_top'>$_</a>
            <a href='$_/html/main.html' target='_top'>[no frames]</a>
        </li>
EOF
    }
    print $main "</ul></body>\n";
    close $main;
}
