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