#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::RealBin/../blib/lib", $FindBin::RealBin;
use LaTeXML::Util::Pathname;
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,
  "webdir=s"  => \$WEBDEST,
  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;

$WEBDEST = pathname_absolute($WEBDEST);

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);
$classes =~ s/(_)/\\_/gs;
my $packages = join(', ', grep { $_ } map { s/\.sty$// && $_ } @bindings);
$packages =~ s/(_)/\\_/gs;

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", "--splitat=subsection",
      "--sourcedir=$MANDIR",
      "--urlstyle=server",
      "--sitedirectory=$WEBDEST",
      "--css=$DOCDIR/latexmldoc.css",
      "--javascript=LaTeXML-maybeMathjax.js",
      ($validate ? () : ("--novalidate")),
      "--icon=$DOCDIR/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)
  --webdir          Directory to put website at. Defaults to
                    '/local/www/site/htdocs/LaTeXML'
  --help            Shows this help.

=cut
