#!./perl -Ilib -w

# This file should really be extracted from a .PL file

use Config;		# for config options in the makefile
use File::Spec;
use Getopt::Long;	# for command-line parsing
use Cwd;
use Pod::Html 'anchorify';

=head1 NAME

installhtml - converts a collection of POD pages to HTML format.

=head1 SYNOPSIS

    installhtml  [--help] [--podpath=<name>:...:<name>] [--podroot=<name>]
         [--htmldir=<name>] [--htmlroot=<name>]  [--norecurse] [--recurse]
         [--splithead=<name>,...,<name>]   [--splititem=<name>,...,<name>]
         [--libpods=<name>,...,<name>]        [--ignore=<name>,...,<name>]
         [--verbose]

=head1 DESCRIPTION

I<installhtml> converts a collection of POD pages to a corresponding
collection of HTML pages.  This is primarily used to convert the pod
pages found in the perl distribution.

=head1 OPTIONS

=over 4

=item B<--help> help

Displays the usage.

=item B<--podroot> POD search path base directory

The base directory to search for all .pod and .pm files to be converted.
Default is current directory.

=item B<--podpath> POD search path

The list of directories to search for .pod and .pm files to be converted.
Default is 'podroot/.'.

=item B<--recurse> recurse on subdirectories

Whether or not to convert all .pm and .pod files found in subdirectories
too.  Default is to not recurse.

=item B<--htmldir> HTML destination directory

The base directory which all HTML files will be written to.  This should
be a path relative to the filesystem, not the resulting URL.

=item B<--htmlroot> URL base directory

The base directory which all resulting HTML files will be visible at in
a URL.  The default is '/'.

=item B<--splithead> POD files to split on =head directive

Comma-separated list of pod files to split by the =head directive.  The
.pod suffix is optional. These files should have names specified
relative to podroot.

=item B<--splititem> POD files to split on =item directive

Comma-separated list of all pod files to split by the =item directive.
The .pod suffix is optional.  I<installhtml> does not do the actual
split, rather it invokes I<splitpod> to do the dirty work.  As with
--splithead, these files should have names specified relative to podroot.

=item B<--splitpod> Directory containing the splitpod program

The directory containing the splitpod program. The default is 'podroot/pod'.

=item B<--libpods> library PODs for LE<lt>E<gt> links

Comma-separated list of "library" pod files.  This is the same list that
will be passed to pod2html when any pod is converted.

=item B<--ignore> files to be ignored

Comma-separated of files that shouldn't be installed, given relative
to podroot.

=item B<--verbose> verbose output

Self-explanatory.

=back

=head1 EXAMPLE

The following command-line is an example of the one we use to convert
perl documentation:

    ./installhtml --podpath=lib:ext:pod:vms   \
			--podroot=/usr/src/perl     \
			--htmldir=/perl/nmanual     \
			--htmlroot=/perl/nmanual    \
			--splithead=pod/perlipc     \
			--splititem=pod/perlfunc    \
			--libpods=perlfunc,perlguts,perlvar,perlrun,perlop \
			--recurse \
			--verbose

=head1 AUTHOR

Chris Hall E<lt>hallc@cs.colorado.eduE<gt>

=cut

my $usage;

$usage =<<END_OF_USAGE;
Usage: $^PROGRAM_NAME --help --podpath=<name>:...:<name> --podroot=<name>
         --htmldir=<name> --htmlroot=<name> --norecurse --recurse
         --splithead=<name>,...,<name> --splititem=<name>,...,<name>
         --libpods=<name>,...,<name> --ignore=<name>,...,<name> --verbose

    --help      - this message
    --podpath   - colon-separated list of directories containing .pod and
                  .pm files to be converted (. by default).
    --podroot   - filesystem base directory from which all relative paths in
                  podpath stem (default is .).
    --htmldir   - directory to store resulting html files in relative
                  to the filesystem (\$podroot/html by default).
    --htmlroot  - http-server base directory from which all relative paths
                  in podpath stem (default is /).
    --libpods   - comma-separated list of files to search for =item pod
                  directives in as targets of C<> and implicit links (empty
                  by default).
    --norecurse - don't recurse on those subdirectories listed in podpath.
                  (default behavior).
    --recurse   - recurse on those subdirectories listed in podpath
    --splithead - comma-separated list of .pod or .pm files to split.  will
                  split each file into several smaller files at every occurrence
                  of a pod =head[1-6] directive.
    --splititem - comma-separated list of .pod or .pm files to split using
                  splitpod.
    --splitpod  - directory where the program splitpod can be found
                  (\$podroot/pod by default).
    --ignore    - comma-separated list of files that shouldn't be installed.
    --verbose   - self-explanatory.

END_OF_USAGE

my (@libpods, @podpath, $podroot, $htmldir, $htmlroot, $recurse, @splithead,
    @splititem, $splitpod, $verbose, $pod2html, @ignore);

@libpods = @( () );
@podpath = @( "." );	# colon-separated list of directories containing .pod
			# and .pm files to be converted.
$podroot = ".";		# assume the pods we want are here
$htmldir = "";		# nothing for now...
$htmlroot = "/";	# default value
$recurse = 0;		# default behavior
@splithead = @( () );	# don't split any files by default
@splititem = @( () );	# don't split any files by default
$splitpod = "";		# nothing for now.

$verbose = 0;		# whether or not to print debugging info

$pod2html = "pod/pod2html";

usage("") unless (nelems @ARGV);

# Overcome shell's p1,..,p8 limitation.  
# See vms/descrip_mms.template -> descrip.mms for invokation.
if ( $^OS_NAME eq 'VMS' ) { @ARGV = split(m/\s+/,@ARGV[0]); }

our (%Options);

# parse the command-line
my $result = GetOptions( \%Options, < qw(
	help
	podpath=s
	podroot=s
	htmldir=s
	htmlroot=s
	libpods=s
	ignore=s
	recurse!
	splithead=s
	splititem=s
	splitpod=s
	verbose
));
usage("invalid parameters") unless $result;
parse_command_line();


# set these variables to appropriate values if the user didn't specify
#  values for them.
$htmldir = "$htmlroot/html" unless $htmldir;
$splitpod = "$podroot/pod" unless $splitpod;


# make sure that the destination directory exists
(mkdir($htmldir, 0755) ||
	die "$^PROGRAM_NAME: cannot make directory $htmldir: $^OS_ERROR\n") if ! -d $htmldir;


# the following array will eventually contain files that are to be
# ignored in the conversion process.  these are files that have been
# process by splititem or splithead and should not be converted as a
# result.
my @splitdirs;

# split pods. It's important to do this before convert ANY pods because
# it may affect some of the links
@splitdirs = @( () );    # files in these directories won't get an index
split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, < @splithead);
split_on_item($podroot,           \@splitdirs, \@ignore, < @splititem);


# convert the pod pages found in @poddirs
#warn "converting files\n" if $verbose;
#warn "\@ignore\t= @ignore\n" if $verbose;
foreach my $dir ( @podpath) {
    installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
}


# now go through and create master indices for each pod we split
foreach my $dir ( @splititem) {
    print $^STDOUT, "creating index $htmldir/$dir.html\n" if $verbose;
    create_index("$htmldir/$dir.html", "$htmldir/$dir");
}

foreach my $dir ( @splithead) {
    (my $pod = $dir) =~ s,^.*/,,;
    $dir .= ".pod" unless $dir =~ m/(\.pod|\.pm)$/;
    # let pod2html create the file
    runpod2html($dir, 1);

    # now go through and truncate after the index
    $dir =~ m/^(.*?)(\.pod|\.pm)?$/sm;
    my $file = "$htmldir/$1";
    print $^STDOUT, "creating index $file.html\n" if $verbose;

    # read in everything until what would have been the first =head
    # directive, patching the index as we go.
    open(my $h_fh, "<", "$file.html") ||
	die "$^PROGRAM_NAME: error opening $file.html for input: $^OS_ERROR\n";
    $^INPUT_RECORD_SEPARATOR = "";
    my @data = @( () );
    while ( ~< *$h_fh) {
	last if m/name="name"/i;
	$_ =~ s{href="#(.*)">}{$( do {
	    my $url = "$pod/$1.html" ;
	    $url = Pod::Html::relativize_url( $url, "$file.html" )
	    if ( ! defined %Options{?htmlroot} || %Options{?htmlroot} eq '' );
	    "href=\"$url\">" ;
	})}gi;
	push @data, $_;
    }
    close($h_fh);

    # now rewrite the file
    open($h_fh, ">", "$file.html") ||
	die "$^PROGRAM_NAME: error opening $file.html for output: $^OS_ERROR\n";
    print $h_fh, "$(join ' ',@data)", "\n";
    close($h_fh);
}

##############################################################################


sub usage {
    warn "$^PROGRAM_NAME: $(join ' ',@_)\n" if (nelems @_);
    die $usage;
}


sub parse_command_line {
    usage() if defined %Options{?help};
    %Options{+help} = ""; 	    	    # make -w shut up

    # list of directories
    @podpath   = split(":", %Options{?podpath}) if defined %Options{?podpath};

    # lists of files
    @splithead = split(",", %Options{?splithead}) if defined %Options{?splithead};
    @splititem = split(",", %Options{?splititem}) if defined %Options{?splititem};
    @libpods   = split(",", %Options{?libpods}) if defined %Options{?libpods};

    $htmldir  = %Options{?htmldir}	    if defined %Options{?htmldir};
    $htmlroot = %Options{?htmlroot}	    if defined %Options{?htmlroot};
    $podroot  = %Options{?podroot}	    if defined %Options{?podroot};
    $splitpod = %Options{?splitpod}	    if defined %Options{?splitpod};

    $recurse  = %Options{?recurse}	    if defined %Options{?recurse};
    $verbose  = %Options{?verbose}	    if defined %Options{?verbose};

    @ignore = map "$podroot/$_", split(",", %Options{?ignore}) if defined %Options{?ignore};
}


sub create_index($html, $dir) {
    (my $pod = $dir) =~ s,^.*/,,;
    my(@files, @filedata, @index);
    my($lcp1,$lcp2);


    # get the list of .html files in this directory
    opendir(my $dirhandle, $dir) ||
	die "$^PROGRAM_NAME: error opening directory $dir for reading: $^OS_ERROR\n";
    @files = sort( grep { m/\.html?$/ }, @( readdir($dirhandle)));
    closedir($dirhandle);

    open(my $html_fh, ">", "$html") ||
	die "$^PROGRAM_NAME: error opening $html for output: $^OS_ERROR\n";

    # for each .html file in the directory, extract the index
    #	embedded in the file and throw it into the big index.
    print $html_fh, "<DL COMPACT>\n";
    foreach my $file ( @files) {
	$^INPUT_RECORD_SEPARATOR = "";

	open(my $in_fh, "<", "$dir/$file") ||
	    die "$^PROGRAM_NAME: error opening $dir/$file for input: $^OS_ERROR\n";
	@filedata = @( ~< *$in_fh );
	close($in_fh);

	# pull out the NAME section
	my $name;
	($name) = < grep( { m/name="name"/i }, @filedata);
	@($lcp1,$lcp2) = @($name =~ m,/H1>\s(\S+)\s[\s-]*(.*?)\s*$,smi);
	if (defined $lcp1 and $lcp1 =~ m,^<P>$,i) { # Uninteresting.  Try again.
	    @($lcp1,$lcp2) = @($name =~ m,/H1>\s<P>\s*(\S+)\s[\s-]*(.*?)\s*$,smi);
	}
	my $url= "$pod/$file" ;
	if ( ! defined %Options{?htmlroot} || %Options{?htmlroot} eq '' ) {
	    $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
	}

	if (defined $lcp1) {
	    print $html_fh, qq(<DT><A HREF="$url">);
	    print $html_fh, "$lcp1</A></DT><DD>$lcp2</DD>\n";
	}

	next;

	@index = grep( { m/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s }, @filedata);
	for ( @index) {
	    s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$lcp2/s;
	    s,#,$dir/$file#,g;
	    print $html_fh, "$_\n<P><HR><P>\n";
	}
    }
    print $html_fh, "</DL>\n";

    close($html_fh);
}


sub split_on_head($podroot, $htmldir, $splitdirs, $ignore, @< @splithead) {
    my($dirname, $filename);

    # split the files specified in @splithead on =head[1-6] pod directives
    print $^STDOUT, "splitting files by head.\n" if $verbose &&( (nelems @splithead)-1) +>= 0;
    foreach my $pod ( @splithead) {
	# figure out the directory name and filename
	$pod      =~ s,^([^/]*)$,/$1,;
	$pod      =~ m,(.*)/(.*?)(\.pod)?$,;
	$dirname  = $1;
	$filename = "$2.pod";

	# since we are splitting this file it shouldn't be converted.
	push(@$ignore, "$podroot/$dirname/$filename");

	# split the pod
	splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
	    $splitdirs);
    }
}


sub split_on_item($podroot, $splitdirs, $ignore, @< @splititem) {
    my($pwd, $dirname, $filename);

    print $^STDOUT, "splitting files by item.\n" if $verbose &&( (nelems @splititem)-1) +>= 0;
    $pwd = getcwd();
    my $splitter = File::Spec->rel2abs("$splitpod/splitpod", $pwd);
    my $perl = File::Spec->rel2abs($^EXECUTABLE_NAME, $pwd);
    foreach my $pod ( @splititem) {
	# figure out the directory to split into
	$pod      =~ s,^([^/]*)$,/$1,;
	$pod      =~ m,(.*)/(.*?)(\.pod)?$,;
	$dirname  = "$1/$2";
	$filename = "$2.pod";

	# since we are splitting this file it shouldn't be converted.
	push(@$ignore, "$podroot/$dirname.pod");

	# split the pod
	push(@$splitdirs, "$podroot/$dirname");
	if (! -d "$podroot/$dirname") {
	    mkdir("$podroot/$dirname", 0755) ||
		    die "$^PROGRAM_NAME: error creating directory $podroot/$dirname: $^OS_ERROR\n";
	}
	chdir("$podroot/$dirname") ||
	    die "$^PROGRAM_NAME: error changing to directory $podroot/$dirname: $^OS_ERROR\n";
	die "$splitter not found. Use '-splitpod dir' option.\n"
	    unless -f $splitter;
	system($perl, $splitter, "../$filename") &&
	    warn "$^PROGRAM_NAME: error running '$splitter ../$filename'"
		 ." from $podroot/$dirname";
    }
    chdir($pwd);
}


#
# splitpod - splits a .pod file into several smaller .pod files
#  where a new file is started each time a =head[1-6] pod directive
#  is encountered in the input file.
#
sub splitpod($pod, $poddir, $htmldir, $splitdirs) {
    my(@poddata, @filedata, @heads);
    my($file, $i, $j, $prevsec, $section, $nextsec);

    print $^STDOUT, "splitting $pod\n" if $verbose;

    # read the file in paragraphs
    $^INPUT_RECORD_SEPARATOR = "";
    open(my $splitin_fh, "<", "$pod") ||
	die "$^PROGRAM_NAME: error opening $pod for input: $^OS_ERROR\n";
    @filedata = @( ~< *$splitin_fh );
    close($splitin_fh) ||
	die "$^PROGRAM_NAME: error closing $pod: $^OS_ERROR\n";

    # restore the file internally by =head[1-6] sections
    @poddata = @( () );
    $i = 0; $j = -1;
    while ( $i +<=( (nelems @filedata)-1) ) {
	$j++ if (@filedata[$i] =~ m/^\s*=head[1-6]/);
	if ($j +>= 0) { 
	    @poddata[$j]  = "" unless defined @poddata[$j];
	    @poddata[$j] .= "\n@filedata[$i]" if $j +>= 0;
	}
        $i++;
    }

    # create list of =head[1-6] sections so that we can rewrite
    #  L<> links as necessary.
    my %heads = %( () );
    foreach my $i (0..((nelems @poddata)-1)) {
	%heads{+anchorify($1)} = 1 if @poddata[$i] =~ m/=head[1-6]\s+(.*)/;
    }

    # create a directory of a similar name and store all the
    #  files in there
    $pod =~ s,.*/(.*),$1,;	# get the last part of the name
    my $dir = $pod;
    $dir =~ s/\.pod//g;
    push(@$splitdirs, "$poddir/$dir");
    mkdir("$poddir/$dir", 0755) ||
	die "$^PROGRAM_NAME: could not create directory $poddir/$dir: $^OS_ERROR\n"
	unless -d "$poddir/$dir";

    @poddata[0] =~ m/^\s*=head[1-6]\s+(.*)/;
    $section    = "";
    $nextsec    = $1;

    # for each section of the file create a separate pod file
    for my $i ( 0 .. (nelems @poddata)-1 ) {
	# determine the "prev" and "next" links
	$prevsec = $section;
	$section = $nextsec;
	if ($i +<( (nelems @poddata)-1)) {
	    @poddata[$i+1] =~ m/^\s*=head[1-6]\s+(.*)/;
	    $nextsec       = $1;
	} else {
	    $nextsec = "";
	}

	# determine an appropriate filename (this must correspond with
	#  what pod2html will try and guess)
	# $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
	$file = "$dir/" . anchorify($section) . ".pod";

	# create the new .pod file
	print $^STDOUT, "\tcreating $poddir/$file\n" if $verbose;
	open(my $splitout_fh, ">", "$poddir/$file") ||
	    die "$^PROGRAM_NAME: error opening $poddir/$file for output: $^OS_ERROR\n";
	@poddata[$i] =~ s,L<([^<>]*)>,$(
			defined %heads{?anchorify($1)} ?? "L<$dir/$1>" !! "L<$1>"
		     ),g;
	print $splitout_fh, @poddata[$i]."\n\n";
	print $splitout_fh, "=over 4\n\n";
	print $splitout_fh, "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
	print $splitout_fh, "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
	print $splitout_fh, "=item *\n\nUp to L<$dir>\n\n";
	print $splitout_fh, "=back\n\n";
	close($splitout_fh) ||
	    die "$^PROGRAM_NAME: error closing $poddir/$file: $^OS_ERROR\n";
    }
}


#
# installdir - takes care of converting the .pod and .pm files in the
#  current directory to .html files and then installing those.
#
sub installdir($dir, $recurse, $podroot, $splitdirs, $ignore) {
    my(@dirlist, @podlist, @pmlist, $doindex);

    @dirlist = @( () );	# directories to recurse on
    @podlist = @( () );	# .pod files to install
    @pmlist  = @( () );	# .pm files to install

    # should files in this directory get an index?
    $doindex = (grep( { $_ eq "$podroot/$dir" }, @$splitdirs) ?? 0 !! 1);

    opendir(my $dirhandle, "$podroot/$dir")
	|| die "$^PROGRAM_NAME: error opening directory $podroot/$dir: $^OS_ERROR\n";

    # find the directories to recurse on
    @dirlist = map { if ($^OS_NAME eq 'VMS') {m/^(.*)\.dir$/i; "$dir/$1";} else {"$dir/$_";}},
      grep( { -d "$podroot/$dir/$_" && !m/^\.{1,2}/ }, @( readdir($dirhandle))) if $recurse;
    rewinddir($dirhandle);

    # find all the .pod files within the directory
    @podlist = map { m/^(.*)\.pod$/; "$dir/$1" },
      grep( { ! -d "$podroot/$dir/$_" && m/\.pod$/ }, @( readdir($dirhandle)));
    rewinddir($dirhandle);

    # find all the .pm files within the directory
    @pmlist = map { m/^(.*)\.pm$/; "$dir/$1" },
      grep( { ! -d "$podroot/$dir/$_" && m/\.pm$/ }, @( readdir($dirhandle)));

    closedir($dirhandle);

    # recurse on all subdirectories we kept track of
    foreach my $dir ( @dirlist) {
	installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
    }

    # install all the pods we found
    foreach my $pod ( @podlist) {
	# check if we should ignore it.
	next if $pod =~ m(/t/); # comes from a test file
	next if grep( { $_ eq "$pod.pod" }, @$ignore);

	# check if a .pm files exists too
	if (grep( { $_ eq $pod }, @pmlist)) {
	    print  $^STDOUT, "$^PROGRAM_NAME: Warning both '$podroot/$pod.pod' and "
		. "'$podroot/$pod.pm' exist, using pod\n";
	    push(@ignore, "$pod.pm");
	}
	runpod2html("$pod.pod", $doindex);
    }

    # install all the .pm files we found
    foreach my $pm ( @pmlist) {
	# check if we should ignore it.
	next if $pm =~ m(/t/); # comes from a test file
	next if grep( { $_ eq "$pm.pm" }, @ignore);

	runpod2html("$pm.pm", $doindex);
    }
}


#
# runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
#  file.
#
sub runpod2html($pod, $doindex) {
    my($html, $i, $dir, @dirs);

    $html = $pod;
    $html =~ s/\.(pod|pm)$/.html/g;

    # make sure the destination directories exist
    @dirs = split("/", $html);
    $dir  = "$htmldir/";
    for my $i ( 0 ..  (nelems @dirs)-2 ) {
	if (! -d "$dir@dirs[$i]") {
	    mkdir("$dir@dirs[$i]", 0755) ||
		die "$^PROGRAM_NAME: error creating directory $dir@dirs[$i]: $^OS_ERROR\n";
	}
	$dir .= "@dirs[$i]/";
    }

    # invoke pod2html
    print $^STDOUT, "$podroot/$pod => $htmldir/$html\n" if $verbose;
    Pod::Html::pod2html(
        "--htmldir=$htmldir",
	"--htmlroot=$htmlroot",
	"--podpath=".join(":", @podpath),
	"--podroot=$podroot", "--netscape",
	"--header",
	($doindex ?? "--index" !! "--noindex"),
	"--" . ($recurse ?? "" !! "no") . "recurse",
	(((nelems @libpods)-1) +>= 0) ?? "--libpods=" . join(":", @libpods) !! "",
	"--infile=$podroot/$pod", "--outfile=$htmldir/$html");
    die "$^PROGRAM_NAME: error running $pod2html: $^OS_ERROR\n" if $^CHILD_ERROR;
}
