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