Clarify portability and main program.
[python/dscho.git] / Doc / tools / mkhowto.sh
blobcd85ef078c263d19f54df36fd5c75f5042329aa3
1 #! /usr/bin/env ksh
3 # This script may be invoked by naming it directly or via a shell alias,
4 # but NOT through a symbolic link. Perhaps a future version will allow
5 # the use of a symbolic link.
7 # Using a symbolic link will cause the script to not be able to locate
8 # its support files.
10 MYDIR=`dirname $0`
11 WORKDIR=`pwd`
12 cd $MYDIR
13 MYDIR=`pwd`
14 cd ..
15 TOPDIR=`pwd`
16 cd $WORKDIR
18 # DEFAULT_FORMAT must be upper case...
19 DEFAULT_FORMAT=PDF
20 USE_DEFAULT_FORMAT=true
21 DISCARD_TEMPS=true
23 ICONSERVER=''
25 TEMPBASE=mkhowto-$LOGNAME-$$
27 L2H_INIT_FILE=$TOPDIR/perl/l2hinit.perl
28 L2H_AUX_INIT_FILE=/usr/tmp/$TEMPBASE.perl
30 LOGFILE=/usr/tmp/$TEMPBASE.how
31 LOGGING=''
33 usage() {
34 MYNAME=`basename $0`
35 cat <<EOF
36 usage: $MYNAME [options...] file ...
38 Options specifying formats to build:
39 --html HyperText Markup Language
40 --pdf Portable Document Format (default)
41 --ps PostScript
42 --dvi "DeVice Indepentent" format from TeX
43 --text ASCII text (requires lynx)
45 More than one output format may be specified, or --all.
47 HTML options:
48 --address, -a Specify an address for page footers.
49 --link Specify the number of levels to include on each page.
50 --split, -s Specify a section level for page splitting, default: $MAX_SPLIT_DEPTH.
51 --iconserver, -i Specify location of icons (default: ../).
53 Other options:
54 --a4 Format for A4 paper.
55 --letter Format for US letter paper (the default).
56 --help, -H Show this text.
57 --logging, -l Log stdout and stderr to a file (*.how).
58 --debugging, -D Echo commands as they are executed.
59 --keep, -k Keep temporary files around.
60 --quiet, -q Do not print command output to stdout.
61 (stderr is also lost, sorry; see *.how for errors)
63 EOF
64 if [ "$2" ] ; then
65 echo "$2"
66 echo
68 exit $1
71 # These are LaTeX2HTML controls; they reflect l2h variables of the same name.
72 # The values here are the defaults after modification by perl/l2hinit.perl.
74 ADDRESS=''
75 MAX_LINK_DEPTH=3
76 MAX_SPLIT_DEPTH=6
78 build_html() {
79 TEXFILE=`kpsewhich $1.tex`
80 TEXFILE="${TEXFILE#./}"
81 BUILDDIR=${2:-$1}
82 latex2html \
83 -init_file $L2H_INIT_FILE \
84 -init_file $L2H_AUX_INIT_FILE \
85 -dir $BUILDDIR $TEXFILE || exit $?
86 if [ "$MAX_SPLIT_DEPTH" -ne 1 ] ; then
87 (cd $BUILDDIR; $MYDIR/node2label.pl *.html) || exit $?
91 use_latex() {
92 # two args: <file> <latextype>
93 MYFILE=$1
94 MYLATEX=$2
96 # We really have to do it three times to get all the page numbers right,
97 # since the length of the ToC makes a real difference.
99 $MYDIR/newind.py >$MYFILE.ind
100 $MYDIR/newind.py modindex >mod$MYFILE.ind
101 $MYLATEX $MYFILE || exit $?
102 if [ -f mod$MYFILE.idx ] ; then
103 makeindex mod$MYFILE.idx
105 if [ -f $MYFILE.idx ] ; then
106 $MYDIR/fix_hack $MYFILE.idx
107 makeindex $MYFILE.idx
108 $MYDIR/indfix.py $MYFILE.ind
110 if [ `grep -c '^\\\\bibdata{' $MYFILE.aux` -ne 0 ] ; then
111 USE_BIBTEX=true
112 bibtex $MYFILE
113 else
114 USE_BIBTEX=''
116 if [ -f $MYFILE.syn ] ; then
117 # This hack is due to a bug with the module synopsis support that
118 # causes the last module synopsis to be written out twice in
119 # howto documents (not present for manuals). Repeated below.
120 uniq $MYFILE.syn >TEMP.syn && mv TEMP.syn $MYFILE.syn || exit $?
122 $MYLATEX $MYFILE || exit $?
123 if [ -f mod$MYFILE.idx ] ; then
124 makeindex mod$MYFILE.idx
126 if [ -f $MYFILE.idx ] ; then
127 $MYDIR/fix_hack $MYFILE.idx || exit $?
128 makeindex -s $TOPDIR/texinputs/python.ist $MYFILE.idx || exit $?
130 if [ -f $MYFILE.toc -a $MYLATEX = pdflatex ] ; then
131 $MYDIR/toc2bkm.py -c section $MYFILE
133 if [ -f $MYFILE.syn ] ; then
134 uniq $MYFILE.syn >TEMP.syn && mv TEMP.syn $MYFILE.syn || exit $?
136 if [ "$USE_BIBTEX" ] ; then
137 bibtex $MYFILE
139 $MYLATEX $MYFILE || exit $?
142 build_dvi() {
143 use_latex $1 latex
146 build_pdf() {
147 use_latex $1 pdflatex
150 build_ps() {
151 dvips -N0 -o $1.ps $1 || exit $?
154 build_text() {
155 lynx -nolist -dump $2/index.html >$1.txt
158 l2hoption() {
159 if [ "$2" ] ; then
160 echo "\$$1 = \"$2\";" >>$L2H_AUX_INIT_FILE
164 cleanup() {
165 rm -f $1.aux $1.log $1.out $1.toc $1.bkm $1.idx $1.ilg $1.ind $1.syn
166 rm -f mod$1.idx mod$1.ilg mod$1.ind
167 if [ ! "$BUILD_DVI" ] ; then
168 rm -f $1.dvi
170 rm -rf $1.temp-html
171 rm -f $1/IMG* $1/*.pl $1/WARNINGS $1/index.dat $1/modindex.dat
172 # bibtex stuff
173 rm -f $1.bbl $1.blg
176 parse_option() {
177 # When using a long option with a parameter separated by '=',
178 # generalize the parsing of the two:
179 OPTION="$1"
180 unset VALUE
181 STUFF=`echo "$1" | grep '^--[-a-zA-Z0-9]*='`
182 if [ "$STUFF" ] ; then
183 # This leaves OPTION=--option= and VALUE=value
184 OPTION=`echo "$STUFF" | sed 's/^\(--[-a-zA-Z0-9]*=\)\(.*\)$/\1/'`
185 VALUE=`echo "$STUFF" | sed 's/^\(--[-a-zA-Z0-9]*=\)\(.*\)$/\2/'`
189 # figure out what our targets are:
190 while [ "$1" ] ; do
191 parse_option "$1"
192 case "$OPTION" in
193 --all|--al)
194 BUILD_PDF=true
195 BUILD_PS=true
196 BUILD_DVI=true
197 BUILD_HTML=true
198 BUILD_TEXT=true
199 USE_DEFAULT_FORMAT=false
200 shift 1
202 --pdf|--pd)
203 BUILD_PDF=true
204 USE_DEFAULT_FORMAT=false
205 shift 1
207 --ps|--postscript|--postscrip|--postscri|--postscr|--postsc|--posts|--post|--pos|--po)
208 BUILD_PS=true
209 USE_DEFAULT_FORMAT=false
210 shift 1
212 --dvi|--dv|--d)
213 BUILD_DVI=true
214 USE_DEFAULT_FORMAT=false
215 shift 1
217 --html|--htm|--ht)
218 BUILD_HTML=true
219 USE_DEFAULT_FORMAT=false
220 shift 1
222 --text|--tex|--te|--t)
223 BUILD_TEXT=true
224 USE_DEFAULT_FORMAT=false
225 shift 1
227 -H|--help|--hel|--he)
228 usage 0
230 -i|--iconserver|--iconserve|--iconserv|--iconser|--iconse|--icons|--icon|--ico|--ic|--i)
231 ICONSERVER="$2"
232 shift 2
234 --iconserver=*|--iconserve=*|--iconserv=*|--iconser=*|--iconse=*|--icons=*|--icon=*|--ico=*|--ic=*|--i=*)
235 ICONSERVER="$VALUE"
236 shift 1
238 -a|--address|--addres|--addre|-addr|--add|--ad)
239 ADDRESS="$2"
240 shift 2
242 --address=*|--addres=*|--addre=*|-addr=*|--add=*|--ad=*)
243 ADDRESS="$VALUE"
244 shift 1
246 --a4)
247 TEXINPUTS=$TOPDIR/paper-a4:$TEXINPUTS
248 shift 1
250 --letter|--lette|--lett|--let|--le)
251 shift 1
253 --link|--lin|--li)
254 LINK="$2"
255 shift 2
257 --link=*|--lin=*|--li=*)
258 LINK="$VALUE"
259 shift 1
261 -s|--split|--spli|--spl|--sp|--s)
262 MAX_SPLIT_DEPTH="$2"
263 shift 2
265 --split=|--spli=|--spl=|--sp=|--s=)
266 MAX_SPLIT_DEPTH="$VALUE"
267 shift 1
269 -l|--logging|--loggin|--loggi|--logg|--log|--lo)
270 LOGGING=true
271 shift 1
273 -D|--debugging|--debuggin|--debuggi|--debugg|--debug|--debu|--deb|--de)
274 DEBUGGING=true
275 shift 1
277 -k|--keep|--kee|--ke|--k)
278 DISCARD_TEMPS=''
279 shift 1
281 -q|--quiet|--quie|--qui|--qu|--q)
282 QUIET=true
283 shift 1
286 shift 1
287 break
290 usage 2 "Unknown option: ${VALUE%=}"
293 break;;
294 esac
295 done
297 if [ $# = 0 ] ; then
298 # check for a single .tex file in .
299 COUNT=`ls -1 *.tex | wc -l | sed 's/[ ]//g'`
300 if [ "$COUNT" -eq 1 ] ; then
301 set -- `ls -1 *.tex`
302 elif [ "$COUNT" -gt 1 ] ; then
303 usage 2 "Could not deduce which file(s) to process as HOWTO documents."
304 else
305 usage 2 "No file to process."
309 if [ $USE_DEFAULT_FORMAT = true ] ; then
310 eval "BUILD_$DEFAULT_FORMAT=true"
313 if [ "$QUIET" ] ; then
314 exec >/dev/null
317 if [ "$DEBUGGING" ] ; then
318 set -x
321 echo '# auxillary init file for latex2html' >$L2H_AUX_INIT_FILE
322 echo '# generated by mkhowto.sh -- do not edit' >>$L2H_AUX_INIT_FILE
323 if [ "$ICONSERVER" ] ; then
324 ICONSERVER="${ICONSERVER%/}"
326 l2hoption ICONSERVER "$ICONSERVER"
327 l2hoption ADDRESS "$ADDRESS"
328 l2hoption MAX_LINK_DEPTH "$MAX_LINK_DEPTH"
329 l2hoption MAX_SPLIT_DEPTH "$MAX_SPLIT_DEPTH"
330 echo '1;' >>$L2H_AUX_INIT_FILE
332 for FILE in $@ ; do
333 FILEDIR=`dirname $FILE`
334 FILE=`basename $FILE .tex`
336 # Put the directory the .tex file is in is also the first directory in
337 # TEXINPUTS, to allow files there to override files in the common area.
339 TEXINPUTS=$FILEDIR:$TOPDIR/texinputs:$TEXINPUTS
340 export TEXINPUTS
342 if [ "$BUILD_DVI" -o "$BUILD_PS" ] ; then
343 build_dvi $FILE 2>&1 | tee -a $LOGFILE
344 HAVE_TEMPS=true
346 if [ "$BUILD_PDF" ] ; then
347 build_pdf $FILE 2>&1 | tee -a $LOGFILE
348 HAVE_TEMPS=true
350 if [ "$BUILD_PS" ] ; then
351 build_ps $FILE 2>&1 | tee -a $LOGFILE
353 if [ "$BUILD_HTML" ] ; then
354 if [ ! "$HAVE_TEMPS" ] ; then
355 # need to get aux file
356 build_dvi $FILE 2>&1 | tee -a $LOGFILE
357 HAVE_TEMPS=true
359 build_html $FILE $FILE 2>&1 | tee -a $LOGFILE
360 if [ "$ICONSERVER" = "." ] ; then
361 cp $TOPDIR/icons/*.gif $FILE/
364 if [ "$BUILD_TEXT" ] ; then
365 if [ ! "$HAVE_TEMPS" ] ; then
366 # need to get aux file
367 build_dvi $FILE 2>&1 | tee -a $LOGFILE
368 HAVE_TEMPS=true
370 # this is why building text really has to be last:
371 if [ "$MAX_SPLIT_DEPTH" -ne 1 ] ; then
372 echo '# re-hack this file for --text:' >>$L2H_AUX_INIT_FILE
373 l2hoption MAX_SPLIT_DEPTH 1
374 echo '1;' >>$L2H_AUX_INIT_FILE
375 TEMPDIR=$FILE.temp-html
376 build_html $FILE $TEMPDIR 2>&1 | tee -a $LOGFILE
377 else
378 TEMPDIR=$FILE
380 build_text $FILE $TEMPDIR 2>&1 | tee -a $LOGFILE
383 if [ "$DISCARD_TEMPS" ] ; then
384 cleanup $FILE 2>&1 | tee -a $LOGFILE
386 # keep the logfile around
387 if [ "$LOGGING" ] ; then
388 cp $LOGFILE $FILE.how
390 rm -f $LOGFILE
391 done
393 if [ ! "$DEBUGGING" ] ; then
394 rm -f $L2H_AUX_INIT_FILE