#!/usr/bin/perl -w
use strict;
use warnings;
use FindBin;
use lib "$FindBin::RealBin/../blib/lib", $FindBin::RealBin;
use Carp;
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;
use MakeTools;

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Assume this script is in LaTeXML's doc directory!
my $DOCDIR = $FindBin::RealBin;
# Use latexml from blib!
my $LATEXMLDIR = "$DOCDIR/..";
$ENV{PATH}      = "$LATEXMLDIR/blib/script:$ENV{PATH}";
$ENV{TEXINPUTS} = "$LATEXMLDIR/blib/lib/LaTeXML/texmf"
  . ($ENV{TEXINPUTS} ? ":" . $ENV{TEXINPUTS} : '')
  . "::";
my $MANDIR  = "$DOCDIR/manual";
my $WEBDEST = "/local/www/site/htdocs/LaTeXML";

my $identity = "makemanual (part of LaTeXML)";
my ($force, $help, $validate, $verbosity) = (0, 0, 1, 0);
my ($dopdf, $dohtml) = (undef, undef);
GetOptions("force!" => \$force,
  "help"      => \$help,
  "pdf!"      => \$dopdf,
  "html!"     => \$dohtml,
  "xhtml!"    => \$dohtml,
  "validate!" => \$validate,
  verbose     => sub { $verbosity++; },
  quiet       => sub { $verbosity--; },
) or pod2usage(-message => $identity, -exitval => 1, -verbose => 0, -output => \*STDERR);
pod2usage(-message => $identity, -exitval => 1, -verbose => 2, -output => \*STDOUT) if $help;

BEGIN { $SIG{__DIE__} = \&confess; }

if ((!defined $dopdf) && (!defined $dohtml)) {
  $dopdf = $dohtml = 1; }
elsif (!defined $dopdf) {
  $dopdf = 1 if defined $dohtml && !$dohtml; }
elsif (!defined $dohtml) {
  $dohtml = 1 if defined $dopdf && !$dopdf; }

#======================================================================
# Generate Appendices from PODS, Schema, etc
#======================================================================
setVerbosity($verbosity);

heading("Extracting Appendices");
system("$MANDIR/genpods" . ($force ? " --force" : "")) == 0
  or warn "Failed to generate pods?";
system("$MANDIR/genschema" . ($force ? " --force" : "")) == 0
  or warn "Failed to generate schema documentation?";
my $version;
{ use LaTeXML;
  $version = $LaTeXML::VERSION; }

my $DIR;
opendir($DIR, "$LATEXMLDIR/lib/LaTeXML/Package") or die "Couldn't read Packages: $!";
my @bindings = sort grep { !/(?:_support\.|_core\.|\#)/ } grep { $_ }
  map { s/.ltxml$// && $_ } readdir($DIR);
closedir($DIR);

my $classes  = join(', ', grep { $_ } map { s/\.cls$// && $_ } @bindings);
my $packages = join(', ', grep { $_ } map { s/\.sty$// && $_ } @bindings);

my $texversion = $version;
$texversion =~ s/\_/\\_/g;
saveData("$MANDIR/release.tex",
  "\\def\\CurrentVersion{$texversion}\n"
    . "\\def\\CurrentClasses{$classes}\n"
    . "\\def\\CurrentPackages{$packages}\n");

#======================================================================
# Now generate the manual
#======================================================================
if ($dopdf) {
  heading("Generating pdf of manual");
  pdflatex("$MANDIR/manual.tex",
    dependencies => ["$DOCDIR/sty/latexmldoc.sty", "$MANDIR/release.tex",
      "$MANDIR/schema.tex", "$MANDIR/pods"],
    indexoptions => [],
    force        => $force);
  copy("$MANDIR/manual.pdf", "$LATEXMLDIR/manual.pdf");
  copy("$MANDIR/manual.pdf", "$WEBDEST/manual.pdf");
}

if ($dohtml) {
  heading("Generating html of manual");
  latexml("$MANDIR/manual.tex" => "$WEBDEST/manual/index.html",
    dependencies => ["$DOCDIR/sty/latexmldoc.sty.ltxml", "$MANDIR/release.tex",
      "$MANDIR/schema.tex", "$MANDIR/pods"],
    postoptions => [
      "--format=html5",
      "--split", "--splitnaming=labelrelative",
      "--sourcedir=$MANDIR",
      "--urlstyle=server",
      "--sitedirectory=$WEBDEST",
      "--css=../latexmldoc.css",
      "--javascript=../LaTeXML-maybeMathjax.js",
      ($validate ? () : ("--novalidate")),
      "--icon=../favicon.ico"],
    force => $force);
  # but if only css needs updating...
  copy("$MANDIR/../latexmldoc.css", "$WEBDEST/latexmldoc.css");
}
#======================================================================

__END__

=head1 NAME

C<makemanual> - generate manual for LaTeXML

=head1 SYNOPSIS

makemanual [options]

 Options:
  --force           Force regeneration (default: only if needed)
  --help            Shows this help.

=cut
