1#!/bin/sh -e
2# gendocs.sh -- generate a GNU manual in many formats.  This script is
3#   mentioned in maintain.texi.  See the help message below for usage details.
4
5scriptversion=2015-01-02.22
6
7# Copyright 2003-2015 Free Software Foundation, Inc.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22# Original author: Mohit Agarwal.
23# Send bug reports and any other correspondence to bug-gnulib@gnu.org.
24#
25# The latest version of this script, and the companion template, is
26# available from the Gnulib repository:
27#
28# http://git.savannah.gnu.org/cgit/gnulib.git/tree/build-aux/gendocs.sh
29# http://git.savannah.gnu.org/cgit/gnulib.git/tree/doc/gendocs_template
30
31# TODO:
32# - image importation was only implemented for HTML generated by
33#   makeinfo.  But it should be simple enough to adjust.
34# - images are not imported in the source tarball.  All the needed
35#   formats (PDF, PNG, etc.) should be included.
36
37prog=`basename "$0"`
38srcdir=`pwd`
39
40scripturl="http://git.savannah.gnu.org/cgit/gnulib.git/plain/build-aux/gendocs.sh"
41templateurl="http://git.savannah.gnu.org/cgit/gnulib.git/plain/doc/gendocs_template"
42
43: ${SETLANG="env LANG= LC_MESSAGES= LC_ALL= LANGUAGE="}
44: ${MAKEINFO="makeinfo"}
45: ${TEXI2DVI="texi2dvi -t @finalout"}
46: ${DOCBOOK2HTML="docbook2html"}
47: ${DOCBOOK2PDF="docbook2pdf"}
48: ${DOCBOOK2TXT="docbook2txt"}
49: ${GENDOCS_TEMPLATE_DIR="."}
50: ${PERL='perl'}
51: ${TEXI2HTML="texi2html"}
52unset CDPATH
53unset use_texi2html
54
55version="gendocs.sh $scriptversion
56
57Copyright 2013 Free Software Foundation, Inc.
58There is NO warranty.  You may redistribute this software
59under the terms of the GNU General Public License.
60For more information about these matters, see the files named COPYING."
61
62usage="Usage: $prog [OPTION]... PACKAGE MANUAL-TITLE
63
64Generate output in various formats from PACKAGE.texinfo (or .texi or
65.txi) source.  See the GNU Maintainers document for a more extensive
66discussion:
67  http://www.gnu.org/prep/maintain_toc.html
68
69Options:
70  --email ADR use ADR as contact in generated web pages; always give this.
71
72  -s SRCFILE   read Texinfo from SRCFILE, instead of PACKAGE.{texinfo|texi|txi}
73  -o OUTDIR    write files into OUTDIR, instead of manual/.
74  -I DIR       append DIR to the Texinfo search path.
75  --common ARG pass ARG in all invocations.
76  --html ARG   pass ARG to makeinfo or texi2html for HTML targets.
77  --info ARG   pass ARG to makeinfo for Info, instead of --no-split.
78  --no-ascii   skip generating the plain text output.
79  --no-html    skip generating the html output.
80  --no-info    skip generating the info output.
81  --no-tex     skip generating the dvi and pdf output.
82  --source ARG include ARG in tar archive of sources.
83  --split HOW  make split HTML by node, section, chapter; default node.
84
85  --texi2html  use texi2html to make HTML target, with all split versions.
86  --docbook    convert through DocBook too (xml, txt, html, pdf).
87
88  --help       display this help and exit successfully.
89  --version    display version information and exit successfully.
90
91Simple example: $prog --email bug-gnu-emacs@gnu.org emacs \"GNU Emacs Manual\"
92
93Typical sequence:
94  cd PACKAGESOURCE/doc
95  wget \"$scripturl\"
96  wget \"$templateurl\"
97  $prog --email BUGLIST MANUAL \"GNU MANUAL - One-line description\"
98
99Output will be in a new subdirectory \"manual\" (by default;
100use -o OUTDIR to override).  Move all the new files into your web CVS
101tree, as explained in the Web Pages node of maintain.texi.
102
103Please use the --email ADDRESS option so your own bug-reporting
104address will be used in the generated HTML pages.
105
106MANUAL-TITLE is included as part of the HTML <title> of the overall
107manual/index.html file.  It should include the name of the package being
108documented.  manual/index.html is created by substitution from the file
109$GENDOCS_TEMPLATE_DIR/gendocs_template.  (Feel free to modify the
110generic template for your own purposes.)
111
112If you have several manuals, you'll need to run this script several
113times with different MANUAL values, specifying a different output
114directory with -o each time.  Then write (by hand) an overall index.html
115with links to them all.
116
117If a manual's Texinfo sources are spread across several directories,
118first copy or symlink all Texinfo sources into a single directory.
119(Part of the script's work is to make a tar.gz of the sources.)
120
121As implied above, by default monolithic Info files are generated.
122If you want split Info, or other Info options, use --info to override.
123
124You can set the environment variables MAKEINFO, TEXI2DVI, TEXI2HTML,
125and PERL to control the programs that get executed, and
126GENDOCS_TEMPLATE_DIR to control where the gendocs_template file is
127looked for.  With --docbook, the environment variables DOCBOOK2HTML,
128DOCBOOK2PDF, and DOCBOOK2TXT are also consulted.
129
130By default, makeinfo and texi2dvi are run in the default (English)
131locale, since that's the language of most Texinfo manuals.  If you
132happen to have a non-English manual and non-English web site, see the
133SETLANG setting in the source.
134
135Email bug reports or enhancement requests to bug-gnulib@gnu.org.
136"
137
138MANUAL_TITLE=
139PACKAGE=
140EMAIL=webmasters@gnu.org  # please override with --email
141commonarg= # passed to all makeinfo/texi2html invcations.
142dirargs=   # passed to all tools (-I dir).
143dirs=      # -I directories.
144htmlarg=
145infoarg=--no-split
146generate_ascii=true
147generate_html=true
148generate_info=true
149generate_tex=true
150outdir=manual
151source_extra=
152split=node
153srcfile=
154
155while test $# -gt 0; do
156  case $1 in
157    -s)          shift; srcfile=$1;;
158    -o)          shift; outdir=$1;;
159    -I)          shift; dirargs="$dirargs -I '$1'"; dirs="$dirs $1";;
160    --common)    shift; commonarg=$1;;
161    --docbook)   docbook=yes;;
162    --email)     shift; EMAIL=$1;;
163    --html)      shift; htmlarg=$1;;
164    --info)      shift; infoarg=$1;;
165    --no-ascii)  generate_ascii=false;;
166    --no-html)   generate_ascii=false;;
167    --no-info)   generate_info=false;;
168    --no-tex)    generate_tex=false;;
169    --source)    shift; source_extra=$1;;
170    --split)     shift; split=$1;;
171    --texi2html) use_texi2html=1;;
172
173    --help)      echo "$usage"; exit 0;;
174    --version)   echo "$version"; exit 0;;
175    -*)
176      echo "$0: Unknown option \`$1'." >&2
177      echo "$0: Try \`--help' for more information." >&2
178      exit 1;;
179    *)
180      if test -z "$PACKAGE"; then
181        PACKAGE=$1
182      elif test -z "$MANUAL_TITLE"; then
183        MANUAL_TITLE=$1
184      else
185        echo "$0: extra non-option argument \`$1'." >&2
186        exit 1
187      fi;;
188  esac
189  shift
190done
191
192# makeinfo uses the dirargs, but texi2dvi doesn't.
193commonarg=" $dirargs $commonarg"
194
195# For most of the following, the base name is just $PACKAGE
196base=$PACKAGE
197
198if test -n "$srcfile"; then
199  # but here, we use the basename of $srcfile
200  base=`basename "$srcfile"`
201  case $base in
202    *.txi|*.texi|*.texinfo) base=`echo "$base"|sed 's/\.[texinfo]*$//'`;;
203  esac
204  PACKAGE=$base
205elif test -s "$srcdir/$PACKAGE.texinfo"; then
206  srcfile=$srcdir/$PACKAGE.texinfo
207elif test -s "$srcdir/$PACKAGE.texi"; then
208  srcfile=$srcdir/$PACKAGE.texi
209elif test -s "$srcdir/$PACKAGE.txi"; then
210  srcfile=$srcdir/$PACKAGE.txi
211else
212  echo "$0: cannot find .texinfo or .texi or .txi for $PACKAGE in $srcdir." >&2
213  exit 1
214fi
215
216if test ! -r $GENDOCS_TEMPLATE_DIR/gendocs_template; then
217  echo "$0: cannot read $GENDOCS_TEMPLATE_DIR/gendocs_template." >&2
218  echo "$0: it is available from $templateurl." >&2
219  exit 1
220fi
221
222# Function to return size of $1 in something resembling kilobytes.
223calcsize()
224{
225  size=`ls -ksl $1 | awk '{print $1}'`
226  echo $size
227}
228
229# copy_images OUTDIR HTML-FILE...
230# -------------------------------
231# Copy all the images needed by the HTML-FILEs into OUTDIR.
232# Look for them in . and the -I directories; this is simpler than what
233# makeinfo supports with -I, but hopefully it will suffice.
234copy_images()
235{
236  local odir
237  odir=$1
238  shift
239  $PERL -n -e "
240BEGIN {
241  \$me = '$prog';
242  \$odir = '$odir';
243  @dirs = qw(. $dirs);
244}
245" -e '
246/<img src="(.*?)"/g && ++$need{$1};
247
248END {
249  #print "$me: @{[keys %need]}\n";  # for debugging, show images found.
250  FILE: for my $f (keys %need) {
251    for my $d (@dirs) {
252      if (-f "$d/$f") {
253        use File::Basename;
254        my $dest = dirname ("$odir/$f");
255        #
256        use File::Path;
257        -d $dest || mkpath ($dest)
258          || die "$me: cannot mkdir $dest: $!\n";
259        #
260        use File::Copy;
261        copy ("$d/$f", $dest)
262          || die "$me: cannot copy $d/$f to $dest: $!\n";
263        next FILE;
264      }
265    }
266    die "$me: $ARGV: cannot find image $f\n";
267  }
268}
269' -- "$@" || exit 1
270}
271
272case $outdir in
273  /*) abs_outdir=$outdir;;
274  *)  abs_outdir=$srcdir/$outdir;;
275esac
276
277echo "Making output for $srcfile"
278echo " in `pwd`"
279mkdir -p "$outdir/"
280
281#
282if $generate_info; then
283  cmd="$SETLANG $MAKEINFO -o $PACKAGE.info $commonarg $infoarg \"$srcfile\""
284  echo "Generating info... ($cmd)"
285  rm -f $PACKAGE.info* # get rid of any strays
286  eval "$cmd"
287  tar czf "$outdir/$PACKAGE.info.tar.gz" $PACKAGE.info*
288  ls -l "$outdir/$PACKAGE.info.tar.gz"
289  info_tgz_size=`calcsize "$outdir/$PACKAGE.info.tar.gz"`
290  # do not mv the info files, there's no point in having them available
291  # separately on the web.
292fi  # end info
293
294#
295if $generate_tex; then
296  cmd="$SETLANG $TEXI2DVI $dirargs \"$srcfile\""
297  printf "\nGenerating dvi... ($cmd)\n"
298  eval "$cmd"
299  # compress/finish dvi:
300  gzip -f -9 $PACKAGE.dvi
301  dvi_gz_size=`calcsize $PACKAGE.dvi.gz`
302  mv $PACKAGE.dvi.gz "$outdir/"
303  ls -l "$outdir/$PACKAGE.dvi.gz"
304
305  cmd="$SETLANG $TEXI2DVI --pdf $dirargs \"$srcfile\""
306  printf "\nGenerating pdf... ($cmd)\n"
307  eval "$cmd"
308  pdf_size=`calcsize $PACKAGE.pdf`
309  mv $PACKAGE.pdf "$outdir/"
310  ls -l "$outdir/$PACKAGE.pdf"
311fi # end tex (dvi + pdf)
312
313#
314if $generate_ascii; then
315  opt="-o $PACKAGE.txt --no-split --no-headers $commonarg"
316  cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
317  printf "\nGenerating ascii... ($cmd)\n"
318  eval "$cmd"
319  ascii_size=`calcsize $PACKAGE.txt`
320  gzip -f -9 -c $PACKAGE.txt >"$outdir/$PACKAGE.txt.gz"
321  ascii_gz_size=`calcsize "$outdir/$PACKAGE.txt.gz"`
322  mv $PACKAGE.txt "$outdir/"
323  ls -l "$outdir/$PACKAGE.txt" "$outdir/$PACKAGE.txt.gz"
324fi
325
326#
327
328if $generate_html; then
329# Split HTML at level $1.  Used for texi2html.
330html_split()
331{
332  opt="--split=$1 --node-files $commonarg $htmlarg"
333  cmd="$SETLANG $TEXI2HTML --output $PACKAGE.html $opt \"$srcfile\""
334  printf "\nGenerating html by $1... ($cmd)\n"
335  eval "$cmd"
336  split_html_dir=$PACKAGE.html
337  (
338    cd ${split_html_dir} || exit 1
339    ln -sf ${PACKAGE}.html index.html
340    tar -czf "$abs_outdir/${PACKAGE}.html_$1.tar.gz" -- *.html
341  )
342  eval html_$1_tgz_size=`calcsize "$outdir/${PACKAGE}.html_$1.tar.gz"`
343  rm -f "$outdir"/html_$1/*.html
344  mkdir -p "$outdir/html_$1/"
345  mv ${split_html_dir}/*.html "$outdir/html_$1/"
346  rmdir ${split_html_dir}
347}
348
349if test -z "$use_texi2html"; then
350  opt="--no-split --html -o $PACKAGE.html $commonarg $htmlarg"
351  cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
352  printf "\nGenerating monolithic html... ($cmd)\n"
353  rm -rf $PACKAGE.html  # in case a directory is left over
354  eval "$cmd"
355  html_mono_size=`calcsize $PACKAGE.html`
356  gzip -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
357  html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
358  copy_images "$outdir/" $PACKAGE.html
359  mv $PACKAGE.html "$outdir/"
360  ls -l "$outdir/$PACKAGE.html" "$outdir/$PACKAGE.html.gz"
361
362  # Before Texinfo 5.0, makeinfo did not accept a --split=HOW option,
363  # it just always split by node.  So if we're splitting by node anyway,
364  # leave it out.
365  if test "x$split" = xnode; then
366    split_arg=
367  else
368    split_arg=--split=$split
369  fi
370  #
371  opt="--html -o $PACKAGE.html $split_arg $commonarg $htmlarg"
372  cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
373  printf "\nGenerating html by $split... ($cmd)\n"
374  eval "$cmd"
375  split_html_dir=$PACKAGE.html
376  copy_images $split_html_dir/ $split_html_dir/*.html
377  (
378    cd $split_html_dir || exit 1
379    tar -czf "$abs_outdir/$PACKAGE.html_$split.tar.gz" -- *
380  )
381  eval \
382    html_${split}_tgz_size=`calcsize "$outdir/$PACKAGE.html_$split.tar.gz"`
383  rm -rf "$outdir/html_$split/"
384  mv $split_html_dir "$outdir/html_$split/"
385  du -s "$outdir/html_$split/"
386  ls -l "$outdir/$PACKAGE.html_$split.tar.gz"
387
388else # use texi2html:
389  opt="--output $PACKAGE.html $commonarg $htmlarg"
390  cmd="$SETLANG $TEXI2HTML $opt \"$srcfile\""
391  printf "\nGenerating monolithic html with texi2html... ($cmd)\n"
392  rm -rf $PACKAGE.html  # in case a directory is left over
393  eval "$cmd"
394  html_mono_size=`calcsize $PACKAGE.html`
395  gzip -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
396  html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
397  mv $PACKAGE.html "$outdir/"
398
399  html_split node
400  html_split chapter
401  html_split section
402fi
403fi # end html
404
405#
406printf "\nMaking .tar.gz for sources...\n"
407d=`dirname $srcfile`
408(
409  cd "$d"
410  srcfiles=`ls -d *.texinfo *.texi *.txi *.eps $source_extra 2>/dev/null` || true
411  tar czfh "$abs_outdir/$PACKAGE.texi.tar.gz" $srcfiles
412  ls -l "$abs_outdir/$PACKAGE.texi.tar.gz"
413)
414texi_tgz_size=`calcsize "$outdir/$PACKAGE.texi.tar.gz"`
415
416#
417# Do everything again through docbook.
418if test -n "$docbook"; then
419  opt="-o - --docbook $commonarg"
420  cmd="$SETLANG $MAKEINFO $opt \"$srcfile\" >${srcdir}/$PACKAGE-db.xml"
421  printf "\nGenerating docbook XML... ($cmd)\n"
422  eval "$cmd"
423  docbook_xml_size=`calcsize $PACKAGE-db.xml`
424  gzip -f -9 -c $PACKAGE-db.xml >"$outdir/$PACKAGE-db.xml.gz"
425  docbook_xml_gz_size=`calcsize "$outdir/$PACKAGE-db.xml.gz"`
426  mv $PACKAGE-db.xml "$outdir/"
427
428  split_html_db_dir=html_node_db
429  opt="$commonarg -o $split_html_db_dir"
430  cmd="$DOCBOOK2HTML $opt \"${outdir}/$PACKAGE-db.xml\""
431  printf "\nGenerating docbook HTML... ($cmd)\n"
432  eval "$cmd"
433  (
434    cd ${split_html_db_dir} || exit 1
435    tar -czf "$abs_outdir/${PACKAGE}.html_node_db.tar.gz" -- *.html
436  )
437  html_node_db_tgz_size=`calcsize "$outdir/${PACKAGE}.html_node_db.tar.gz"`
438  rm -f "$outdir"/html_node_db/*.html
439  mkdir -p "$outdir/html_node_db"
440  mv ${split_html_db_dir}/*.html "$outdir/html_node_db/"
441  rmdir ${split_html_db_dir}
442
443  cmd="$DOCBOOK2TXT \"${outdir}/$PACKAGE-db.xml\""
444  printf "\nGenerating docbook ASCII... ($cmd)\n"
445  eval "$cmd"
446  docbook_ascii_size=`calcsize $PACKAGE-db.txt`
447  mv $PACKAGE-db.txt "$outdir/"
448
449  cmd="$DOCBOOK2PDF \"${outdir}/$PACKAGE-db.xml\""
450  printf "\nGenerating docbook PDF... ($cmd)\n"
451  eval "$cmd"
452  docbook_pdf_size=`calcsize $PACKAGE-db.pdf`
453  mv $PACKAGE-db.pdf "$outdir/"
454fi
455
456#
457printf "\nMaking index.html for $PACKAGE...\n"
458if test -z "$use_texi2html"; then
459  CONDS="/%%IF  *HTML_SECTION%%/,/%%ENDIF  *HTML_SECTION%%/d;\
460         /%%IF  *HTML_CHAPTER%%/,/%%ENDIF  *HTML_CHAPTER%%/d"
461else
462  # should take account of --split here.
463  CONDS="/%%ENDIF.*%%/d;/%%IF  *HTML_SECTION%%/d;/%%IF  *HTML_CHAPTER%%/d"
464fi
465
466curdate=`$SETLANG date '+%B %d, %Y'`
467sed \
468   -e "s!%%TITLE%%!$MANUAL_TITLE!g" \
469   -e "s!%%EMAIL%%!$EMAIL!g" \
470   -e "s!%%PACKAGE%%!$PACKAGE!g" \
471   -e "s!%%DATE%%!$curdate!g" \
472   -e "s!%%HTML_MONO_SIZE%%!$html_mono_size!g" \
473   -e "s!%%HTML_MONO_GZ_SIZE%%!$html_mono_gz_size!g" \
474   -e "s!%%HTML_NODE_TGZ_SIZE%%!$html_node_tgz_size!g" \
475   -e "s!%%HTML_SECTION_TGZ_SIZE%%!$html_section_tgz_size!g" \
476   -e "s!%%HTML_CHAPTER_TGZ_SIZE%%!$html_chapter_tgz_size!g" \
477   -e "s!%%INFO_TGZ_SIZE%%!$info_tgz_size!g" \
478   -e "s!%%DVI_GZ_SIZE%%!$dvi_gz_size!g" \
479   -e "s!%%PDF_SIZE%%!$pdf_size!g" \
480   -e "s!%%ASCII_SIZE%%!$ascii_size!g" \
481   -e "s!%%ASCII_GZ_SIZE%%!$ascii_gz_size!g" \
482   -e "s!%%TEXI_TGZ_SIZE%%!$texi_tgz_size!g" \
483   -e "s!%%DOCBOOK_HTML_NODE_TGZ_SIZE%%!$html_node_db_tgz_size!g" \
484   -e "s!%%DOCBOOK_ASCII_SIZE%%!$docbook_ascii_size!g" \
485   -e "s!%%DOCBOOK_PDF_SIZE%%!$docbook_pdf_size!g" \
486   -e "s!%%DOCBOOK_XML_SIZE%%!$docbook_xml_size!g" \
487   -e "s!%%DOCBOOK_XML_GZ_SIZE%%!$docbook_xml_gz_size!g" \
488   -e "s,%%SCRIPTURL%%,$scripturl,g" \
489   -e "s!%%SCRIPTNAME%%!$prog!g" \
490   -e "$CONDS" \
491$GENDOCS_TEMPLATE_DIR/gendocs_template >"$outdir/index.html"
492
493echo "Done, see $outdir/ subdirectory for new files."
494
495# Local variables:
496# eval: (add-hook 'write-file-hooks 'time-stamp)
497# time-stamp-start: "scriptversion="
498# time-stamp-format: "%:y-%02m-%02d.%02H"
499# time-stamp-end: "$"
500# End:
501