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