summaryrefslogtreecommitdiff
path: root/build-aux/db2any
blob: a240aaa0f8c42ad73831732ddd67adb0ca46b17b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/bin/sh
# db2any - Docbook to html/ps/info rendering
#
#	Copyright (C) 2000, 2001 Free Software Foundation, Inc
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
#
# Note: This requires a Posix shell
#
# $Id$

pgm="db2any"
version="0.7.3"

usage () {
    echo 'usage: db2any [--help] [options] filename' >&2
    exit 1
}

show_banner () {
	cat <<EOF
$pgm $version
Copyright (C) 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

EOF
}

show_help () {
    show_banner
    cat <<EOF
usage: db2any [options] docbookfile

Options:
    --mode    select the mode (one of: ${all_modes})
    --help
    --nosplit
    --copyfiles
    --systemcheck
    --verbose
    --draft
    --manvolume n
    --usestyle	file
    --ignore-xref      [not yet implemented]

EOF
    exit 0
}

# a list of all possible stylesheet locations
stylesheet_dirs='
/usr/local/lib/dsssl/stylesheets/docbook
/usr/local/share/dsssl/stylesheets/docbook
/usr/local/lib/sgml/stylesheet/dsssl/docbook/nwalsh
/usr/local/share/sgml/stylesheet/dsssl/docbook/nwalsh
/usr/lib/dsssl/stylesheets/docbook
/usr/share/dsssl/stylesheets/docbook
/usr/lib/sgml/stylesheet/dsssl/docbook/nwalsh
/usr/share/sgml/stylesheet/dsssl/docbook/nwalsh
/usr/lib/sgml/stylesheets/nwalsh-modular
/usr/share/sgml/stylesheets/nwalsh-modular
'

all_modes="check tex html man texinfo"
input=
verbose=no
draft=no
nosplit=no
copyfiles=no
systemcheck_only=no
mode=none
manvolume="man"
usestyle=""
expected_args=1
ignore_xref=no
while test "`echo $1 | head -c1`" = "-"; do
    case $1 in
      --version)
	echo "$pgm $version"
	exit 0
	;;
      --help|-h|-help)
	show_help
	exit 0
	;;
      --nosplit)
	nosplit=yes
	;;
      --copyfiles)
	copyfiles=yes
	;;
      --systemcheck)
	systemcheck_only=yes
	expected_args=0
	;;
      --mode)
	shift
	if [ $# = 0 ]; then
	   echo "$pgm: missing argument for --mode" >&2
	   exit 1
	fi
	mode="$1"
	;;
      --manvolume)
	shift
	if [ $# = 0 ]; then
	   echo "$pgm: missing argument for --manvolume" >&2
	   exit 1
	fi
	manvolume="$1"
	;;
      --usestyle)
	shift
	if [ $# = 0 ]; then
	   echo "$pgm: missing argument for --usestyle" >&2
	   exit 1
	fi
	usestyle="$1"
	;;
      --verbose)
	verbose=yes
	;;
      --draft)
	draft=yes
	;;
      --ignore-xref)
        ignore_xref=yes
        ;;
      --)
	shift
	break
	;;
      *)
	echo "$pgm: invalid option $1" >&2
	exit 1
	;;
    esac
    shift
done

if [ $# = $expected_args ]; then
    if [ $# = 1 ]; then
       input="$1"
    fi
else
   usage
fi

# check the mode
case "$mode" in
    html|HTML)
	mode=html
	;;
    tex|TEX|TeX|dvi)
	mode=dvi
	;;
    man|MAN)
	mode=man
	;;
    texi|TEXI|texinfo|TEXINFO)
	mode=texinfo
	;;
    check)
	;;
    none)
	if [ $systemcheck_only != yes ]; then
	    echo "$pgm: no mode specified; use one of: ${all_modes}" >&2
	    exit 1
	fi
	;;
    *)
	echo "$pgm: invalid mode $mode" >&2
	echo "$pgm: valid modes are: ${all_modes}"  >&2
	exit 1
	;;
esac

if [ -n $usestyle ]; then
   if [ ! -f $usestyle ]; then
      echo "$pgm: cannot access local stylesteet" >&2
      exit 1
   fi
fi

#######################################
#   Options are all parsed here       #
#######################################

# check whether the given program is availbale in the path
check_prog () {
    tmp="$1"
    save_ifs="$IFS"; IFS=":"
    for i in $PATH; do
        test -z "$i" && i=.
        if test -f $i/$tmp; then
             IFS="$save_ifs"
             return 0
        fi
    done
    IFS="$save_ifs"
    return 1
}


# Figure out all what we need to know about the system we are
# running on and where the Docbook tools are installed.
# Tell about it when running in verbose mode.
do_systemcheck () {
    # look for Jade
    jade_version=`jade -v </dev/null 2>&1 | \
		   sed -n 's/.*:I:.*Jade version "\([0-9.]*\)"/\1/p'`
    if [ -z "$jade_version" ]; then
	echo "$pgm: error: jade not found" >&2
	exit 1
    fi
    [ $verbose = yes ] && echo "$pgm: Jade version $jade_version found" >&2

    # look for JadeTeX
    if ! jadetex -v 2>/dev/null | grep -q '^TeX' ; then
	echo "$pgm: error: jadetex not found" >&2
	exit 1
    fi

    # look for the docbook-to-man script. Fixme: we should check that it
    # it is a recent version.
    if ! docbook-to-man 2>&1 | grep -q '^usage' ; then
	echo "$pgm: error: docbook-to-man not found" >&2
	exit 1
    fi
    [ $verbose = yes ] && echo "$pgm: docbook-to-man found" >&2

    # look for the docbook-to-texi scripts.
    if ! check_prog docbook2texi ;  then
	echo "$pgm: error: docbook2texi not found" >&2
	exit 1
    fi
    [ $verbose = yes ] && echo "$pgm: docbook2texi found" >&2
    if ! sgml2xml -v /dev/null 2>&1 | grep 'SP version' >/dev/null 2>&1 ; then
	echo "$pgm: error: sgml2xml not found" >&2
	exit 1
    fi
    [ $verbose = yes ] && echo "$pgm: sgml2xml found" >&2

    # figure out where our stylesheets are
    tex_stylesheet=none
    for d in ${stylesheet_dirs}; do
	file=${d}/print/docbook.dsl
	if [ -f $file ]; then
	    tex_stylesheet=$file
	    break
	fi
    done
    [ $verbose = yes ] && echo "$pgm: TeX stylesheet: ${tex_stylesheet}" >&2
    html_stylesheet=none
    for d in ${stylesheet_dirs}; do
	file=${d}/html/docbook.dsl
	if [ -f $file ]; then
	    html_stylesheet=$file
	    break
	fi
    done
    [ $verbose = yes ] && echo "$pgm: HTML stylesheet: ${html_stylesheet}" >&2
    if [ $tex_stylesheet = none -o $html_stylesheet = none ]; then
	echo "$pgm: error: stylesheets not found" >&2
	exit 1
    fi

}


# Render the docbook as HTML
render_html () {
    output="`basename $input| sed 's/\.sgml$//'`.html"

    if [ -n "$usestyle" ]; then
	tmpstyle="`pwd`/`basename $usestyle`-html.tmp"
	if [ ! -f $tmpstyle -o $usestyle -nt $tmpstyle ]; then
	    sed "s%@DOCBOOK_DSL@%$html_stylesheet%" $usestyle > $tmpstyle
	fi
    else
	tmpstyle="$tex_stylesheet"
    fi


    # --nosplts creates just one HTML file
    if test $nosplit = yes; then
	echo "running jade on '$input' ..." >&2
	jade -D . -d $tmpstyle -t sgml -i html -V nochunks $input > $output
	echo "$output created"
	return 0
    fi

    # Make sure that we have a html subdir
    if test -d html ; then
	:
    else
	if mkdir html; then
	    echo "'html' directory created" >&2
	else
	    echo "failed to create 'html' directory" >&2
	    exit 1
	fi
    fi

    outputdir="html/`basename $input| sed 's/\.sgml$//'`"

    if test -d $outputdir ; then
	:
    else
	if mkdir $outputdir; then
	    echo "'$outputdir' created" >&2
	else
	    echo "failed to create '$outputdir'" >&2
	    exit 1
	fi
    fi
    echo "creating html pages in '$outputdir' ..." >&2
    if test "$input" = "`basename $input`"; then
	inp="../../$input"
    else
	inp="$input"
    fi

    [ $verbose = yes ] && echo "running jade on '$inp' ..." >&2
    (cd $outputdir && jade -D . -t sgml -i html -d $tmpstyle $inp )
    [ $verbose = yes ] && echo "html version in '$outputdir' created" >&2

    # break out all filerefs and copy them to the outputdirectory
    # fixme: handling of path components is wrong
    if test $copyfiles = yes; then
	echo "looking for filerefs ..." >&2
	for file in `nsgmls -i html $input \
			| awk '/^AFILEREF[ \t]+CDATA/ {print $3}'`; do
	    d=$outputdir/`basename $file`
	    if cat $file > $outputdir/`basename $file` ; then
		echo "  $file -> $d" >&2
	    fi
	done
    fi

    mainfile=`ls $outputdir/${doctype}* | head -1`

    # create a html index file for it, so that we can more easy
    # find the rendred pages
    cat > $output <<EOF
<html><title>$output</title>
<body>

<a href="$mainfile">$mainfile</a>

</body>
</html>
EOF

    [ $verbose = yes ] && echo "$output created with link to '$mainfile'" >&2
}


# This function expects the source file in $texfile and
# the name of the logfle in $logfile
run_jadetex () {
    [ -f $logfile ] && rm $logfile
    jadetex $texfile
    if ! tail $logfile | grep -q '^Output written on'; then
	echo "JadeTeX failed" >&2
	exit 1
    fi
}

# Render the docbook to DVI
render_dvi () {
    output="`basename $input| sed 's/\.sgml$//'`.dvi"
    texfile="`basename $input| sed 's/\.sgml$//'`.tex"
    logfile="`basename $input| sed 's/\.sgml$//'`.log"
    auxfile="`basename $input| sed 's/\.sgml$//'`.aux"

    if [ -n "$usestyle" ]; then
	tmpstyle="`basename $usestyle`-tex.tmp"
	if [ ! -f $tmpstyle -o $usestyle -nt $tmpstyle ]; then
	    sed "s%@DOCBOOK_DSL@%$tex_stylesheet%" $usestyle > $tmpstyle
	fi
    else
	tmpstyle="$tex_stylesheet"
    fi
    [ $verbose = yes ] && echo "running jade on '$input' ..." >&2
    jade -D . -t tex -i tex -d $tmpstyle -o $texfile $input
    if ! tail $texfile | grep -q '\\endFOT{}'; then
	echo "Jade failed" >&2
	exit 1
    fi

    # Better delete the aux file first
    [ -f $auxfile ] && rm $auxfile
    # The first run won't get the references right, so we have to
    # run it 2 or 3 times.  JadeTex doesn't indicate whether a third
    # run is required, so we do it always.
    run_jadetex
    if [ $draft = no ]; then
	if tail -100 $logfile \
	   | grep -q '^LaTeX Warning: There were undefined references'; then
	   echo 'running JadeTeX a second and third 2time' >&2
	   run_jadetex
	   run_jadetex
	fi
    fi

    [ $verbose = yes ] && echo "$output created as '$output'" >&2
}

# Render the docbook to troff
render_man () {
    output="`basename $input| sed 's/\.sgml$//'`.$manvolume"

    [ $verbose = yes ] && echo "running docbook-to-man on '$input' ..." >&2
    docbook-to-man $input > $output
    [ $verbose = yes ] && echo "man page '$output' created" >&2
}

# Render the docbook to texinfo
render_texinfo () {
    output="`basename $input| sed 's/\.sgml$/.texi/'`"
    tmpxml="`basename $input| sed 's/\.sgml$/.xml/'`"

    [ $verbose = yes ] && echo "running sgml2xml on '$input' ..." >&2
    sgml2xml -x lower $input > $tmpxml
    [ $verbose = yes ] && echo "running docbook2texi on '$tmpxml' ..." >&2
    docbook2texi $tmpxml | sed 's,--,---,' >$output
    rm $tmpxml
    [ $verbose = yes ] && echo "texinfo '$output' created" >&2

}

#######################################
#	 main function		      #
#######################################

do_systemcheck
[ $systemcheck_only = yes ] && exit 0

if [ ! -f "$input" ]; then
    input="$input.sgml"
    if [ ! -f "$input" ]; then
	echo "$pgm: '$input': no such file" >&2
	exit 1
    fi
fi

# grep the document type
doctype=`grep -i '\<doctype' $input|awk 'NR==1 {print $2}'| tr '[A-Z]' '[a-z]'`
if test -z "$doctype"; then
    echo "$pgm: error: no DOCTYPE declaration found" >&2
    exit 1
fi
[ $verbose = yes ] &&  echo "$input: DOCTYPE is '$doctype'" >&2

case $mode in
    check)
	nsgmls -vs $input
	exit $?
	;;
    html)
	render_html
	;;
    dvi)
	render_dvi
	;;
    man)
	render_man
	;;
    texinfo)
	render_texinfo
	;;
esac


exit 0