#! @PERL@
#-*- perl -*-
#
# ${RHOME}/etc/build-help
# Usage:  build-help [--rhome dir] [--html] [--nroff] [--latex]
#                    [--dosnames] [--htmllists] [pkg] [lib]
# Install all html help files for package pkg to library lib
# (defaults are `${RHOME}/src/library/base' and the default library,
# `${RHOME}/library', respectively).

use File::Basename;
use Cwd;
use Getopt::Long;

@knownoptions = ("rhome:s", "html", "nroff", "latex", "debug",
		 "dosnames", "htmllists");
GetOptions (@knownoptions) || usage();

$cwd=getcwd();
if($opt_rhome){
    $RHOME=$opt_rhome;
    print STDERR "RHOME from --rhome: '$RHOME'\n" if $opt_debug;
}
elsif($ENV{"RHOME"}){
    $RHOME=$ENV{"RHOME"};
    print STDERR "RHOME from ENV: '$RHOME'\n" if $opt_debug;
}
else{
    chdir(dirname($0) . "/..");
    $RHOME=getcwd();
}
chdir($cwd);
print STDERR "Current directory (cwd): '$cwd'\n" if $opt_debug;


require "$RHOME/etc/Rdconvlib.pl";
require "$RHOME/etc/buildlib.pl";

# if option --htmllists is set we only rebuild some list files and
# exit

if($opt_htmllists){
    build_htmlpkglist("$RHOME/library");

    %anindex = read_anindex("$RHOME/library");
    %htmlindex = read_htmlindex("$RHOME/library");

    build_htmlfctlist("$RHOME/library");

    exit 0;
}





# default is to build all documentation formats
if(!$opt_html && !$opt_nroff && !$opt_latex){
    $opt_html = 1;
    $opt_nroff = 1;
    $opt_latex = 1;
}

($pkg, $lib, @mandir) = buildinit();
$dest = "$lib/$pkg";
print STDERR "Destination 'dest'= '$dest'\n" if $opt_debug;

# if building help or html pages we have to create the corresponding
# directories and build some indices

if($opt_html || $opt_nroff){
    build_index();
}
if($opt_latex){
  mkdir "$dest/latex", $dir_mod || die "Could not create $dest/latex: $!\n";
}

print " >>> Building help pages for package `$pkg'\n";
print "     Formats: ";
print "nroff " if $opt_nroff;
print "html " if $opt_html;
print "latex " if $opt_latex;
print "\n";


# get %htmlindex and %anindex

if($opt_html){
    %anindex = read_anindex($lib);
    %htmlindex = read_htmlindex($lib);
    build_htmlfctlist($lib);
}

foreach $manfile (@mandir) {
    if($manfile =~ /\.Rd$/i){
        $manfilebase = basename($manfile, (".Rd", ".rd"));
	print "  $manfilebase";

	if($opt_nroff){
	    $destfile = "$dest/help/$manfilebase";
	    open(nroffout, "| tbl | nroff -ms 2> /dev/null | " .
		 " ${RHOME}/etc/help.pretty >$destfile");
	    Rdconv($manfile, "nroff", "");# maybe $opt_debug
	    close(nroffout);
	}

	if($opt_html){
	    my $targetfile = $anindex{$manfilebase};
	    $destfile = "$dest/html/$targetfile.$HTML";
	    print "\t$destfile" if $opt_debug;
	    open(htmlout,">$destfile");
	    Rdconv($manfile, "html", "");# maybe $opt_debug
	    close(htmlout);
	}

	if($opt_latex){
	    $destfile = "$dest/latex/$manfilebase.tex";
	    open(latexout,">$destfile");
	    Rdconv($manfile, "latex", "");# maybe $opt_debug
	    close(latexout);
	}

	print "\n";
    }
}


sub usage {

    print "Usage:  build-help [--rhome dir] [--html] [--nroff]\n" .
	  "        [--latex] [--dosnmaes] [pkg] [lib]\n";

    exit 0;
}