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