chiark / gitweb /
makebs: work around build-tools 24.0.1 version bug
[fdroidserver.git] / docs / gendocs.sh
1
2
3 #!/bin/sh -e
4 # gendocs.sh -- generate a GNU manual in many formats.  This script is
5 #   mentioned in maintain.texi.  See the help message below for usage details.
6
7 scriptversion=2014-10-09.23
8
9 # Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
10 # Free Software Foundation, Inc.
11 #
12 # This program is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 #
25 # Original author: Mohit Agarwal.
26 # Send bug reports and any other correspondence to bug-texinfo@gnu.org.
27 #
28 # The latest version of this script, and the companion template, is
29 # available from Texinfo CVS:
30 # http://savannah.gnu.org/cgi-bin/viewcvs/texinfo/texinfo/util/gendocs.sh
31 # http://savannah.gnu.org/cgi-bin/viewcvs/texinfo/texinfo/util/gendocs_template
32 #
33 # An up-to-date copy is also maintained in Gnulib (gnu.org/software/gnulib).
34
35 # TODO:
36 # - image importation was only implemented for HTML generated by
37 #   makeinfo.  But it should be simple enough to adjust.
38 # - images are not imported in the source tarball.  All the needed
39 #   formats (PDF, PNG, etc.) should be included.
40
41 prog=`basename "$0"`
42 srcdir=`pwd`
43 timestamp=${SOURCE_DATE_EPOCH:-$(date +%s)}
44
45 scripturl="http://savannah.gnu.org/cgi-bin/viewcvs/~checkout~/texinfo/texinfo/util/gendocs.sh"
46 templateurl="http://savannah.gnu.org/cgi-bin/viewcvs/~checkout~/texinfo/texinfo/util/gendocs_template"
47
48 : ${SETLANG="env LANG= LC_MESSAGES= LC_ALL= LANGUAGE="}
49 : ${MAKEINFO="makeinfo"}
50 : ${TEXI2DVI="TEX=\"tex -output-comment=''\" texi2dvi --output-comment=\"\" -t @finalout"}
51 : ${DOCBOOK2HTML="docbook2html"}
52 : ${DOCBOOK2PDF="docbook2pdf"}
53 : ${DOCBOOK2TXT="docbook2txt"}
54 : ${GENDOCS_TEMPLATE_DIR="."}
55 : ${PERL='perl'}
56 : ${TEXI2HTML="texi2html"}
57 unset CDPATH
58 unset use_texi2html
59
60 version="gendocs.sh $scriptversion
61
62 Copyright 2013 Free Software Foundation, Inc.
63 There is NO warranty.  You may redistribute this software
64 under the terms of the GNU General Public License.
65 For more information about these matters, see the files named LICENSE."
66
67 usage="Usage: $prog [OPTION]... PACKAGE MANUAL-TITLE
68
69 Generate output in various formats from PACKAGE.texinfo (or .texi or
70 .txi) source.  See the GNU Maintainers document for a more extensive
71 discussion:
72   http://www.gnu.org/prep/maintain_toc.html
73
74 Options:
75   --email ADR use ADR as contact in generated web pages; always give this.
76
77   -s SRCFILE   read Texinfo from SRCFILE, instead of PACKAGE.{texinfo|texi|txi}
78   -o OUTDIR    write files into OUTDIR, instead of manual/.
79   -I DIR       append DIR to the Texinfo search path.
80   --common ARG pass ARG in all invocations.
81   --html ARG   pass ARG to makeinfo or texi2html for HTML targets.
82   --info ARG   pass ARG to makeinfo for Info, instead of --no-split.
83   --no-ascii   skip generating the plain text 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's directories.
146 htmlarg=
147 infoarg=--no-split
148 generate_ascii=true
149 outdir=manual
150 source_extra=
151 split=node
152 srcfile=
153
154 while test $# -gt 0; do
155   case $1 in
156     -s)          shift; srcfile=$1;;
157     -o)          shift; outdir=$1;;
158     -I)          shift; dirargs="$dirargs -I '$1'"; dirs="$dirs $1";;
159     --common)    shift; commonarg=$1;;
160     --docbook)   docbook=yes;;
161     --email)     shift; EMAIL=$1;;
162     --html)      shift; htmlarg=$1;;
163     --info)      shift; infoarg=$1;;
164     --no-ascii)  generate_ascii=false;;
165     --source)    shift; source_extra=$1;;
166     --split)     shift; split=$1;;
167     --texi2html) use_texi2html=1;;
168
169     --help)      echo "$usage"; exit 0;;
170     --version)   echo "$version"; exit 0;;
171     -*)
172       echo "$0: Unknown option \`$1'." >&2
173       echo "$0: Try \`--help' for more information." >&2
174       exit 1;;
175     *)
176       if test -z "$PACKAGE"; then
177         PACKAGE=$1
178       elif test -z "$MANUAL_TITLE"; then
179         MANUAL_TITLE=$1
180       else
181         echo "$0: extra non-option argument \`$1'." >&2
182         exit 1
183       fi;;
184   esac
185   shift
186 done
187
188 # makeinfo uses the dirargs, but texi2dvi doesn't.
189 commonarg=" $dirargs $commonarg"
190
191 # For most of the following, the base name is just $PACKAGE
192 base=$PACKAGE
193
194 if test -n "$srcfile"; then
195   # but here, we use the basename of $srcfile
196   base=`basename "$srcfile"`
197   case $base in
198     *.txi|*.texi|*.texinfo) base=`echo "$base"|sed 's/\.[texinfo]*$//'`;;
199   esac
200   PACKAGE=$base
201 elif test -s "$srcdir/$PACKAGE.texinfo"; then
202   srcfile=$srcdir/$PACKAGE.texinfo
203 elif test -s "$srcdir/$PACKAGE.texi"; then
204   srcfile=$srcdir/$PACKAGE.texi
205 elif test -s "$srcdir/$PACKAGE.txi"; then
206   srcfile=$srcdir/$PACKAGE.txi
207 else
208   echo "$0: cannot find .texinfo or .texi or .txi for $PACKAGE in $srcdir." >&2
209   exit 1
210 fi
211
212 if test ! -r $GENDOCS_TEMPLATE_DIR/gendocs_template; then
213   echo "$0: cannot read $GENDOCS_TEMPLATE_DIR/gendocs_template." >&2
214   echo "$0: it is available from $templateurl." >&2
215   exit 1
216 fi
217
218 # Function to return size of $1 in something resembling kilobytes.
219 calcsize()
220 {
221   size=`ls -ksl $1 | awk '{print $1}'`
222   echo $size
223 }
224
225 # copy_images OUTDIR HTML-FILE...
226 # -------------------------------
227 # Copy all the images needed by the HTML-FILEs into OUTDIR.  Look
228 # for them in the -I directories.
229 copy_images()
230 {
231   local odir
232   odir=$1
233   shift
234   $PERL -n -e "
235 BEGIN {
236   \$me = '$prog';
237   \$odir = '$odir';
238   @dirs = qw($dirs);
239 }
240 " -e '
241 /<img src="(.*?)"/g && ++$need{$1};
242
243 END {
244   #print "$me: @{[keys %need]}\n";  # for debugging, show images found.
245   FILE: for my $f (keys %need) {
246     for my $d (@dirs) {
247       if (-f "$d/$f") {
248         use File::Basename;
249         my $dest = dirname ("$odir/$f");
250         #
251         use File::Path;
252         -d $dest || mkpath ($dest)
253           || die "$me: cannot mkdir $dest: $!\n";
254         #
255         use File::Copy;
256         copy ("$d/$f", $dest)
257           || die "$me: cannot copy $d/$f to $dest: $!\n";
258         next FILE;
259       }
260     }
261     die "$me: $ARGV: cannot find image $f\n";
262   }
263 }
264 ' -- "$@" || exit 1
265 }
266
267 case $outdir in
268   /*) abs_outdir=$outdir;;
269   *)  abs_outdir=$srcdir/$outdir;;
270 esac
271
272 echo "Making output for $srcfile"
273 echo " in `pwd`"
274 mkdir -p "$outdir/"
275
276 taropts="--mtime=@$timestamp --mode=go=rX,u+rw,a-s"
277 cmd="$SETLANG $MAKEINFO -o $PACKAGE.info $commonarg $infoarg \"$srcfile\""
278 echo "Generating info... ($cmd)"
279 eval "$cmd"
280 tar $taropts --create $PACKAGE.info* | gzip --no-name -f -9 --to-stdout > "$outdir/$PACKAGE.info.tar.gz"
281 ls -l "$outdir/$PACKAGE.info.tar.gz"
282 info_tgz_size=`calcsize "$outdir/$PACKAGE.info.tar.gz"`
283 # do not mv the info files, there's no point in having them available
284 # separately on the web.
285
286 cmd="$SETLANG $TEXI2DVI $dirargs \"$srcfile\""
287 printf "\nGenerating dvi... ($cmd)\n"
288 eval "$cmd"
289 # compress/finish dvi:
290 gzip --no-name -f -9 $PACKAGE.dvi
291 dvi_gz_size=`calcsize $PACKAGE.dvi.gz`
292 mv $PACKAGE.dvi.gz "$outdir/"
293 ls -l "$outdir/$PACKAGE.dvi.gz"
294
295 cmd="$SETLANG $TEXI2DVI --pdf $dirargs \"$srcfile\""
296 printf "\nGenerating pdf... ($cmd)\n"
297 eval "$cmd"
298 pdf_size=`calcsize $PACKAGE.pdf`
299 mv $PACKAGE.pdf "$outdir/"
300 ls -l "$outdir/$PACKAGE.pdf"
301
302 if $generate_ascii; then
303   opt="-o $PACKAGE.txt --no-split --no-headers $commonarg"
304   cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
305   printf "\nGenerating ascii... ($cmd)\n"
306   eval "$cmd"
307   ascii_size=`calcsize $PACKAGE.txt`
308   gzip --no-name -f -9 -c $PACKAGE.txt >"$outdir/$PACKAGE.txt.gz"
309   ascii_gz_size=`calcsize "$outdir/$PACKAGE.txt.gz"`
310   mv $PACKAGE.txt "$outdir/"
311   ls -l "$outdir/$PACKAGE.txt" "$outdir/$PACKAGE.txt.gz"
312 fi
313
314 html_split()
315 {
316   opt="--split=$1 --node-files $commonarg $htmlarg"
317   cmd="$SETLANG $TEXI2HTML --output $PACKAGE.html $opt \"$srcfile\""
318   printf "\nGenerating html by $1... ($cmd)\n"
319   eval "$cmd"
320   split_html_dir=$PACKAGE.html
321   (
322     cd ${split_html_dir} || exit 1
323     ln -sf ${PACKAGE}.html index.html
324     tar $taropts --create -- *.html | gzip --no-name -f -9 --to-stdout > "$abs_outdir/${PACKAGE}.html_$1.tar.gz"
325   )
326   eval html_$1_tgz_size=`calcsize "$outdir/${PACKAGE}.html_$1.tar.gz"`
327   rm -f "$outdir"/html_$1/*.html
328   mkdir -p "$outdir/html_$1/"
329   mv ${split_html_dir}/*.html "$outdir/html_$1/"
330   rmdir ${split_html_dir}
331 }
332
333 if test -z "$use_texi2html"; then
334   opt="--no-split --html -o $PACKAGE.html $commonarg $htmlarg"
335   cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
336   printf "\nGenerating monolithic html... ($cmd)\n"
337   rm -rf $PACKAGE.html  # in case a directory is left over
338   eval "$cmd"
339   html_mono_size=`calcsize $PACKAGE.html`
340   gzip --no-name -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
341   html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
342   copy_images "$outdir/" $PACKAGE.html
343   mv $PACKAGE.html "$outdir/"
344   ls -l "$outdir/$PACKAGE.html" "$outdir/$PACKAGE.html.gz"
345
346   opt="--html -o $PACKAGE.html --split=$split $commonarg $htmlarg"
347   cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
348   printf "\nGenerating html by $split... ($cmd)\n"
349   eval "$cmd"
350   split_html_dir=$PACKAGE.html
351   copy_images $split_html_dir/ $split_html_dir/*.html
352   (
353     cd $split_html_dir || exit 1
354     tar $taropts --create -- * | gzip --no-name -f -9 --to-stdout > "$abs_outdir/$PACKAGE.html_$split.tar.gz"
355   )
356   eval \
357     html_${split}_tgz_size=`calcsize "$outdir/$PACKAGE.html_$split.tar.gz"`
358   rm -rf "$outdir/html_$split/"
359   mv $split_html_dir "$outdir/html_$split/"
360   du -s "$outdir/html_$split/"
361   ls -l "$outdir/$PACKAGE.html_$split.tar.gz"
362
363 else # use texi2html:
364   opt="--output $PACKAGE.html $commonarg $htmlarg"
365   cmd="$SETLANG $TEXI2HTML $opt \"$srcfile\""
366   printf "\nGenerating monolithic html with texi2html... ($cmd)\n"
367   rm -rf $PACKAGE.html  # in case a directory is left over
368   eval "$cmd"
369   html_mono_size=`calcsize $PACKAGE.html`
370   gzip --no-name -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
371   html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
372   mv $PACKAGE.html "$outdir/"
373
374   html_split node
375   html_split chapter
376   html_split section
377 fi
378
379 printf "\nMaking .tar.gz for sources...\n"
380 d=`dirname $srcfile`
381 (
382   cd "$d"
383   srcfiles=`ls -d *.texinfo *.texi *.txi *.eps $source_extra 2>/dev/null` || true
384   tar $taropts --create --dereference $srcfiles | gzip --no-name -f -9 --to-stdout > "$abs_outdir/$PACKAGE.texi.tar.gz"
385   ls -l "$abs_outdir/$PACKAGE.texi.tar.gz"
386 )
387 texi_tgz_size=`calcsize "$outdir/$PACKAGE.texi.tar.gz"`
388
389 if test -n "$docbook"; then
390   opt="-o - --docbook $commonarg"
391   cmd="$SETLANG $MAKEINFO $opt \"$srcfile\" >${srcdir}/$PACKAGE-db.xml"
392   printf "\nGenerating docbook XML... ($cmd)\n"
393   eval "$cmd"
394   docbook_xml_size=`calcsize $PACKAGE-db.xml`
395   gzip --no-name -f -9 -c $PACKAGE-db.xml >"$outdir/$PACKAGE-db.xml.gz"
396   docbook_xml_gz_size=`calcsize "$outdir/$PACKAGE-db.xml.gz"`
397   mv $PACKAGE-db.xml "$outdir/"
398
399   split_html_db_dir=html_node_db
400   opt="$commonarg -o $split_html_db_dir"
401   cmd="$DOCBOOK2HTML $opt \"${outdir}/$PACKAGE-db.xml\""
402   printf "\nGenerating docbook HTML... ($cmd)\n"
403   eval "$cmd"
404   (
405     cd ${split_html_db_dir} || exit 1
406     tar $taropts --create -- *.html | gzip --no-name -f -9 --to-stdout > "$abs_outdir/${PACKAGE}.html_node_db.tar.gz"
407   )
408   html_node_db_tgz_size=`calcsize "$outdir/${PACKAGE}.html_node_db.tar.gz"`
409   rm -f "$outdir"/html_node_db/*.html
410   mkdir -p "$outdir/html_node_db"
411   mv ${split_html_db_dir}/*.html "$outdir/html_node_db/"
412   rmdir ${split_html_db_dir}
413
414   cmd="$DOCBOOK2TXT \"${outdir}/$PACKAGE-db.xml\""
415   printf "\nGenerating docbook ASCII... ($cmd)\n"
416   eval "$cmd"
417   docbook_ascii_size=`calcsize $PACKAGE-db.txt`
418   mv $PACKAGE-db.txt "$outdir/"
419
420   cmd="$DOCBOOK2PDF \"${outdir}/$PACKAGE-db.xml\""
421   printf "\nGenerating docbook PDF... ($cmd)\n"
422   eval "$cmd"
423   docbook_pdf_size=`calcsize $PACKAGE-db.pdf`
424   mv $PACKAGE-db.pdf "$outdir/"
425 fi
426
427 printf "\nMaking index file...\n"
428 if test -z "$use_texi2html"; then
429   CONDS="/%%IF  *HTML_SECTION%%/,/%%ENDIF  *HTML_SECTION%%/d;\
430          /%%IF  *HTML_CHAPTER%%/,/%%ENDIF  *HTML_CHAPTER%%/d"
431 else
432   # should take account of --split here.
433   CONDS="/%%ENDIF.*%%/d;/%%IF  *HTML_SECTION%%/d;/%%IF  *HTML_CHAPTER%%/d"
434 fi
435
436 curdate=`$SETLANG date -u '+%B %d, %Y' -d @$timestamp`
437 sed \
438    -e "s!%%TITLE%%!$MANUAL_TITLE!g" \
439    -e "s!%%EMAIL%%!$EMAIL!g" \
440    -e "s!%%PACKAGE%%!$PACKAGE!g" \
441    -e "s!%%DATE%%!$curdate!g" \
442    -e "s!%%HTML_MONO_SIZE%%!$html_mono_size!g" \
443    -e "s!%%HTML_MONO_GZ_SIZE%%!$html_mono_gz_size!g" \
444    -e "s!%%HTML_NODE_TGZ_SIZE%%!$html_node_tgz_size!g" \
445    -e "s!%%HTML_SECTION_TGZ_SIZE%%!$html_section_tgz_size!g" \
446    -e "s!%%HTML_CHAPTER_TGZ_SIZE%%!$html_chapter_tgz_size!g" \
447    -e "s!%%INFO_TGZ_SIZE%%!$info_tgz_size!g" \
448    -e "s!%%DVI_GZ_SIZE%%!$dvi_gz_size!g" \
449    -e "s!%%PDF_SIZE%%!$pdf_size!g" \
450    -e "s!%%ASCII_SIZE%%!$ascii_size!g" \
451    -e "s!%%ASCII_GZ_SIZE%%!$ascii_gz_size!g" \
452    -e "s!%%TEXI_TGZ_SIZE%%!$texi_tgz_size!g" \
453    -e "s!%%DOCBOOK_HTML_NODE_TGZ_SIZE%%!$html_node_db_tgz_size!g" \
454    -e "s!%%DOCBOOK_ASCII_SIZE%%!$docbook_ascii_size!g" \
455    -e "s!%%DOCBOOK_PDF_SIZE%%!$docbook_pdf_size!g" \
456    -e "s!%%DOCBOOK_XML_SIZE%%!$docbook_xml_size!g" \
457    -e "s!%%DOCBOOK_XML_GZ_SIZE%%!$docbook_xml_gz_size!g" \
458    -e "s,%%SCRIPTURL%%,$scripturl,g" \
459    -e "s!%%SCRIPTNAME%%!$prog!g" \
460    -e "$CONDS" \
461 $GENDOCS_TEMPLATE_DIR/gendocs_template >"$outdir/index.html"
462
463 echo "Done, see $outdir/ subdirectory for new files."
464
465 # Local variables:
466 # eval: (add-hook 'write-file-hooks 'time-stamp)
467 # time-stamp-start: "scriptversion="
468 # time-stamp-format: "%:y-%02m-%02d.%02H"
469 # time-stamp-end: "$"
470 # End: