#! /usr/local/bin/perl -w

# Globus-Web specific installation script.
# This script checks out the globus source code, generates the documentation
# and installs it on the Globus WWW server's filespace.

use Carp;
use strict;
use FileHandle;

my $me;
chomp($me = `whoami`);
my $scratchdir='/sandbox/' . $me . '/globusdoc';
$ENV{CVSROOT}='/home/globdev/CVS/globus-current';
my @dirs;
my %tags;
my $ssl_path="/soft/pub/packages/SSLeay-0.9.0";
my $webroot = '/mcs/www-unix.globus.org/api';
my $docdir = "$webroot/c-". &construct_date;
my $internal_docdir = "$webroot/internalc-" . &construct_date;

if(! -d $webroot)
{
    croak("Cannot access Globus web site from this host");
}

system("rm -rf $scratchdir $docdir $internal_docdir");
&make_directory($scratchdir) || croak("Couldn't make $scratchdir");
&checkout_globus($scratchdir) || croak("Couldn't checkout globus");
&build_documentation("$scratchdir/globus", $docdir, $internal_docdir) ||
    croak("Couldn't build documentation");

# Update links to http://www-unix.globus.org/api/{c,internalc}
unlink("$webroot/c", "$webroot/internalc");
symlink('c-' . &construct_date, "$webroot/c");
symlink('internalc-' . &construct_date, "$webroot/internalc");

# Run installdox on the installed documentation
@dirs = &construct_dir_list($docdir);
%tags = &construct_tagfile_list($docdir);
foreach my $dir (@dirs)
{
    &install_html_doc("$docdir/$dir", %tags);
}
# And on the installed internal documentation
@dirs = &construct_dir_list($internal_docdir);
%tags = &construct_tagfile_list($internal_docdir);
foreach my $dir (@dirs)
{
    &install_html_doc("$internal_docdir/$dir", %tags);
}
sub make_directory
{
    my $dir_to_make = $_[0];
    my $dir = "";
    my $rc = 1;

    foreach (split('/', $dir_to_make))
    {
        $dir .= "/$_";
        if(! -d $dir)
        {
            mkdir($dir, 0755) || ($rc = 0);
        }
    }
    $rc;
}

sub checkout_globus
{
    my $dir = shift;
    my $rc = 1;

    chdir $dir;
    (system("cvs checkout globus_gass_copy")==0) || ($rc = 0);
    chdir 'globus';
    system("autoreconf");

    $rc;
}

sub construct_date()
{
    my @date = localtime(time);
    my $datestr;
    
    $datestr = sprintf("%04d-%02d-%02d", 1900+$date[5], $date[4]+1, $date[3]);

    $datestr;
}

sub build_documentation
{
    my($dir, $docdir, $internal_docdir)=@_;
    my $cmd;

    # First, apply a patch to the Doxyfile.in, to include globus-website
    # specific paths for the search engine.
    chdir $dir;
    system("patch -p0 < Miscellaneous/Doxygen/globus-web-doxyfile-patch");

    mkdir "$dir/doc-build", 0755;
    chdir "$dir/doc-build";

    $cmd  = "../configure ";
    $cmd .= "--with-ssl-path=$ssl_path ";
    $cmd .= "--without-ldap ";
    $cmd .= "--with-docdir=$docdir ";
    $cmd .= "--with-internal-docdir=$internal_docdir ";
    $cmd  .= " && make documentation internal_documentation ";
    $cmd  .= " && make install_documentation install_internal_documentation ";

    (system($cmd)==0) || 1;
}

sub update_docdir_links($$)
{
    
    1;
}

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

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


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 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";
    my %subst;
   
    # 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")
    {
        my $tagsubsts="";

        eval `grep '^%subst' $html_path/installdox`;
        foreach (keys %subst)
        {
            $tagsubsts .= " -l $_\@" . $tags{$_};
        }
        system "(cd $html_path && $installdox $tagsubsts *.html tree.js)";
    }
}

