#!/bin/sh -e
# gendocs.sh -- generate a GNU manual in many formats.  This script is
#   mentioned in maintain.texi.  See the help message below for usage details.

scriptversion=2022-01-01.00

# Copyright 2003-2022 Free Software Foundation, Inc.
#
# 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 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# Original author: Mohit Agarwal.
# Send bug reports and any other correspondence to bug-gnulib@gnu.org.
#
# The latest version of this script, and the companion template, is
# available from the Gnulib repository:
#
# https://git.savannah.gnu.org/cgit/gnulib.git/tree/build-aux/gendocs.sh
# https://git.savannah.gnu.org/cgit/gnulib.git/tree/doc/gendocs_template

# TODO:
# - image importing was only implemented for HTML generated by
#   makeinfo.  But it should be simple enough to adjust.
# - images are not imported in the source tarball.  All the needed
#   formats (PDF, PNG, etc.) should be included.

prog=`basename "$0"`
srcdir=`pwd`

scripturl="https://git.savannah.gnu.org/cgit/gnulib.git/plain/build-aux/gendocs.sh"
templateurl="https://git.savannah.gnu.org/cgit/gnulib.git/plain/doc/gendocs_template"

: ${SETLANG="env LANG= LC_MESSAGES= LC_ALL= LANGUAGE="}
: ${MAKEINFO="makeinfo"}
: ${TEXI2DVI="texi2dvi"}
: ${DOCBOOK2HTML="docbook2html"}
: ${DOCBOOK2PDF="docbook2pdf"}
: ${DOCBOOK2TXT="docbook2txt"}
: ${GENDOCS_TEMPLATE_DIR="."}
: ${PERL='perl'}
: ${TEXI2HTML="texi2html"}
unset CDPATH
unset use_texi2html

MANUAL_TITLE=
PACKAGE=
EMAIL=webmasters@gnu.org  # please override with --email
commonarg= # passed to all makeinfo/texi2html invcations.
dirargs=   # passed to all tools (-I dir).
dirs=      # -I directories.
htmlarg="--css-ref=https://www.gnu.org/software/gnulib/manual.css -c TOP_NODE_UP_URL=/manual"
default_htmlarg=true
infoarg=--no-split
generate_ascii=true
generate_html=true
generate_info=true
generate_tex=true
outdir=manual
source_extra=
split=node
srcfile=
texarg="-t @finalout"

version="gendocs.sh $scriptversion

Copyright 2022 Free Software Foundation, Inc.
There is NO warranty.  You may redistribute this software
under the terms of the GNU General Public License.
For more information about these matters, see the files named COPYING."

usage="Usage: $prog [OPTION]... PACKAGE MANUAL-TITLE

Generate output in various formats from PACKAGE.texinfo (or .texi or
.txi) source.  See the GNU Maintainers document for a more extensive
discussion:
  https://www.gnu.org/prep/maintain_toc.html

Options:
  --email ADR use ADR as contact in generated web pages; always give this.

  -s SRCFILE   read Texinfo from SRCFILE, instead of PACKAGE.{texinfo|texi|txi}
  -o OUTDIR    write files into OUTDIR, instead of manual/.
  -I DIR       append DIR to the Texinfo search path.
  --common ARG pass ARG in all invocations.
  --html ARG   pass ARG to makeinfo or texi2html for HTML targets,
                 instead of '$htmlarg'.
  --info ARG   pass ARG to makeinfo for Info, instead of --no-split.
  --no-ascii   skip generating the plain text output.
  --no-html    skip generating the html output.
  --no-info    skip generating the info output.
  --no-tex     skip generating the dvi and pdf output.
  --source ARG include ARG in tar archive of sources.
  --split HOW  make split HTML by node, section, chapter; default node.
  --tex ARG    pass ARG to texi2dvi for DVI and PDF, instead of -t @finalout.

  --texi2html  use texi2html to make HTML target, with all split versions.
  --docbook    convert through DocBook too (xml, txt, html, pdf).

  --help       display this help and exit successfully.
  --version    display version information and exit successfully.

Simple example: $prog --email bug-gnu-emacs@gnu.org emacs \"GNU Emacs Manual\"

Typical sequence:
  cd PACKAGESOURCE/doc
  wget \"$scripturl\"
  wget \"$templateurl\"
  $prog --email BUGLIST MANUAL \"GNU MANUAL - One-line description\"

Output will be in a new subdirectory \"manual\" (by default;
use -o OUTDIR to override).  Move all the new files into your web CVS
tree, as explained in the Web Pages node of maintain.texi.

Please use the --email ADDRESS option so your own bug-reporting
address will be used in the generated HTML pages.

MANUAL-TITLE is included as part of the HTML <title> of the overall
manual/index.html file.  It should include the name of the package being
documented.  manual/index.html is created by substitution from the file
$GENDOCS_TEMPLATE_DIR/gendocs_template.  (Feel free to modify the
generic template for your own purposes.)

If you have several manuals, you'll need to run this script several
times with different MANUAL values, specifying a different output
directory with -o each time.  Then write (by hand) an overall index.html
with links to them all.

If a manual's Texinfo sources are spread across several directories,
first copy or symlink all Texinfo sources into a single directory.
(Part of the script's work is to make a tar.gz of the sources.)

As implied above, by default monolithic Info files are generated.
If you want split Info, or other Info options, use --info to override.

You can set the environment variables MAKEINFO, TEXI2DVI, TEXI2HTML,
and PERL to control the programs that get executed, and
GENDOCS_TEMPLATE_DIR to control where the gendocs_template file is
looked for.  With --docbook, the environment variables DOCBOOK2HTML,
DOCBOOK2PDF, and DOCBOOK2TXT are also consulted.

By default, makeinfo and texi2dvi are run in the default (English)
locale, since that's the language of most Texinfo manuals.  If you
happen to have a non-English manual and non-English web site, see the
SETLANG setting in the source.

Email bug reports or enhancement requests to bug-gnulib@gnu.org.
"

while test $# -gt 0; do
  case $1 in
    -s)          shift; srcfile=$1;;
    -o)          shift; outdir=$1;;
    -I)          shift; dirargs="$dirargs -I '$1'"; dirs="$dirs $1";;
    --common)    shift; commonarg=$1;;
    --docbook)   docbook=yes;;
    --email)     shift; EMAIL=$1;;
    --html)      shift; default_htmlarg=false; htmlarg=$1;;
    --info)      shift; infoarg=$1;;
    --no-ascii)  generate_ascii=false;;
    --no-html)   generate_ascii=false;;
    --no-info)   generate_info=false;;
    --no-tex)    generate_tex=false;;
    --source)    shift; source_extra=$1;;
    --split)     shift; split=$1;;
    --tex)       shift; texarg=$1;;
    --texi2html) use_texi2html=1;;

    --help)      echo "$usage"; exit 0;;
    --version)   echo "$version"; exit 0;;
    -*)
      echo "$0: Unknown option \`$1'." >&2
      echo "$0: Try \`--help' for more information." >&2
      exit 1;;
    *)
      if test -z "$PACKAGE"; then
        PACKAGE=$1
      elif test -z "$MANUAL_TITLE"; then
        MANUAL_TITLE=$1
      else
        echo "$0: extra non-option argument \`$1'." >&2
        exit 1
      fi;;
  esac
  shift
done

# makeinfo uses the dirargs, but texi2dvi doesn't.
commonarg=" $dirargs $commonarg"

# For most of the following, the base name is just $PACKAGE
base=$PACKAGE

if $default_htmlarg && test -n "$use_texi2html"; then
  # The legacy texi2html doesn't support TOP_NODE_UP_URL
  htmlarg="--css-ref=https://www.gnu.org/software/gnulib/manual.css"
fi

if test -n "$srcfile"; then
  # but here, we use the basename of $srcfile
  base=`basename "$srcfile"`
  case $base in
    *.txi|*.texi|*.texinfo) base=`echo "$base"|sed 's/\.[texinfo]*$//'`;;
  esac
  PACKAGE=$base
elif test -s "$srcdir/$PACKAGE.texinfo"; then
  srcfile=$srcdir/$PACKAGE.texinfo
elif test -s "$srcdir/$PACKAGE.texi"; then
  srcfile=$srcdir/$PACKAGE.texi
elif test -s "$srcdir/$PACKAGE.txi"; then
  srcfile=$srcdir/$PACKAGE.txi
else
  echo "$0: cannot find .texinfo or .texi or .txi for $PACKAGE in $srcdir." >&2
  exit 1
fi

if test ! -r $GENDOCS_TEMPLATE_DIR/gendocs_template; then
  echo "$0: cannot read $GENDOCS_TEMPLATE_DIR/gendocs_template." >&2
  echo "$0: it is available from $templateurl." >&2
  exit 1
fi

# Function to return size of $1 in something resembling kilobytes.
calcsize()
{
  size=`ls -ksl $1 | awk '{print $1}'`
  echo $size
}

# copy_images OUTDIR HTML-FILE...
# -------------------------------
# Copy all the images needed by the HTML-FILEs into OUTDIR.
# Look for them in . and the -I directories; this is simpler than what
# makeinfo supports with -I, but hopefully it will suffice.
copy_images()
{
  local odir
  odir=$1
  shift
  $PERL -n -e "
BEGIN {
  \$me = '$prog';
  \$odir = '$odir';
  @dirs = qw(. $dirs);
}
" -e '
/<img src="(.*?)"/g && ++$need{$1};

END {
  #print "$me: @{[keys %need]}\n";  # for debugging, show images found.
  FILE: for my $f (keys %need) {
    for my $d (@dirs) {
      if (-f "$d/$f") {
        use File::Basename;
        my $dest = dirname ("$odir/$f");
        #
        use File::Path;
        -d $dest || mkpath ($dest)
          || die "$me: cannot mkdir $dest: $!\n";
        #
        use File::Copy;
        copy ("$d/$f", $dest)
          || die "$me: cannot copy $d/$f to $dest: $!\n";
        next FILE;
      }
    }
    die "$me: $ARGV: cannot find image $f\n";
  }
}
' -- "$@" || exit 1
}

case $outdir in
  /*) abs_outdir=$outdir;;
  *)  abs_outdir=$srcdir/$outdir;;
esac

echo "Making output for $srcfile"
echo " in `pwd`"
mkdir -p "$outdir/"

# 