#  Copyright (c) 1997-2018
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-----------------------------------------------------------------------------
#
#  Complete production cycle for automatic part of documentation
#

my ($noHTML, $noExt, $snapshot, $link_resources);

if ( !GetOptions( noHTML => \$noHTML, noExt => \$noExt, snapshot => \$snapshot,
                  'link-resources' => \$link_resources ) or
     @ARGV > 1 ||
     !@ARGV && !$DeveloperMode) {
   my $outdirvar = $DeveloperMode ? '[OutDir]' : 'OutDir';
   die <<".";
usage: polymake [--ignore-config] --script generate_docs [--noHTML --noExt --snapshot --link-resources] $outdirvar

  OutDir    top directory where all created documentation files are placed
            It must be specified when running in an installed polymake;
            in a developer's workspace, the default location is build/doc.

  --noHTML  stop after created XML files with documentation skeleton,
            do not produce final HTML pages

  --noExt   do not produce separate pages describing single extensions
            Note: features defined in extensions will still appear on the common pages;
            to suppress them, please add --ignore-config polymake option.
            Bundled extensions can't be suppressed without reconfiguration of polymake.

  --snapshot  include the current GIT branch information instead of the polymake version.

  --link-resources  add symlinks to the resource files (CSS, icons) in OutDir even when no HTML is produced
.
}

my $Wiki="http://polymake.org/";
my $docdir="$InstallTop/xml/documentation";

my $outdir=$ARGV[0] || Cwd::abs_path("$InstallArch/../doc");
if (-d $outdir) {
   # wipe out everything if this looks like the old documentation
   if (not grep { not -d $_ ? m{/(?:inc|images|PTL)$} : m{\.(?:html|css|js)$} } glob("$outdir/*")
         and
       -f "$outdir/inc/core.inc" && -f "$outdir/inc/doc_index.inc"
         and
       !glob("$outdir/*.html") || (-f "$outdir/core.html" && -f "$outdir/doc_index.html")) {
      File::Path::remove_tree($outdir, { keep_root=>1 });
   } elsif (glob("$outdir/*")) {
      die "output directory $outdir contains unexpected files; please wipe it out or specify a different location\n";
   }
} else {
   File::Path::make_path($outdir);
}

my @Apps=sorted_uniq(sort( map { /$filename_re/o } map { glob("$_/apps/*") } $InstallTop, @Core::Extension::active[$Core::Extension::num_bundled..$#Core::Extension::active] ));

my $tmpdir=new Tempdir;

script("extract_docs", '--wiki' => $Wiki, '--outsuffix' => '.html', '--outdir' => $tmpdir, @Apps);

my $pmdocns=doc_namespace();

my $writer=open_doc_file("version.xml");
$writer->startTag([ $pmdocns, "version" ]);
if ($DeveloperMode && $snapshot) {
   my ($rev)= `git rev-parse --short HEAD`;
   $writer->dataElement("commit", $rev);
} else {
   $writer->dataElement("release", $Version);
}

$writer->startTag("extensions");
for my $ext (@Core::Extension::active) {
  $writer->startTag("extension");
  if (!$ext->is_bundled) {
     $writer->dataElement("non_bundled", 1);
  }
  $writer->dataElement("URI", $ext->URI);
  unless ($noExt) {
     $writer->dataElement("name", $ext->short_name) if ($ext->short_name);
     $writer->dataElement("text", $ext->credit->text) if ($ext->credit);
     my $ext_file = $ext->URI;
     $ext_file =~ s|http://||og;
     $ext_file =~ s|[/:.]|_|og;
     $writer->dataElement("file", $ext_file);
  }
  $writer->endTag;
}
$writer->endTag;

$writer->endTag;

close_doc_file($writer);

sub run_xsltproc {
   my ($out, $stylesheet, $in, $ext_name)=@_;
   if (system("xsltproc", "--path", "$tmpdir $docdir/entities",
                          "--nonet",
                          "--stringparam", "Wiki", $Wiki,
                          "--stringparam", "ext_name", $ext_name,
                          "-o", $out,
                          "$docdir/$stylesheet.xsl", "$in")) {
      die "Command 'xsltproc @_' returned with error code $?\n",
          "Temporary files preserved in $tmpdir\n";
   }
}

push(@Apps, "core") if -f "$tmpdir/core.xml";
foreach (@Apps) {
   run_xsltproc("$outdir/inc/$_.inc", "doc", "$_.xml");
}

$writer=open_doc_file("applications.xml");
$writer->startTag([ $pmdocns, "applications" ]);
$writer->emptyTag("file", name => "$_.xml") for @Apps;
$writer->endTag;
close_doc_file($writer);

run_xsltproc("$outdir/inc/index.inc",     "index",     "applications.xml");
run_xsltproc("$tmpdir/all.xml",           "all",       "applications.xml");
run_xsltproc("$outdir/inc/doc_index.inc", "doc_index", "all.xml");

unless ($noExt) {
   foreach (@Core::Extension::active) {
      my $ext_name = $_->URI;
      my $ext_file = $_->URI;
      $ext_file =~ s|http://||og;
      $ext_file =~ s|[/:.]|_|og;
      run_xsltproc("$tmpdir/$ext_file.xml",     "extension", "all.xml", $ext_name);
      run_xsltproc("$outdir/inc/$ext_file.inc", "doc",       "$ext_file.xml", $ext_name);
      run_xsltproc("$outdir/$ext_file.html",    "export",    "$outdir/inc/$ext_file.inc") unless ($noHTML);
   }
   run_xsltproc("$outdir/inc/extensions.inc",  "ext_list",   "applications.xml");
   run_xsltproc("$outdir/extensions.html",     "export",     "$outdir/inc/extensions.inc") unless ($noHTML);
}

unless ($noHTML) {
   foreach (@Apps) {
      run_xsltproc("$outdir/$_.html", "export", "$outdir/inc/$_.inc");
   }
   run_xsltproc("$outdir/index.html", "export", "$outdir/inc/index.inc");
   run_xsltproc("$outdir/doc_index.html", "export", "$outdir/inc/doc_index.inc");
}

if ($link_resources || !$noHTML) {
   symlink "$docdir/images", "$outdir/images";
   foreach my $file (glob("$docdir/*.{js,css}")) {
      symlink $file, "$outdir/".substr($file, length($docdir)+1);
   }
}


# Local Variables:
# mode: perl
# cperl-indent-level: 3
# indent-tabs-mode:nil
# End:
