autoupdate
[gnulib.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=2012-10-23.16
6
7 # Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 prog=`basename "$0"`
34 srcdir=`pwd`
35
36 scripturl="http://savannah.gnu.org/cgi-bin/viewcvs/~checkout~/texinfo/texinfo/util/gendocs.sh"
37 templateurl="http://savannah.gnu.org/cgi-bin/viewcvs/~checkout~/texinfo/texinfo/util/gendocs_template"
38
39 : ${SETLANG="env LANG= LC_MESSAGES= LC_ALL= LANGUAGE="}
40 : ${MAKEINFO="makeinfo"}
41 : ${TEXI2DVI="texi2dvi -t @finalout"}
42 : ${DVIPS="dvips"}
43 : ${DOCBOOK2HTML="docbook2html"}
44 : ${DOCBOOK2PDF="docbook2pdf"}
45 : ${DOCBOOK2PS="docbook2ps"}
46 : ${DOCBOOK2TXT="docbook2txt"}
47 : ${GENDOCS_TEMPLATE_DIR="."}
48 : ${TEXI2HTML="texi2html"}
49 unset CDPATH
50 unset use_texi2html
51
52 version="gendocs.sh $scriptversion
53
54 Copyright 2012 Free Software Foundation, Inc.
55 There is NO warranty.  You may redistribute this software
56 under the terms of the GNU General Public License.
57 For more information about these matters, see the files named COPYING."
58
59 usage="Usage: $prog [OPTION]... PACKAGE MANUAL-TITLE
60
61 Generate output in various formats from PACKAGE.texinfo (or .texi or
62 .txi) source.  See the GNU Maintainers document for a more extensive
63 discussion:
64   http://www.gnu.org/prep/maintain_toc.html
65
66 Options:
67   -s SRCFILE  read Texinfo from SRCFILE, instead of PACKAGE.{texinfo|texi|txi}
68   -o OUTDIR   write files into OUTDIR, instead of manual/.
69   -I DIR      append DIR to the Texinfo search path.
70   --email ADR use ADR as contact in generated web pages.
71   --docbook   convert to DocBook too (xml, txt, html, pdf and ps).
72   --html ARG  pass indicated ARG to makeinfo or texi2html for HTML targets.
73   --info ARG  pass indicated ARG to makeinfo for Info, instead of --no-split.
74   --texi2html use texi2html to generate HTML targets.
75   --help      display this help and exit successfully.
76   --version   display version information and exit successfully.
77
78 Simple example: $prog --email bug-gnu-emacs@gnu.org emacs \"GNU Emacs Manual\"
79
80 Typical sequence:
81   cd PACKAGESOURCE/doc
82   wget \"$scripturl\"
83   wget \"$templateurl\"
84   $prog --email BUGLIST MANUAL \"GNU MANUAL - One-line description\"
85
86 Output will be in a new subdirectory \"manual\" (by default;
87 use -o OUTDIR to override).  Move all the new files into your web CVS
88 tree, as explained in the Web Pages node of maintain.texi.
89
90 Please do use the --email ADDRESS option to specify your bug-reporting
91 address in the generated HTML pages.
92
93 MANUAL-TITLE is included as part of the HTML <title> of the overall
94 manual/index.html file.  It should include the name of the package being
95 documented.  manual/index.html is created by substitution from the file
96 $GENDOCS_TEMPLATE_DIR/gendocs_template.  (Feel free to modify the
97 generic template for your own purposes.)
98
99 If you have several manuals, you'll need to run this script several
100 times with different MANUAL values, specifying a different output
101 directory with -o each time.  Then write (by hand) an overall index.html
102 with links to them all.
103
104 If a manual's Texinfo sources are spread across several directories,
105 first copy or symlink all Texinfo sources into a single directory.
106 (Part of the script's work is to make a tar.gz of the sources.)
107
108 As implied above, by default monolithic Info files are generated.
109 If you want split Info, or other Info options, use --info to override.
110
111 You can set the environment variables MAKEINFO, TEXI2DVI, TEXI2HTML, and
112 DVIPS to control the programs that get executed, and
113 GENDOCS_TEMPLATE_DIR to control where the gendocs_template file is
114 looked for.  With --docbook, the environment variables DOCBOOK2HTML,
115 DOCBOOK2PDF, DOCBOOK2PS, and DOCBOOK2TXT are also respected.
116
117 By default, makeinfo and texi2dvi are run in the default (English)
118 locale, since that's the language of most Texinfo manuals.  If you
119 happen to have a non-English manual and non-English web site, see the
120 SETLANG setting in the source.
121
122 Email bug reports or enhancement requests to bug-texinfo@gnu.org.
123 "
124
125 calcsize()
126 {
127   size=`ls -ksl $1 | awk '{print $1}'`
128   echo $size
129 }
130
131 MANUAL_TITLE=
132 PACKAGE=
133 EMAIL=webmasters@gnu.org  # please override with --email
134 commonarg= # Options passed to all the tools (-I dir).
135 htmlarg=
136 infoarg=--no-split
137 outdir=manual
138 srcfile=
139
140 while test $# -gt 0; do
141   case $1 in
142     --email) shift; EMAIL=$1;;
143     --help) echo "$usage"; exit 0;;
144     --version) echo "$version"; exit 0;;
145     -s) shift; srcfile=$1;;
146     -o) shift; outdir=$1;;
147     -I) shift; commonarg="$commonarg -I '$1'";;
148     --docbook) docbook=yes;;
149     --html) shift; htmlarg=$1;;
150     --info) shift; infoarg=$1;;
151     --texi2html) use_texi2html=1;;
152     -*)
153       echo "$0: Unknown option \`$1'." >&2
154       echo "$0: Try \`--help' for more information." >&2
155       exit 1;;
156     *)
157       if test -z "$PACKAGE"; then
158         PACKAGE=$1
159       elif test -z "$MANUAL_TITLE"; then
160         MANUAL_TITLE=$1
161       else
162         echo "$0: extra non-option argument \`$1'." >&2
163         exit 1
164       fi;;
165   esac
166   shift
167 done
168
169 # For most of the following, the base name is just $PACKAGE
170 base=$PACKAGE
171
172 if test -n "$srcfile"; then
173   # but here, we use the basename of $srcfile
174   base=`basename "$srcfile"`
175   case $base in
176     *.txi|*.texi|*.texinfo) base=`echo "$base"|sed 's/\.[texinfo]*$//'`;;
177   esac
178   PACKAGE=$base
179 elif test -s "$srcdir/$PACKAGE.texinfo"; then
180   srcfile=$srcdir/$PACKAGE.texinfo
181 elif test -s "$srcdir/$PACKAGE.texi"; then
182   srcfile=$srcdir/$PACKAGE.texi
183 elif test -s "$srcdir/$PACKAGE.txi"; then
184   srcfile=$srcdir/$PACKAGE.txi
185 else
186   echo "$0: cannot find .texinfo or .texi or .txi for $PACKAGE in $srcdir." >&2
187   exit 1
188 fi
189
190 if test ! -r $GENDOCS_TEMPLATE_DIR/gendocs_template; then
191   echo "$0: cannot read $GENDOCS_TEMPLATE_DIR/gendocs_template." >&2
192   echo "$0: it is available from $templateurl." >&2
193   exit 1
194 fi
195
196 case $outdir in
197   /*) abs_outdir=$outdir;;
198   *)  abs_outdir=$srcdir/$outdir;;
199 esac
200
201 echo "Generating output formats for $srcfile"
202
203 cmd="$SETLANG $MAKEINFO -o $PACKAGE.info $commonarg $infoarg \"$srcfile\""
204 echo "Generating info file(s)... ($cmd)"
205 eval "$cmd"
206 mkdir -p "$outdir/"
207 tar czf "$outdir/$PACKAGE.info.tar.gz" $PACKAGE.info*
208 info_tgz_size=`calcsize "$outdir/$PACKAGE.info.tar.gz"`
209 # do not mv the info files, there's no point in having them available
210 # separately on the web.
211
212 cmd="$SETLANG $TEXI2DVI $commonarg \"$srcfile\""
213 echo "Generating dvi ... ($cmd)"
214 eval "$cmd"
215
216 # now, before we compress dvi:
217 echo "Generating postscript..."
218 $DVIPS $PACKAGE -o
219 gzip -f -9 $PACKAGE.ps
220 ps_gz_size=`calcsize $PACKAGE.ps.gz`
221 mv $PACKAGE.ps.gz "$outdir/"
222
223 # compress/finish dvi:
224 gzip -f -9 $PACKAGE.dvi
225 dvi_gz_size=`calcsize $PACKAGE.dvi.gz`
226 mv $PACKAGE.dvi.gz "$outdir/"
227
228 cmd="$SETLANG $TEXI2DVI --pdf $commonarg \"$srcfile\""
229 echo "Generating pdf ... ($cmd)"
230 eval "$cmd"
231 pdf_size=`calcsize $PACKAGE.pdf`
232 mv $PACKAGE.pdf "$outdir/"
233
234 opt="-o $PACKAGE.txt --no-split --no-headers $commonarg"
235 cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
236 echo "Generating ASCII... ($cmd)"
237 eval "$cmd"
238 ascii_size=`calcsize $PACKAGE.txt`
239 gzip -f -9 -c $PACKAGE.txt >"$outdir/$PACKAGE.txt.gz"
240 ascii_gz_size=`calcsize "$outdir/$PACKAGE.txt.gz"`
241 mv $PACKAGE.txt "$outdir/"
242
243 html_split()
244 {
245   opt="--split=$1 $commonarg $htmlarg --node-files"
246   cmd="$SETLANG $TEXI2HTML --output $PACKAGE.html $opt \"$srcfile\""
247   echo "Generating html by $1... ($cmd)"
248   eval "$cmd"
249   split_html_dir=$PACKAGE.html
250   (
251     cd ${split_html_dir} || exit 1
252     ln -sf ${PACKAGE}.html index.html
253     tar -czf "$abs_outdir/${PACKAGE}.html_$1.tar.gz" -- *.html
254   )
255   eval html_$1_tgz_size=`calcsize "$outdir/${PACKAGE}.html_$1.tar.gz"`
256   rm -f "$outdir"/html_$1/*.html
257   mkdir -p "$outdir/html_$1/"
258   mv ${split_html_dir}/*.html "$outdir/html_$1/"
259   rmdir ${split_html_dir}
260 }
261
262 if test -z "$use_texi2html"; then
263   opt="--no-split --html -o $PACKAGE.html $commonarg $htmlarg"
264   cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
265   echo "Generating monolithic html... ($cmd)"
266   rm -rf $PACKAGE.html  # in case a directory is left over
267   eval "$cmd"
268   html_mono_size=`calcsize $PACKAGE.html`
269   gzip -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
270   html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
271   mv $PACKAGE.html "$outdir/"
272
273   opt="--html -o $PACKAGE.html $commonarg $htmlarg"
274   cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
275   echo "Generating html by node... ($cmd)"
276   eval "$cmd"
277   split_html_dir=$PACKAGE.html
278   (
279    cd ${split_html_dir} || exit 1
280    tar -czf "$abs_outdir/${PACKAGE}.html_node.tar.gz" -- *.html
281   )
282   html_node_tgz_size=`calcsize "$outdir/${PACKAGE}.html_node.tar.gz"`
283   rm -f "$outdir"/html_node/*.html
284   mkdir -p "$outdir/html_node/"
285   mv ${split_html_dir}/*.html "$outdir/html_node/"
286   rmdir ${split_html_dir}
287 else
288   opt="--output $PACKAGE.html $commonarg $htmlarg"
289   cmd="$SETLANG $TEXI2HTML $opt \"$srcfile\""
290   echo "Generating monolithic html... ($cmd)"
291   rm -rf $PACKAGE.html  # in case a directory is left over
292   eval "$cmd"
293   html_mono_size=`calcsize $PACKAGE.html`
294   gzip -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
295   html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
296   mv $PACKAGE.html "$outdir/"
297
298   html_split node
299   html_split chapter
300   html_split section
301 fi
302
303 echo Making .tar.gz for sources...
304 d=`dirname $srcfile`
305 (
306   cd "$d"
307   srcfiles=`ls *.texinfo *.texi *.txi *.eps 2>/dev/null` || true
308   tar cvzfh "$abs_outdir/$PACKAGE.texi.tar.gz" $srcfiles
309 )
310 texi_tgz_size=`calcsize "$outdir/$PACKAGE.texi.tar.gz"`
311
312 if test -n "$docbook"; then
313   opt="-o - --docbook $commonarg"
314   cmd="$SETLANG $MAKEINFO $opt \"$srcfile\" > ${srcdir}/$PACKAGE-db.xml"
315   echo "Generating docbook XML... ($cmd)"
316   eval "$cmd"
317   docbook_xml_size=`calcsize $PACKAGE-db.xml`
318   gzip -f -9 -c $PACKAGE-db.xml >"$outdir/$PACKAGE-db.xml.gz"
319   docbook_xml_gz_size=`calcsize "$outdir/$PACKAGE-db.xml.gz"`
320   mv $PACKAGE-db.xml "$outdir/"
321
322   split_html_db_dir=html_node_db
323   opt="$commonarg -o $split_html_db_dir"
324   cmd="$DOCBOOK2HTML $opt \"${outdir}/$PACKAGE-db.xml\""
325   echo "Generating docbook HTML... ($cmd)"
326   eval "$cmd"
327   (
328     cd ${split_html_db_dir} || exit 1
329     tar -czf "$abs_outdir/${PACKAGE}.html_node_db.tar.gz" -- *.html
330   )
331   html_node_db_tgz_size=`calcsize "$outdir/${PACKAGE}.html_node_db.tar.gz"`
332   rm -f "$outdir"/html_node_db/*.html
333   mkdir -p "$outdir/html_node_db"
334   mv ${split_html_db_dir}/*.html "$outdir/html_node_db/"
335   rmdir ${split_html_db_dir}
336
337   cmd="$DOCBOOK2TXT \"${outdir}/$PACKAGE-db.xml\""
338   echo "Generating docbook ASCII... ($cmd)"
339   eval "$cmd"
340   docbook_ascii_size=`calcsize $PACKAGE-db.txt`
341   mv $PACKAGE-db.txt "$outdir/"
342
343   cmd="$DOCBOOK2PS \"${outdir}/$PACKAGE-db.xml\""
344   echo "Generating docbook PS... ($cmd)"
345   eval "$cmd"
346   gzip -f -9 -c $PACKAGE-db.ps >"$outdir/$PACKAGE-db.ps.gz"
347   docbook_ps_gz_size=`calcsize "$outdir/$PACKAGE-db.ps.gz"`
348   mv $PACKAGE-db.ps "$outdir/"
349
350   cmd="$DOCBOOK2PDF \"${outdir}/$PACKAGE-db.xml\""
351   echo "Generating docbook PDF... ($cmd)"
352   eval "$cmd"
353   docbook_pdf_size=`calcsize $PACKAGE-db.pdf`
354   mv $PACKAGE-db.pdf "$outdir/"
355 fi
356
357 echo "Writing index file..."
358 if test -z "$use_texi2html"; then
359    CONDS="/%%IF  *HTML_SECTION%%/,/%%ENDIF  *HTML_SECTION%%/d;\
360           /%%IF  *HTML_CHAPTER%%/,/%%ENDIF  *HTML_CHAPTER%%/d"
361 else
362    CONDS="/%%ENDIF.*%%/d;/%%IF  *HTML_SECTION%%/d;/%%IF  *HTML_CHAPTER%%/d"
363 fi
364 curdate=`$SETLANG date '+%B %d, %Y'`
365 sed \
366    -e "s!%%TITLE%%!$MANUAL_TITLE!g" \
367    -e "s!%%EMAIL%%!$EMAIL!g" \
368    -e "s!%%PACKAGE%%!$PACKAGE!g" \
369    -e "s!%%DATE%%!$curdate!g" \
370    -e "s!%%HTML_MONO_SIZE%%!$html_mono_size!g" \
371    -e "s!%%HTML_MONO_GZ_SIZE%%!$html_mono_gz_size!g" \
372    -e "s!%%HTML_NODE_TGZ_SIZE%%!$html_node_tgz_size!g" \
373    -e "s!%%HTML_SECTION_TGZ_SIZE%%!$html_section_tgz_size!g" \
374    -e "s!%%HTML_CHAPTER_TGZ_SIZE%%!$html_chapter_tgz_size!g" \
375    -e "s!%%INFO_TGZ_SIZE%%!$info_tgz_size!g" \
376    -e "s!%%DVI_GZ_SIZE%%!$dvi_gz_size!g" \
377    -e "s!%%PDF_SIZE%%!$pdf_size!g" \
378    -e "s!%%PS_GZ_SIZE%%!$ps_gz_size!g" \
379    -e "s!%%ASCII_SIZE%%!$ascii_size!g" \
380    -e "s!%%ASCII_GZ_SIZE%%!$ascii_gz_size!g" \
381    -e "s!%%TEXI_TGZ_SIZE%%!$texi_tgz_size!g" \
382    -e "s!%%DOCBOOK_HTML_NODE_TGZ_SIZE%%!$html_node_db_tgz_size!g" \
383    -e "s!%%DOCBOOK_ASCII_SIZE%%!$docbook_ascii_size!g" \
384    -e "s!%%DOCBOOK_PS_GZ_SIZE%%!$docbook_ps_gz_size!g" \
385    -e "s!%%DOCBOOK_PDF_SIZE%%!$docbook_pdf_size!g" \
386    -e "s!%%DOCBOOK_XML_SIZE%%!$docbook_xml_size!g" \
387    -e "s!%%DOCBOOK_XML_GZ_SIZE%%!$docbook_xml_gz_size!g" \
388    -e "s,%%SCRIPTURL%%,$scripturl,g" \
389    -e "s!%%SCRIPTNAME%%!$prog!g" \
390    -e "$CONDS" \
391 $GENDOCS_TEMPLATE_DIR/gendocs_template >"$outdir/index.html"
392
393 echo "Done, see $outdir/ subdirectory for new files."
394
395 # Local variables:
396 # eval: (add-hook 'write-file-hooks 'time-stamp)
397 # time-stamp-start: "scriptversion="
398 # time-stamp-format: "%:y-%02m-%02d.%02H"
399 # time-stamp-end: "$"
400 # End: