/[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.20 - (show annotations) (download)
Sat Sep 28 02:30:06 2013 UTC (9 months, 3 weeks ago) by ottxor
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +13 -6 lines
merge prefix patches

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

  ViewVC Help
Powered by ViewVC 1.1.20