/[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 - (hide annotations) (download)
Wed Mar 28 19:34:46 2012 UTC (2 years, 3 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 agriffis 1.1 #!/bin/bash
2 vapier 1.19 # $Header: /var/cvsroot/gentoo-src/build-docbook-catalog/build-docbook-catalog,v 1.18 2012/03/28 19:27:46 vapier Exp $
3 agriffis 1.1 #
4     # build-docbook-catalog: populate /etc/xml/docbook based in
5     # installed docbook-xml-dtd versions.
6     #
7 vapier 1.8 # Copyright 2004-2012 Gentoo Foundation
8 agriffis 1.1 # Distributed under the terms of the GNU General Public License v2
9     # written by Aron Griffis
10     #
11    
12 vapier 1.13 ROOTCONFDIR=/etc/xml
13     ROOTCATALOG=${ROOTCONFDIR}/catalog
14     CATALOG=${ROOTCONFDIR}/docbook
15 agriffis 1.1 DOCBOOKDIR=/usr/share/sgml/docbook
16     DTDS=
17     LATEST_DTD=
18     LATEST_DATE=
19 agriffis 1.2 VERBOSE=false
20     ZERO=${0##*/}
21 agriffis 1.1
22     #
23 vapier 1.9 # usage!
24     #
25     usage() {
26     cat <<-EOF
27     Usage: ${ZERO} [options]
28    
29     Options:
30 vapier 1.18 -r, --root ROOT path to work on
31 vapier 1.9 -v, --verbose Be verbose
32     -h, --help This!
33     EOF
34     [[ $# -gt 0 ]] && eerror "$*"
35     exit 0
36     }
37    
38     #
39 agriffis 1.1 # main (called from bottom)
40     #
41     main() {
42 vapier 1.10 local d v opts
43 agriffis 1.2
44 vapier 1.18 opts=$(getopt -o hr:v --long help,root:,verbose -n "${ZERO}" -- "$@") || exit 1
45 vapier 1.16 eval set -- "${opts}"
46 agriffis 1.2 while true; do
47 vapier 1.16 case $1 in
48 vapier 1.9 -h|--help) usage ;;
49 vapier 1.18 -r|--root) ROOT=$2 ; shift ;;
50 vapier 1.9 -v|--verbose) VERBOSE=true ;;
51     --) break ;;
52     *) usage "options parsing failed on $1!" ;;
53 agriffis 1.2 esac
54 vapier 1.9 shift
55 agriffis 1.2 done
56 agriffis 1.1
57 vapier 1.18 : ${ROOT:=/}
58     [[ ${ROOT} != */ ]] && ROOT="${ROOT}/"
59     [[ ${ROOT} != /* ]] && ROOT="${PWD}${ROOT}"
60     if [[ ${ROOT} != "/" ]] ; then
61     echo "Working on root ${ROOT}"
62     fi
63    
64 vapier 1.19 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 agriffis 1.1 create_catalogs # will exit on error
74 flameeyes 1.3 for type in xsl xsl-ns xsl-saxon xsl-xalan; do
75 vapier 1.16 populate_xsl ${type}
76 flameeyes 1.3 done
77 agriffis 1.1
78     # Clean out old dtds from catalog
79 agriffis 1.2 verb "Cleaning out old DocBook XML versions from ${CATALOG} and ${ROOTCATALOG}"
80 vapier 1.15 clean_catalog "${DOCBOOKDIR}/xml\(-simple\)*-dtd-[^/\"']*/[^/\"']*" "${CATALOG}"
81     clean_catalog "${DOCBOOKDIR}/xml\(-simple\)*-dtd-[^/\"']*/[^/\"']*" "${ROOTCATALOG}"
82 agriffis 1.1
83     if set_dtds; then
84     for d in ${DTDS}; do
85     populate_dtd ${d}
86     done
87 flameeyes 1.6 for d in ${SIMPLE_DTDS}; do
88     populate_simple_dtd ${d}
89     done
90 agriffis 1.1 populate_entities
91     fi
92    
93 vapier 1.19 ) 123>"${ROOT}${ROOTCONFDIR}/.keep"
94    
95 agriffis 1.1 exit 0
96     }
97    
98 vapier 1.7 #
99 agriffis 1.2 # verbose echo -- only echo if called with --verbose
100     #
101     verb() {
102 vapier 1.16 ${VERBOSE} && echo "$*"
103 agriffis 1.2 }
104    
105 agriffis 1.1 #
106 vapier 1.8 # show an error and abort
107     #
108     error() {
109     printf '%s: %b, aborting\n' "${ZERO}" "$*" 1>&2
110     exit 1
111     }
112    
113     #
114 agriffis 1.1 # fill in the DTDS variable based on installed versions
115 vapier 1.7 #
116 agriffis 1.1 set_dtds() {
117 vapier 1.11 DTDS= SIMPLE_DTS=
118    
119 vapier 1.18 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 vapier 1.11 fi
126    
127 agriffis 1.1 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 vapier 1.14 # 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 vapier 1.18 local file="${ROOT}$1"
142 vapier 1.14 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 agriffis 1.1 # create the catalogs root and docbook specific
152     #
153     create_catalogs() {
154 vapier 1.14 local adds
155    
156 vapier 1.18 if [[ ! -r ${ROOT}${ROOTCATALOG} ]] ; then
157 agriffis 1.1 echo "Creating XML Catalog root ${ROOTCATALOG}"
158 vapier 1.18 xmlcatalog --noout --create "${ROOT}${ROOTCATALOG}"
159     if [[ ! -r ${ROOT}${ROOTCATALOG} ]] ; then
160 vapier 1.8 error "failed creating ${ROOTCATALOG}"
161 agriffis 1.1 fi
162     else
163 agriffis 1.2 verb "Found XML Catalog root ${ROOTCATALOG}"
164 agriffis 1.1 # clean out existing entries
165 agriffis 1.2 verb " Cleaning existing ${CATALOG} delegates from ${ROOTCATALOG}"
166 vapier 1.15 clean_catalog "file://${CATALOG}" "${ROOTCATALOG}"
167 agriffis 1.1 fi
168    
169 vapier 1.18 if [[ ! -r ${ROOT}${CATALOG} ]] ; then
170 agriffis 1.1 echo "Creating DocBook XML Catalog ${CATALOG}"
171 vapier 1.18 xmlcatalog --noout --create "${ROOT}${CATALOG}"
172     if [[ ! -r ${ROOT}${CATALOG} ]] ; then
173 vapier 1.8 error "failed creating ${CATALOG}"
174 agriffis 1.1 fi
175     else
176 agriffis 1.2 verb "Found DocBook XML Catalog ${CATALOG}"
177 agriffis 1.1 fi
178    
179     # dtd pointers
180 agriffis 1.2 verb " Populating ${ROOTCATALOG} with DTD delegates to ${CATALOG}"
181 vapier 1.14 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 agriffis 1.1
190     # entities pointer
191 agriffis 1.2 verb " Populating ${ROOTCATALOG} with ISO entities delegate to ${CATALOG}"
192 vapier 1.14 adds=(
193     "delegatePublic" "ISO 8879:1986" "${CATALOG}"
194     )
195     multi_xmlcatalog_add "${ROOTCATALOG}" "${adds[@]}"
196 agriffis 1.1 }
197    
198     #
199     # clean_catalog
200     # $1 == regex to clean
201     # $2 == catalog
202     #
203     clean_catalog() {
204 vapier 1.18 local list f regex=$1 catalog=${ROOT}$2
205 agriffis 1.1
206     list=$(egrep --only-matching "${regex}" "${catalog}" | sort -u)
207     for f in ${list}; do
208 vapier 1.15 xmlcatalog --noout --del "${f}" "${catalog}"
209 agriffis 1.1 done
210     }
211    
212 vapier 1.7 #
213 agriffis 1.1 # populate a specific dtd version into the docbook catalog
214 vapier 1.18 # $1 == ./subpath/to/docbookx.dtd
215 agriffis 1.1 #
216     populate_dtd() {
217 vapier 1.18 local dtd=${DOCBOOKDIR}/$1
218     local docbookdir=${dtd%/*}
219     local v=${docbookdir##*-}
220     local adds dtd_date
221 agriffis 1.1
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 agriffis 1.2 verb " Populating ${CATALOG} based on ${docbookdir}"
231 vapier 1.14 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 agriffis 1.1
245     # grab the RCS date from docbookx.dtd for comparison purposes
246 vapier 1.18 if [[ ! -f ${ROOT}${docbookdir}/ent/iso-lat1.ent ]]; then
247 agriffis 1.2 verb " No entities available for ${dtd}"
248 agriffis 1.1 return 0
249     fi
250     dtd_date=$(egrep --only-matching --max-count=1 \
251 vapier 1.18 '[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}' \
252     "${ROOT}${dtd}")
253 agriffis 1.1 if [[ -z ${dtd_date} ]]; then
254 agriffis 1.2 verb " Couldn't find RCS date in ${dtd}, ignoring entities"
255 agriffis 1.1 return 0
256     fi
257 agriffis 1.2 verb " RCS datestamp in ${dtd} is ${dtd_date}"
258 vapier 1.16 dtd_date=$(date -d "${dtd_date}" +%s)
259     if [[ ${dtd_date} -gt ${LATEST_DATE:-0} ]] ; then
260 agriffis 1.1 LATEST_DATE=${dtd_date}
261     LATEST_DTD=${dtd}
262     fi
263     }
264    
265 vapier 1.7 #
266 flameeyes 1.6 # populate a specific simple dtd version into the docbook catalog
267 vapier 1.18 # $1 == ./subpath/to/sdocbook.dtd
268 flameeyes 1.6 #
269     populate_simple_dtd() {
270 vapier 1.18 local dtd=${DOCBOOKDIR}/$1
271     local docbookdir=${dtd%/*}
272     local v=${docbookdir##*-}
273     local adds dtd_date
274 flameeyes 1.6
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 vapier 1.14 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 flameeyes 1.6 }
291    
292 agriffis 1.1 #
293     # populate ISO DocBook entities from the most recent DTD
294     #
295     populate_entities() {
296 vapier 1.10 local isodir=${LATEST_DTD%/*}/ent i j
297     local entities=() avail=()
298 agriffis 1.1
299     # sanity check
300 vapier 1.18 if [[ -z ${LATEST_DTD} || ! -d ${ROOT}${isodir} ]]; then
301 agriffis 1.1 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 vapier 1.18 avail=($(ls "${ROOT}${isodir}" | sort))
332 agriffis 1.1
333     # double-check the lists
334 agriffis 1.2 verb " Populating ${CATALOG} with ISO DocBook entities"
335 agriffis 1.1 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 vapier 1.18 "file://${isodir}/${entities[i]}" "${ROOT}${CATALOG}"
352 agriffis 1.1 let j=j+1
353     let i=i+2
354     else
355 vapier 1.8 error "${0}: whoah, shouldn't be here"
356 agriffis 1.1 fi
357     done
358     }
359    
360     #
361     # populate XSL stylesheets
362     #
363     populate_xsl() {
364 vapier 1.14 local f adds
365 agriffis 1.1
366 flameeyes 1.3 # 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 vapier 1.15 clean_catalog "${DOCBOOKDIR}/${type}-stylesheets(-[0-9\.]+)?" "${CATALOG}"
371     clean_catalog "${DOCBOOKDIR}/${type}-stylesheets(-[0-9\.]+)?" "${ROOTCATALOG}"
372 flameeyes 1.3
373 vapier 1.17 local xsldir=${DOCBOOKDIR}/${type}-stylesheets
374 flameeyes 1.3
375 vapier 1.18 if [[ ! -d ${ROOT}${xsldir} ]] ; then
376 flameeyes 1.3 echo "DocBook XSL stylesheets (${type}) not found" >&2
377 agriffis 1.1 return 1
378     fi
379 flameeyes 1.3
380 vapier 1.18 if [[ ! -e ${ROOT}${xsldir}/html/docbook.xsl || ! -e ${ROOT}${xsldir}/common/l10n.xml ]] ; then
381 flameeyes 1.3 echo "DocBook XSL stylesheets are missing files from ${xsldir}" >&2
382 agriffis 1.1 return 1
383     fi
384    
385     # Populate catalog with XSL entries
386 flameeyes 1.3 echo "Found DocBook XSL stylesheets (${type}) in ${xsldir}"
387 flameeyes 1.4
388     verb " Populating ${ROOTCATALOG} with XSL delegations"
389 vapier 1.14 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 flameeyes 1.4
395 agriffis 1.2 verb " Populating ${CATALOG} with XSL stylesheets"
396 vapier 1.14 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 agriffis 1.1 }
402    
403     # Call the main routine
404 agriffis 1.2 main "$@"

  ViewVC Help
Powered by ViewVC 1.1.20