aboutsummaryrefslogtreecommitdiff
blob: f3246bd5030f82022ed1dd626e6d7eae22daecc4 (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
#!/bin/bash
# $Header: /usr/local/src/gentoo/gentoo-src/cvsroot/gentoo-src/build-docbook-catalog/build-docbook-catalog,v 1.11 2012/03/27 22:41:10 vapier Exp $
#
# build-docbook-catalog: populate /etc/xml/docbook based in
# installed docbook-xml-dtd versions.
#
# Copyright 2004-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# written by Aron Griffis
#

ROOTCATALOG=/etc/xml/catalog
CATALOG=/etc/xml/docbook
DOCBOOKDIR=/usr/share/sgml/docbook
DTDS=
LATEST_DTD=
LATEST_DATE=
VERBOSE=false
ZERO=${0##*/}

#
# usage!
#
usage() {
	cat <<-EOF
	Usage: ${ZERO} [options]

	Options:
	  -v, --verbose    Be verbose
	  -h, --help       This!
	EOF
	[[ $# -gt 0 ]] && eerror "$*"
	exit 0
}

#
# main (called from bottom)
#
main() {
	local d v opts

	opts=$(getopt -o hv --long help,verbose -n "$ZERO" -- "$@") || exit 1
	eval set -- "$opts"
	while true; do
		case "$1" in
			-h|--help) usage ;;
			-v|--verbose) VERBOSE=true ;;
			--) break ;;
			*) usage "options parsing failed on $1!" ;;
		esac
		shift
	done

	create_catalogs			# will exit on error
	for type in xsl xsl-ns xsl-saxon xsl-xalan; do
	    populate_xsl $type
	done

	# Clean out old dtds from catalog
	verb "Cleaning out old DocBook XML versions from ${CATALOG} and ${ROOTCATALOG}"
	clean_catalog "${DOCBOOKDIR}/xml\(-simple\)*-dtd-[^/\"']*/[^/\"']*" ${CATALOG}
	clean_catalog "${DOCBOOKDIR}/xml\(-simple\)*-dtd-[^/\"']*/[^/\"']*" ${ROOTCATALOG}

	if set_dtds; then
		for d in ${DTDS}; do
			populate_dtd ${d}
		done
		for d in ${SIMPLE_DTDS}; do
			populate_simple_dtd ${d}
		done
		populate_entities
	fi

	exit 0
}

#
# verbose echo -- only echo if called with --verbose
#
verb() {
	$VERBOSE && echo "$*"
}

#
# show an error and abort
#
error() {
	printf '%s: %b, aborting\n' "${ZERO}" "$*" 1>&2
	exit 1
}

#
# fill in the DTDS variable based on installed versions
#
set_dtds() {
	DTDS= SIMPLE_DTS=

	if [[ -d ${DOCBOOKDIR} ]] ; then
		DTDS=$(find ${DOCBOOKDIR} -path '*/xml-dtd-*/docbookx.dtd')
		SIMPLE_DTDS=$(find ${DOCBOOKDIR} -path '*/xml-simple-dtd-*/sdocbook.dtd')
	fi

	if [[ -z ${DTDS} ]]; then
		echo "No installed DocBook XML DTDs found"
		return 1
	else
		return 0
	fi
}

#
# create the catalogs root and docbook specific
#
create_catalogs() {
	if [[ ! -r ${ROOTCATALOG} ]] ; then
		echo "Creating XML Catalog root ${ROOTCATALOG}"
		/usr/bin/xmlcatalog --noout --create ${ROOTCATALOG}
		if [[ ! -r ${ROOTCATALOG} ]] ; then
			error "failed creating ${ROOTCATALOG}"
		fi
	else
		verb "Found XML Catalog root ${ROOTCATALOG}"
		# clean out existing entries
		verb "  Cleaning existing ${CATALOG} delegates from ${ROOTCATALOG}"
		clean_catalog "file://${CATALOG}" ${ROOTCATALOG}
	fi

	if [[ ! -r ${CATALOG} ]] ; then
		echo "Creating DocBook XML Catalog ${CATALOG}"
		/usr/bin/xmlcatalog --noout --create ${CATALOG}
		if [[ ! -r ${CATALOG} ]] ; then
			error "failed creating ${CATALOG}"
		fi
	else
		verb "Found DocBook XML Catalog ${CATALOG}"
	fi

	# dtd pointers
	verb "  Populating ${ROOTCATALOG} with DTD delegates to ${CATALOG}"
	xmlcatalog --noout --add "delegatePublic" "-//OASIS//ENTITIES DocBook" "file://${CATALOG}" ${ROOTCATALOG}
	xmlcatalog --noout --add "delegatePublic" "-//OASIS//ELEMENTS DocBook" "file://${CATALOG}" ${ROOTCATALOG}
	xmlcatalog --noout --add "delegatePublic" "-//OASIS//DTD DocBook" "file://${CATALOG}" ${ROOTCATALOG}
	xmlcatalog --noout --add "delegateSystem" "http://www.oasis-open.org/docbook/" "file://${CATALOG}" ${ROOTCATALOG}
	xmlcatalog --noout --add "delegateURI" "http://www.oasis-open.org/docbook/" "file://${CATALOG}" ${ROOTCATALOG}

	# entities pointer
	verb "  Populating ${ROOTCATALOG} with ISO entities delegate to ${CATALOG}"
	xmlcatalog --noout --add "delegatePublic" "ISO 8879:1986" "file://${CATALOG}" ${ROOTCATALOG}
}

#
# clean_catalog
# $1 == regex to clean
# $2 == catalog
#
clean_catalog() {
	local list f regex=$1 catalog=$2

	list=$(egrep --only-matching "${regex}" "${catalog}" | sort -u)
	for f in ${list}; do
		xmlcatalog --noout --del "${f}" ${catalog}
	done
}

#
# populate a specific dtd version into the docbook catalog
# $1 == /path/to/docbookx.dtd
#
populate_dtd() {
	local dtd=$1 docbookdir=${1%/*} dtd_date
	local v=${docbookdir##*-}

	# sanity check
	if [[ ${dtd} != */xml-dtd-*/* ]]; then
		echo "Warning: I don't understand \"${dtd}\"" >&2
		return
	fi
	echo "Found DocBook XML ${v} in ${docbookdir}"

	# Populate the docbook catalog with this version
	verb "  Populating ${CATALOG} based on ${docbookdir}"
	xmlcatalog --noout --add "public" "-//OASIS//ELEMENTS DocBook XML Information Pool V${v}//EN" "file://${docbookdir}/dbpoolx.mod" ${CATALOG}
	xmlcatalog --noout --add "public" "-//OASIS//DTD DocBook XML V${v}//EN" "file://${docbookdir}/docbookx.dtd" ${CATALOG}
	xmlcatalog --noout --add "public" "-//OASIS//ENTITIES DocBook XML Character Entities V${v}//EN" "file://${docbookdir}/dbcentx.mod" ${CATALOG}
	xmlcatalog --noout --add "public" "-//OASIS//ENTITIES DocBook XML Notations V${v}//EN" "file://${docbookdir}/dbnotnx.mod" ${CATALOG}
	xmlcatalog --noout --add "public" "-//OASIS//ENTITIES DocBook XML Additional General Entities V${v}//EN" "file://${docbookdir}/dbgenent.mod" ${CATALOG}
	xmlcatalog --noout --add "public" "-//OASIS//ELEMENTS DocBook XML Document Hierarchy V${v}//EN" "file://${docbookdir}/dbhierx.mod" ${CATALOG}
	xmlcatalog --noout --add "public" "-//OASIS//DTD XML Exchange Table Model 19990315//EN" "file://${docbookdir}/soextblx.dtd" ${CATALOG}
	xmlcatalog --noout --add "public" "-//OASIS//DTD DocBook XML CALS Table Model V${v}//EN" "file://${docbookdir}/calstblx.dtd" ${CATALOG}
	xmlcatalog --noout --add "rewriteSystem" "http://www.oasis-open.org/docbook/xml/${v}" "file://${docbookdir}" ${CATALOG}
	xmlcatalog --noout --add "rewriteURI" "http://www.oasis-open.org/docbook/xml/${v}" "file://${docbookdir}" ${CATALOG}

	# grab the RCS date from docbookx.dtd for comparison purposes
	if [[ ! -f ${docbookdir}/ent/iso-lat1.ent ]]; then
		verb "  No entities available for ${dtd}"
		return 0
	fi
	dtd_date=$(egrep --only-matching --max-count=1 \
		'[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}' "${dtd}")
	if [[ -z ${dtd_date} ]]; then
		verb "  Couldn't find RCS date in ${dtd}, ignoring entities"
		return 0
	fi
	verb "  RCS datestamp in ${dtd} is ${dtd_date}"
	dtd_date=$(date -d "$dtd_date" +%s)
	if [[ -z $LATEST_DTD || $dtd_date -gt $LATEST_DATE ]]; then
		LATEST_DATE=${dtd_date}
		LATEST_DTD=${dtd}
	fi
}

#
# populate a specific simple dtd version into the docbook catalog
# $1 == /path/to/sdocbook.dtd
#
populate_simple_dtd() {
	local dtd=$1 docbookdir=${1%/*}
	local v=${docbookdir##*-}

	# sanity check
	if [[ ${dtd} != */xml-simple-dtd-*/* ]]; then
		echo "Warning: I don't understand \"${dtd}\"" >&2
		return
	fi
	echo "Found Simplified DocBook XML ${v} in ${docbookdir}"

	# Populate the docbook catalog with this version
	verb "  Populating ${CATALOG} based on ${docbookdir}"
	xmlcatalog --noout --add "public" "-//OASIS//DTD Simplified DocBook XML V${v}//EN" "file://${docbookdir}/sdocbook.dtd" ${CATALOG}
	xmlcatalog --noout --add "rewriteSystem" "http://www.oasis-open.org/docbook/xml/simple/${v}" "file://${docbookdir}" ${CATALOG}
	xmlcatalog --noout --add "rewriteURI" "http://www.oasis-open.org/docbook/xml/simple/${v}" "file://${docbookdir}" ${CATALOG}
}

#
# populate ISO DocBook entities from the most recent DTD
#
populate_entities() {
	local isodir=${LATEST_DTD%/*}/ent i j
	local entities=() avail=()

	# sanity check
	if [[ -z ${LATEST_DTD} || ! -d ${isodir} ]]; then
		echo "No ISO DocBook entities available for catalog"
		return 0
	fi
	echo "Using ISO DocBook entities from ${isodir}"

	# here are the entities we know about;
	# note these must remain sorted!
	entities=(
		"iso-amsa.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Arrow Relations//EN"
		"iso-amsb.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Binary Operators//EN"
		"iso-amsc.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Delimiters//EN"
		"iso-amsn.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Negated Relations//EN"
		"iso-amso.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Ordinary//EN"
		"iso-amsr.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Relations//EN"
		"iso-box.ent" "ISO 8879:1986//ENTITIES Box and Line Drawing//EN"
		"iso-cyr1.ent" "ISO 8879:1986//ENTITIES Russian Cyrillic//EN"
		"iso-cyr2.ent" "ISO 8879:1986//ENTITIES Non-Russian Cyrillic//EN"
		"iso-dia.ent" "ISO 8879:1986//ENTITIES Diacritical Marks//EN"
		"iso-grk1.ent" "ISO 8879:1986//ENTITIES Greek Letters//EN"
		"iso-grk2.ent" "ISO 8879:1986//ENTITIES Monotoniko Greek//EN"
		"iso-grk3.ent" "ISO 8879:1986//ENTITIES Greek Symbols//EN"
		"iso-grk4.ent" "ISO 8879:1986//ENTITIES Alternative Greek Symbols//EN"
		"iso-lat1.ent" "ISO 8879:1986//ENTITIES Added Latin 1//EN"
		"iso-lat2.ent" "ISO 8879:1986//ENTITIES Added Latin 2//EN"
		"iso-num.ent" "ISO 8879:1986//ENTITIES Numeric and Special Graphic//EN"
		"iso-pub.ent" "ISO 8879:1986//ENTITIES Publishing//EN"
		"iso-tech.ent" "ISO 8879:1986//ENTITIES General Technical//EN"
	)

	# here are the entities available; assume no spaces in filenames...
	avail=($(ls ${isodir} | sort))

	# double-check the lists
	verb "  Populating ${CATALOG} with ISO DocBook entities"
	i=0 ; j=0
	while [[ ${i} -lt ${#entities[@]} || ${j} -lt ${#avail[@]} ]]; do
		if [[ ${i} -ge ${#entities[@]} ]]; then
			echo "Warning: Extra ISO entities file: ${avail[j]}"
			let j=j+1
		elif [[ ${j} -ge ${#avail[@]} ]]; then
			echo "Warning: Entities file not found: ${entities[i]}"
			let i=i+2
		elif [[ ${avail[j]} < ${entities[i]} ]]; then
			echo "Warning: Extra ISO entities file: ${avail[j]}"
			let j=j+1
		elif [[ ${entities[i]} < ${avail[j]} ]]; then
			echo "Warning: Entities file not found: ${entities[i]}"
			let i=i+2
		elif [[ ${entities[i]} == ${avail[j]} ]]; then
			xmlcatalog --noout --add "public" "${entities[i+1]}" \
				"file://${isodir}/${entities[i]}" ${CATALOG}
			let j=j+1
			let i=i+2
		else
			error "${0}: whoah, shouldn't be here"
		fi
	done
}

#
# populate XSL stylesheets
#
populate_xsl() {
	local f

	# This is either xsl, xsl-ns, xsl-saxon or xsl-xalan
	local type=$1

	# Delete current entries from the catalog (delete legacy versioned entries too)
	clean_catalog "${DOCBOOKDIR}/${type}-stylesheets(-[0-9\.]+)?" $CATALOG
	clean_catalog "${DOCBOOKDIR}/${type}-stylesheets(-[0-9\.]+)?" $ROOTCATALOG

	local xsldir=/usr/share/sgml/docbook/${type}-stylesheets

	if [[ ! -d ${xsldir} ]]; then
		echo "DocBook XSL stylesheets (${type}) not found" >&2
		return 1
	fi

	if [[ ! -e ${xsldir}/html/docbook.xsl || ! -e ${xsldir}/common/l10n.xml ]]; then
		echo "DocBook XSL stylesheets are missing files from ${xsldir}" >&2
		return 1
	fi

	# Populate catalog with XSL entries
	echo "Found DocBook XSL stylesheets (${type}) in ${xsldir}"

	verb "  Populating ${ROOTCATALOG} with XSL delegations"
	xmlcatalog --noout --add "delegateSystem" "http://docbook.sourceforge.net/release/${type}/" "file://${CATALOG}" ${ROOTCATALOG}
	xmlcatalog --noout --add "delegateURI" "http://docbook.sourceforge.net/release/${type}/" "file://${CATALOG}" ${ROOTCATALOG}

	verb "  Populating ${CATALOG} with XSL stylesheets"
	xmlcatalog --noout --add "rewriteSystem" "http://docbook.sourceforge.net/release/${type}/current" "file://${xsldir}" ${CATALOG}
	xmlcatalog --noout --add "rewriteURI" "http://docbook.sourceforge.net/release/${type}/current" "file://${xsldir}" ${CATALOG}
}

# Call the main routine
main "$@"