/[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.21 - (show annotations) (download)
Mon May 23 15:53:13 2016 UTC (12 months ago) by haubi
Branch: MAIN
CVS Tags: HEAD
Changes since 1.20: +2 -1 lines
Prepend ROOT with "/." when "//" redirects to network (as in Cygwin).

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

  ViewVC Help
Powered by ViewVC 1.1.20