/[gentoo-src]/build-docbook-catalog/build-docbook-catalog
Gentoo

Contents of /build-docbook-catalog/build-docbook-catalog

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (show annotations) (download)
Wed Mar 28 19:34:46 2012 UTC (2 years, 5 months ago) by vapier
Branch: MAIN
Changes since 1.18: +12 -5 lines
add locking to /etc/xml dir to avoid multiple runs trashing each other

1 #!/bin/bash
2 # $Header: /var/cvsroot/gentoo-src/build-docbook-catalog/build-docbook-catalog,v 1.18 2012/03/28 19:27:46 vapier Exp $
3 #
4 # build-docbook-catalog: populate /etc/xml/docbook based in
5 # installed docbook-xml-dtd versions.
6 #
7 # Copyright 2004-2012 Gentoo Foundation
8 # Distributed under the terms of the GNU General Public License v2
9 # written by Aron Griffis
10 #
11
12 ROOTCONFDIR=/etc/xml
13 ROOTCATALOG=${ROOTCONFDIR}/catalog
14 CATALOG=${ROOTCONFDIR}/docbook
15 DOCBOOKDIR=/usr/share/sgml/docbook
16 DTDS=
17 LATEST_DTD=
18 LATEST_DATE=
19 VERBOSE=false
20 ZERO=${0##*/}
21
22 #
23 # usage!
24 #
25 usage() {
26 cat <<-EOF
27 Usage: ${ZERO} [options]
28
29 Options:
30 -r, --root ROOT path to work on
31 -v, --verbose Be verbose
32 -h, --help This!
33 EOF
34 [[ $# -gt 0 ]] && eerror "$*"
35 exit 0
36 }
37
38 #
39 # main (called from bottom)
40 #
41 main() {
42 local d v opts
43
44 opts=$(getopt -o hr:v --long help,root:,verbose -n "${ZERO}" -- "$@") || exit 1
45 eval set -- "${opts}"
46 while true; do
47 case $1 in
48 -h|--help) usage ;;
49 -r|--root) ROOT=$2 ; shift ;;
50 -v|--verbose) VERBOSE=true ;;
51 --) break ;;
52 *) usage "options parsing failed on $1!" ;;
53 esac
54 shift
55 done
56
57 : ${ROOT:=/}
58 [[ ${ROOT} != */ ]] && ROOT="${ROOT}/"
59 [[ ${ROOT} != /* ]] && ROOT="${PWD}${ROOT}"
60 if [[ ${ROOT} != "/" ]] ; then
61 echo "Working on root ${ROOT}"
62 fi
63
64 if [[ ! -d ${ROOT}${ROOTCONFDIR} ]] ; then
65 mkdir -p "${ROOT}${ROOTCONFDIR}" || error "could not create ${ROOTCONFDIR}"
66 fi
67
68 (
69 # Lock the dir to avoid trashing other runs that might
70 # be running parallel.
71 flock 123 || error "unable to lock ${ROOTCONFDIR}"
72
73 create_catalogs # will exit on error
74 for type in xsl xsl-ns xsl-saxon xsl-xalan; do
75 populate_xsl ${type}
76 done
77
78 # Clean out old dtds from catalog
79 verb "Cleaning out old DocBook XML versions from ${CATALOG} and ${ROOTCATALOG}"
80 clean_catalog "${DOCBOOKDIR}/xml\(-simple\)*-dtd-[^/\"']*/[^/\"']*" "${CATALOG}"
81 clean_catalog "${DOCBOOKDIR}/xml\(-simple\)*-dtd-[^/\"']*/[^/\"']*" "${ROOTCATALOG}"
82
83 if set_dtds; then
84 for d in ${DTDS}; do
85 populate_dtd ${d}
86 done
87 for d in ${SIMPLE_DTDS}; do
88 populate_simple_dtd ${d}
89 done
90 populate_entities
91 fi
92
93 ) 123>"${ROOT}${ROOTCONFDIR}/.keep"
94
95 exit 0
96 }
97
98 #
99 # verbose echo -- only echo if called with --verbose
100 #
101 verb() {
102 ${VERBOSE} && echo "$*"
103 }
104
105 #
106 # show an error and abort
107 #
108 error() {
109 printf '%s: %b, aborting\n' "${ZERO}" "$*" 1>&2
110 exit 1
111 }
112
113 #
114 # fill in the DTDS variable based on installed versions
115 #
116 set_dtds() {
117 DTDS= SIMPLE_DTS=
118
119 local d=${ROOT}${DOCBOOKDIR}
120 if [[ -d ${d} ]] ; then
121 pushd "${d}" >/dev/null || return 1
122 DTDS=$(find xml-dtd-*/ -name docbookx.dtd)
123 SIMPLE_DTDS=$(find xml-simple-dtd-*/ -name sdocbook.dtd)
124 popd >/dev/null
125 fi
126
127 if [[ -z ${DTDS} ]]; then
128 echo "No installed DocBook XML DTDs found"
129 return 1
130 else
131 return 0
132 fi
133 }
134
135 #
136 # multi_xmlcatalog_add <file> <opts array>
137 #
138 # the opts array is a set of three: what gets passed to --add
139 #
140 multi_xmlcatalog_add() {
141 local file="${ROOT}$1"
142 shift
143
144 while [[ $# -gt 0 ]] ; do
145 xmlcatalog --noout --add "$1" "$2" "file://$3" "${file}"
146 shift 3
147 done
148 }
149
150 #
151 # create the catalogs root and docbook specific
152 #
153 create_catalogs() {
154 local adds
155
156 if [[ ! -r ${ROOT}${ROOTCATALOG} ]] ; then
157 echo "Creating XML Catalog root ${ROOTCATALOG}"
158 xmlcatalog --noout --create "${ROOT}${ROOTCATALOG}"
159 if [[ ! -r ${ROOT}${ROOTCATALOG} ]] ; then
160 error "failed creating ${ROOTCATALOG}"
161 fi
162 else
163 verb "Found XML Catalog root ${ROOTCATALOG}"
164 # clean out existing entries
165 verb " Cleaning existing ${CATALOG} delegates from ${ROOTCATALOG}"
166 clean_catalog "file://${CATALOG}" "${ROOTCATALOG}"
167 fi
168
169 if [[ ! -r ${ROOT}${CATALOG} ]] ; then
170 echo "Creating DocBook XML Catalog ${CATALOG}"
171 xmlcatalog --noout --create "${ROOT}${CATALOG}"
172 if [[ ! -r ${ROOT}${CATALOG} ]] ; then
173 error "failed creating ${CATALOG}"
174 fi
175 else
176 verb "Found DocBook XML Catalog ${CATALOG}"
177 fi
178
179 # dtd pointers
180 verb " Populating ${ROOTCATALOG} with DTD delegates to ${CATALOG}"
181 adds=(
182 "delegatePublic" "-//OASIS//ENTITIES DocBook" "${CATALOG}"
183 "delegatePublic" "-//OASIS//ELEMENTS DocBook" "${CATALOG}"
184 "delegatePublic" "-//OASIS//DTD DocBook" "${CATALOG}"
185 "delegateSystem" "http://www.oasis-open.org/docbook/" "${CATALOG}"
186 "delegateURI" "http://www.oasis-open.org/docbook/" "${CATALOG}"
187 )
188 multi_xmlcatalog_add "${ROOTCATALOG}" "${adds[@]}"
189
190 # entities pointer
191 verb " Populating ${ROOTCATALOG} with ISO entities delegate to ${CATALOG}"
192 adds=(
193 "delegatePublic" "ISO 8879:1986" "${CATALOG}"
194 )
195 multi_xmlcatalog_add "${ROOTCATALOG}" "${adds[@]}"
196 }
197
198 #
199 # clean_catalog
200 # $1 == regex to clean
201 # $2 == catalog
202 #
203 clean_catalog() {
204 local list f regex=$1 catalog=${ROOT}$2
205
206 list=$(egrep --only-matching "${regex}" "${catalog}" | sort -u)
207 for f in ${list}; do
208 xmlcatalog --noout --del "${f}" "${catalog}"
209 done
210 }
211
212 #
213 # populate a specific dtd version into the docbook catalog
214 # $1 == ./subpath/to/docbookx.dtd
215 #
216 populate_dtd() {
217 local dtd=${DOCBOOKDIR}/$1
218 local docbookdir=${dtd%/*}
219 local v=${docbookdir##*-}
220 local adds dtd_date
221
222 # sanity check
223 if [[ ${dtd} != */xml-dtd-*/* ]]; then
224 echo "Warning: I don't understand \"${dtd}\"" >&2
225 return
226 fi
227 echo "Found DocBook XML ${v} in ${docbookdir}"
228
229 # Populate the docbook catalog with this version
230 verb " Populating ${CATALOG} based on ${docbookdir}"
231 adds=(
232 "public" "-//OASIS//ELEMENTS DocBook XML Information Pool V${v}//EN" "${docbookdir}/dbpoolx.mod"
233 "public" "-//OASIS//DTD DocBook XML V${v}//EN" "${docbookdir}/docbookx.dtd"
234 "public" "-//OASIS//ENTITIES DocBook XML Character Entities V${v}//EN" "${docbookdir}/dbcentx.mod"
235 "public" "-//OASIS//ENTITIES DocBook XML Notations V${v}//EN" "${docbookdir}/dbnotnx.mod"
236 "public" "-//OASIS//ENTITIES DocBook XML Additional General Entities V${v}//EN" "${docbookdir}/dbgenent.mod"
237 "public" "-//OASIS//ELEMENTS DocBook XML Document Hierarchy V${v}//EN" "${docbookdir}/dbhierx.mod"
238 "public" "-//OASIS//DTD XML Exchange Table Model 19990315//EN" "${docbookdir}/soextblx.dtd"
239 "public" "-//OASIS//DTD DocBook XML CALS Table Model V${v}//EN" "${docbookdir}/calstblx.dtd"
240 "rewriteSystem" "http://www.oasis-open.org/docbook/xml/${v}" "${docbookdir}"
241 "rewriteURI" "http://www.oasis-open.org/docbook/xml/${v}" "${docbookdir}"
242 )
243 multi_xmlcatalog_add "${CATALOG}" "${adds[@]}"
244
245 # grab the RCS date from docbookx.dtd for comparison purposes
246 if [[ ! -f ${ROOT}${docbookdir}/ent/iso-lat1.ent ]]; then
247 verb " No entities available for ${dtd}"
248 return 0
249 fi
250 dtd_date=$(egrep --only-matching --max-count=1 \
251 '[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}' \
252 "${ROOT}${dtd}")
253 if [[ -z ${dtd_date} ]]; then
254 verb " Couldn't find RCS date in ${dtd}, ignoring entities"
255 return 0
256 fi
257 verb " RCS datestamp in ${dtd} is ${dtd_date}"
258 dtd_date=$(date -d "${dtd_date}" +%s)
259 if [[ ${dtd_date} -gt ${LATEST_DATE:-0} ]] ; then
260 LATEST_DATE=${dtd_date}
261 LATEST_DTD=${dtd}
262 fi
263 }
264
265 #
266 # populate a specific simple dtd version into the docbook catalog
267 # $1 == ./subpath/to/sdocbook.dtd
268 #
269 populate_simple_dtd() {
270 local dtd=${DOCBOOKDIR}/$1
271 local docbookdir=${dtd%/*}
272 local v=${docbookdir##*-}
273 local adds dtd_date
274
275 # sanity check
276 if [[ ${dtd} != */xml-simple-dtd-*/* ]]; then
277 echo "Warning: I don't understand \"${dtd}\"" >&2
278 return
279 fi
280 echo "Found Simplified DocBook XML ${v} in ${docbookdir}"
281
282 # Populate the docbook catalog with this version
283 verb " Populating ${CATALOG} based on ${docbookdir}"
284 adds=(
285 "public" "-//OASIS//DTD Simplified DocBook XML V${v}//EN" "${docbookdir}/sdocbook.dtd"
286 "rewriteSystem" "http://www.oasis-open.org/docbook/xml/simple/${v}" "${docbookdir}"
287 "rewriteURI" "http://www.oasis-open.org/docbook/xml/simple/${v}" "${docbookdir}"
288 )
289 multi_xmlcatalog_add "${CATALOG}" "${adds[@]}"
290 }
291
292 #
293 # populate ISO DocBook entities from the most recent DTD
294 #
295 populate_entities() {
296 local isodir=${LATEST_DTD%/*}/ent i j
297 local entities=() avail=()
298
299 # sanity check
300 if [[ -z ${LATEST_DTD} || ! -d ${ROOT}${isodir} ]]; then
301 echo "No ISO DocBook entities available for catalog"
302 return 0
303 fi
304 echo "Using ISO DocBook entities from ${isodir}"
305
306 # here are the entities we know about;
307 # note these must remain sorted!
308 entities=(
309 "iso-amsa.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Arrow Relations//EN"
310 "iso-amsb.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Binary Operators//EN"
311 "iso-amsc.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Delimiters//EN"
312 "iso-amsn.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Negated Relations//EN"
313 "iso-amso.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Ordinary//EN"
314 "iso-amsr.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Relations//EN"
315 "iso-box.ent" "ISO 8879:1986//ENTITIES Box and Line Drawing//EN"
316 "iso-cyr1.ent" "ISO 8879:1986//ENTITIES Russian Cyrillic//EN"
317 "iso-cyr2.ent" "ISO 8879:1986//ENTITIES Non-Russian Cyrillic//EN"
318 "iso-dia.ent" "ISO 8879:1986//ENTITIES Diacritical Marks//EN"
319 "iso-grk1.ent" "ISO 8879:1986//ENTITIES Greek Letters//EN"
320 "iso-grk2.ent" "ISO 8879:1986//ENTITIES Monotoniko Greek//EN"
321 "iso-grk3.ent" "ISO 8879:1986//ENTITIES Greek Symbols//EN"
322 "iso-grk4.ent" "ISO 8879:1986//ENTITIES Alternative Greek Symbols//EN"
323 "iso-lat1.ent" "ISO 8879:1986//ENTITIES Added Latin 1//EN"
324 "iso-lat2.ent" "ISO 8879:1986//ENTITIES Added Latin 2//EN"
325 "iso-num.ent" "ISO 8879:1986//ENTITIES Numeric and Special Graphic//EN"
326 "iso-pub.ent" "ISO 8879:1986//ENTITIES Publishing//EN"
327 "iso-tech.ent" "ISO 8879:1986//ENTITIES General Technical//EN"
328 )
329
330 # here are the entities available; assume no spaces in filenames...
331 avail=($(ls "${ROOT}${isodir}" | sort))
332
333 # double-check the lists
334 verb " Populating ${CATALOG} with ISO DocBook entities"
335 i=0 ; j=0
336 while [[ ${i} -lt ${#entities[@]} || ${j} -lt ${#avail[@]} ]]; do
337 if [[ ${i} -ge ${#entities[@]} ]]; then
338 echo "Warning: Extra ISO entities file: ${avail[j]}"
339 let j=j+1
340 elif [[ ${j} -ge ${#avail[@]} ]]; then
341 echo "Warning: Entities file not found: ${entities[i]}"
342 let i=i+2
343 elif [[ ${avail[j]} < ${entities[i]} ]]; then
344 echo "Warning: Extra ISO entities file: ${avail[j]}"
345 let j=j+1
346 elif [[ ${entities[i]} < ${avail[j]} ]]; then
347 echo "Warning: Entities file not found: ${entities[i]}"
348 let i=i+2
349 elif [[ ${entities[i]} == ${avail[j]} ]]; then
350 xmlcatalog --noout --add "public" "${entities[i+1]}" \
351 "file://${isodir}/${entities[i]}" "${ROOT}${CATALOG}"
352 let j=j+1
353 let i=i+2
354 else
355 error "${0}: whoah, shouldn't be here"
356 fi
357 done
358 }
359
360 #
361 # populate XSL stylesheets
362 #
363 populate_xsl() {
364 local f adds
365
366 # This is either xsl, xsl-ns, xsl-saxon or xsl-xalan
367 local type=$1
368
369 # Delete current entries from the catalog (delete legacy versioned entries too)
370 clean_catalog "${DOCBOOKDIR}/${type}-stylesheets(-[0-9\.]+)?" "${CATALOG}"
371 clean_catalog "${DOCBOOKDIR}/${type}-stylesheets(-[0-9\.]+)?" "${ROOTCATALOG}"
372
373 local xsldir=${DOCBOOKDIR}/${type}-stylesheets
374
375 if [[ ! -d ${ROOT}${xsldir} ]] ; then
376 echo "DocBook XSL stylesheets (${type}) not found" >&2
377 return 1
378 fi
379
380 if [[ ! -e ${ROOT}${xsldir}/html/docbook.xsl || ! -e ${ROOT}${xsldir}/common/l10n.xml ]] ; then
381 echo "DocBook XSL stylesheets are missing files from ${xsldir}" >&2
382 return 1
383 fi
384
385 # Populate catalog with XSL entries
386 echo "Found DocBook XSL stylesheets (${type}) in ${xsldir}"
387
388 verb " Populating ${ROOTCATALOG} with XSL delegations"
389 adds=(
390 "delegateSystem" "http://docbook.sourceforge.net/release/${type}/" "${CATALOG}"
391 "delegateURI" "http://docbook.sourceforge.net/release/${type}/" "${CATALOG}"
392 )
393 multi_xmlcatalog_add "${ROOTCATALOG}" "${adds[@]}"
394
395 verb " Populating ${CATALOG} with XSL stylesheets"
396 adds=(
397 "rewriteSystem" "http://docbook.sourceforge.net/release/${type}/current" "${xsldir}"
398 "rewriteURI" "http://docbook.sourceforge.net/release/${type}/current" "${xsldir}"
399 )
400 multi_xmlcatalog_add "${CATALOG}" "${adds[@]}"
401 }
402
403 # Call the main routine
404 main "$@"

  ViewVC Help
Powered by ViewVC 1.1.20