| 1 |
#!/bin/bash |
| 2 |
# $Header: /var/cvsroot/gentoo-src/build-docbook-catalog/build-docbook-catalog,v 1.3 2008/09/22 13:43:02 flameeyes Exp $ |
| 3 |
# |
| 4 |
# build-docbook-catalog: populate /etc/xml/docbook based in |
| 5 |
# installed docbook-xml-dtd versions. |
| 6 |
# |
| 7 |
# Copyright 2004 Gentoo Foundation |
| 8 |
# Distributed under the terms of the GNU General Public License v2 |
| 9 |
# written by Aron Griffis |
| 10 |
# |
| 11 |
|
| 12 |
ROOTCATALOG=/etc/xml/catalog |
| 13 |
CATALOG=/etc/xml/docbook |
| 14 |
DOCBOOKDIR=/usr/share/sgml/docbook |
| 15 |
DTDS= |
| 16 |
LATEST_DTD= |
| 17 |
LATEST_DATE= |
| 18 |
VERBOSE=false |
| 19 |
ZERO=${0##*/} |
| 20 |
|
| 21 |
# |
| 22 |
# main (called from bottom) |
| 23 |
# |
| 24 |
main() { |
| 25 |
typeset d v opts |
| 26 |
|
| 27 |
opts=$(getopt -o v --long verbose -n "$ZERO" -- "$@") || exit 1 |
| 28 |
eval set -- "$opts" |
| 29 |
while true; do |
| 30 |
case "$1" in |
| 31 |
-v|--verbose) VERBOSE=true ; shift ;; |
| 32 |
--) shift ; break ;; |
| 33 |
*) echo "Options parsing failed on $1!" >&2 ; exit 1 ;; |
| 34 |
esac |
| 35 |
done |
| 36 |
|
| 37 |
create_catalogs # will exit on error |
| 38 |
for type in xsl xsl-ns xsl-saxon xsl-xalan; do |
| 39 |
populate_xsl $type |
| 40 |
done |
| 41 |
|
| 42 |
# Clean out old dtds from catalog |
| 43 |
verb "Cleaning out old DocBook XML versions from ${CATALOG} and ${ROOTCATALOG}" |
| 44 |
clean_catalog "${DOCBOOKDIR}/xml-dtd-[^/\"']*/[^/\"']*" ${CATALOG} |
| 45 |
clean_catalog "${DOCBOOKDIR}/xml-dtd-[^/\"']*/[^/\"']*" ${ROOTCATALOG} |
| 46 |
|
| 47 |
if set_dtds; then |
| 48 |
for d in ${DTDS}; do |
| 49 |
populate_dtd ${d} |
| 50 |
done |
| 51 |
populate_entities |
| 52 |
fi |
| 53 |
|
| 54 |
exit 0 |
| 55 |
} |
| 56 |
|
| 57 |
# |
| 58 |
# verbose echo -- only echo if called with --verbose |
| 59 |
# |
| 60 |
verb() { |
| 61 |
$VERBOSE && echo "$*" |
| 62 |
} |
| 63 |
|
| 64 |
# |
| 65 |
# fill in the DTDS variable based on installed versions |
| 66 |
# |
| 67 |
set_dtds() { |
| 68 |
DTDS=$(find ${DOCBOOKDIR} -path '*/xml-dtd-*/docbookx.dtd') |
| 69 |
if [[ -z ${DTDS} ]]; then |
| 70 |
echo "No installed DocBook XML DTDs found" |
| 71 |
return 1 |
| 72 |
else |
| 73 |
return 0 |
| 74 |
fi |
| 75 |
} |
| 76 |
|
| 77 |
# |
| 78 |
# create the catalogs root and docbook specific |
| 79 |
# |
| 80 |
create_catalogs() { |
| 81 |
if [[ ! -r ${ROOTCATALOG} ]] ; then |
| 82 |
echo "Creating XML Catalog root ${ROOTCATALOG}" |
| 83 |
/usr/bin/xmlcatalog --noout --create ${ROOTCATALOG} |
| 84 |
if [[ ! -r ${ROOTCATALOG} ]] ; then |
| 85 |
echo "Failed creating ${ROOTCATALOG}, aborting" >&2 |
| 86 |
exit 1 |
| 87 |
fi |
| 88 |
else |
| 89 |
verb "Found XML Catalog root ${ROOTCATALOG}" |
| 90 |
# clean out existing entries |
| 91 |
verb " Cleaning existing ${CATALOG} delegates from ${ROOTCATALOG}" |
| 92 |
clean_catalog "file://${CATALOG}" ${ROOTCATALOG} |
| 93 |
fi |
| 94 |
|
| 95 |
if [[ ! -r ${CATALOG} ]] ; then |
| 96 |
echo "Creating DocBook XML Catalog ${CATALOG}" |
| 97 |
/usr/bin/xmlcatalog --noout --create ${CATALOG} |
| 98 |
if [[ ! -r ${CATALOG} ]] ; then |
| 99 |
echo "Failed creating ${CATALOG}, aborting" >&2 |
| 100 |
exit 1 |
| 101 |
fi |
| 102 |
else |
| 103 |
verb "Found DocBook XML Catalog ${CATALOG}" |
| 104 |
fi |
| 105 |
|
| 106 |
# dtd pointers |
| 107 |
verb " Populating ${ROOTCATALOG} with DTD delegates to ${CATALOG}" |
| 108 |
xmlcatalog --noout --add "delegatePublic" "-//OASIS//ENTITIES DocBook XML" "file://${CATALOG}" ${ROOTCATALOG} |
| 109 |
xmlcatalog --noout --add "delegatePublic" "-//OASIS//DTD DocBook XML" "file://${CATALOG}" ${ROOTCATALOG} |
| 110 |
xmlcatalog --noout --add "delegateSystem" "http://www.oasis-open.org/docbook/" "file://${CATALOG}" ${ROOTCATALOG} |
| 111 |
xmlcatalog --noout --add "delegateURI" "http://www.oasis-open.org/docbook/" "file://${CATALOG}" ${ROOTCATALOG} |
| 112 |
|
| 113 |
# entities pointer |
| 114 |
verb " Populating ${ROOTCATALOG} with ISO entities delegate to ${CATALOG}" |
| 115 |
xmlcatalog --noout --add "delegatePublic" "ISO 8879:1986" "file://${CATALOG}" ${ROOTCATALOG} |
| 116 |
} |
| 117 |
|
| 118 |
# |
| 119 |
# clean_catalog |
| 120 |
# $1 == regex to clean |
| 121 |
# $2 == catalog |
| 122 |
# |
| 123 |
clean_catalog() { |
| 124 |
typeset list f regex=$1 catalog=$2 |
| 125 |
|
| 126 |
list=$(egrep --only-matching "${regex}" "${catalog}" | sort -u) |
| 127 |
for f in ${list}; do |
| 128 |
xmlcatalog --noout --del "${f}" ${catalog} |
| 129 |
done |
| 130 |
} |
| 131 |
|
| 132 |
# |
| 133 |
# populate a specific dtd version into the docbook catalog |
| 134 |
# $1 == /path/to/docbookx.dtd |
| 135 |
# |
| 136 |
populate_dtd() { |
| 137 |
typeset dtd=$1 docbookdir=${1%/*} dtd_date |
| 138 |
typeset v=${docbookdir##*-} |
| 139 |
|
| 140 |
# sanity check |
| 141 |
if [[ ${dtd} != */xml-dtd-*/* ]]; then |
| 142 |
echo "Warning: I don't understand \"${dtd}\"" >&2 |
| 143 |
return |
| 144 |
fi |
| 145 |
echo "Found DocBook XML ${v} in ${docbookdir}" |
| 146 |
|
| 147 |
# Populate the docbook catalog with this version |
| 148 |
verb " Populating ${CATALOG} based on ${docbookdir}" |
| 149 |
xmlcatalog --noout --add "public" "-//OASIS//ELEMENTS DocBook XML Information Pool V${v}//EN" "file://${docbookdir}/dbpoolx.mod" ${CATALOG} |
| 150 |
xmlcatalog --noout --add "public" "-//OASIS//DTD DocBook XML V${v}//EN" "file://${docbookdir}/docbookx.dtd" ${CATALOG} |
| 151 |
xmlcatalog --noout --add "public" "-//OASIS//ENTITIES DocBook XML Character Entities V${v}//EN" "file://${docbookdir}/dbcentx.mod" ${CATALOG} |
| 152 |
xmlcatalog --noout --add "public" "-//OASIS//ENTITIES DocBook XML Notations V${v}//EN" "file://${docbookdir}/dbnotnx.mod" ${CATALOG} |
| 153 |
xmlcatalog --noout --add "public" "-//OASIS//ENTITIES DocBook XML Additional General Entities V${v}//EN" "file://${docbookdir}/dbgenent.mod" ${CATALOG} |
| 154 |
xmlcatalog --noout --add "public" "-//OASIS//ELEMENTS DocBook XML Document Hierarchy V${v}//EN" "file://${docbookdir}/dbhierx.mod" ${CATALOG} |
| 155 |
xmlcatalog --noout --add "public" "-//OASIS//DTD XML Exchange Table Model 19990315//EN" "file://${docbookdir}/soextblx.dtd" ${CATALOG} |
| 156 |
xmlcatalog --noout --add "public" "-//OASIS//DTD DocBook XML CALS Table Model V${v}//EN" "file://${docbookdir}/calstblx.dtd" ${CATALOG} |
| 157 |
xmlcatalog --noout --add "rewriteSystem" "http://www.oasis-open.org/docbook/xml/${v}" "file://${docbookdir}" ${CATALOG} |
| 158 |
xmlcatalog --noout --add "rewriteURI" "http://www.oasis-open.org/docbook/xml/${v}" "file://${docbookdir}" ${CATALOG} |
| 159 |
|
| 160 |
# grab the RCS date from docbookx.dtd for comparison purposes |
| 161 |
if [[ ! -f ${docbookdir}/ent/iso-lat1.ent ]]; then |
| 162 |
verb " No entities available for ${dtd}" |
| 163 |
return 0 |
| 164 |
fi |
| 165 |
dtd_date=$(egrep --only-matching --max-count=1 \ |
| 166 |
'[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}' "${dtd}") |
| 167 |
if [[ -z ${dtd_date} ]]; then |
| 168 |
verb " Couldn't find RCS date in ${dtd}, ignoring entities" |
| 169 |
return 0 |
| 170 |
fi |
| 171 |
verb " RCS datestamp in ${dtd} is ${dtd_date}" |
| 172 |
dtd_date=$(date -d "$dtd_date" +%s) |
| 173 |
if [[ -z $LATEST_DTD || $dtd_date -gt $LATEST_DATE ]]; then |
| 174 |
LATEST_DATE=${dtd_date} |
| 175 |
LATEST_DTD=${dtd} |
| 176 |
fi |
| 177 |
} |
| 178 |
|
| 179 |
# |
| 180 |
# populate ISO DocBook entities from the most recent DTD |
| 181 |
# |
| 182 |
populate_entities() { |
| 183 |
typeset isodir=${LATEST_DTD%/*}/ent i j |
| 184 |
typeset -a entities avail |
| 185 |
|
| 186 |
# sanity check |
| 187 |
if [[ -z ${LATEST_DTD} || ! -d ${isodir} ]]; then |
| 188 |
echo "No ISO DocBook entities available for catalog" |
| 189 |
return 0 |
| 190 |
fi |
| 191 |
echo "Using ISO DocBook entities from ${isodir}" |
| 192 |
|
| 193 |
# here are the entities we know about; |
| 194 |
# note these must remain sorted! |
| 195 |
entities=( |
| 196 |
"iso-amsa.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Arrow Relations//EN" |
| 197 |
"iso-amsb.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Binary Operators//EN" |
| 198 |
"iso-amsc.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Delimiters//EN" |
| 199 |
"iso-amsn.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Negated Relations//EN" |
| 200 |
"iso-amso.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Ordinary//EN" |
| 201 |
"iso-amsr.ent" "ISO 8879:1986//ENTITIES Added Math Symbols: Relations//EN" |
| 202 |
"iso-box.ent" "ISO 8879:1986//ENTITIES Box and Line Drawing//EN" |
| 203 |
"iso-cyr1.ent" "ISO 8879:1986//ENTITIES Russian Cyrillic//EN" |
| 204 |
"iso-cyr2.ent" "ISO 8879:1986//ENTITIES Non-Russian Cyrillic//EN" |
| 205 |
"iso-dia.ent" "ISO 8879:1986//ENTITIES Diacritical Marks//EN" |
| 206 |
"iso-grk1.ent" "ISO 8879:1986//ENTITIES Greek Letters//EN" |
| 207 |
"iso-grk2.ent" "ISO 8879:1986//ENTITIES Monotoniko Greek//EN" |
| 208 |
"iso-grk3.ent" "ISO 8879:1986//ENTITIES Greek Symbols//EN" |
| 209 |
"iso-grk4.ent" "ISO 8879:1986//ENTITIES Alternative Greek Symbols//EN" |
| 210 |
"iso-lat1.ent" "ISO 8879:1986//ENTITIES Added Latin 1//EN" |
| 211 |
"iso-lat2.ent" "ISO 8879:1986//ENTITIES Added Latin 2//EN" |
| 212 |
"iso-num.ent" "ISO 8879:1986//ENTITIES Numeric and Special Graphic//EN" |
| 213 |
"iso-pub.ent" "ISO 8879:1986//ENTITIES Publishing//EN" |
| 214 |
"iso-tech.ent" "ISO 8879:1986//ENTITIES General Technical//EN" |
| 215 |
) |
| 216 |
|
| 217 |
# here are the entities available; assume no spaces in filenames... |
| 218 |
avail=($(ls ${isodir} | sort)) |
| 219 |
|
| 220 |
# double-check the lists |
| 221 |
verb " Populating ${CATALOG} with ISO DocBook entities" |
| 222 |
i=0 ; j=0 |
| 223 |
while [[ ${i} -lt ${#entities[@]} || ${j} -lt ${#avail[@]} ]]; do |
| 224 |
if [[ ${i} -ge ${#entities[@]} ]]; then |
| 225 |
echo "Warning: Extra ISO entities file: ${avail[j]}" |
| 226 |
let j=j+1 |
| 227 |
elif [[ ${j} -ge ${#avail[@]} ]]; then |
| 228 |
echo "Warning: Entities file not found: ${entities[i]}" |
| 229 |
let i=i+2 |
| 230 |
elif [[ ${avail[j]} < ${entities[i]} ]]; then |
| 231 |
echo "Warning: Extra ISO entities file: ${avail[j]}" |
| 232 |
let j=j+1 |
| 233 |
elif [[ ${entities[i]} < ${avail[j]} ]]; then |
| 234 |
echo "Warning: Entities file not found: ${entities[i]}" |
| 235 |
let i=i+2 |
| 236 |
elif [[ ${entities[i]} == ${avail[j]} ]]; then |
| 237 |
xmlcatalog --noout --add "public" "${entities[i+1]}" \ |
| 238 |
"file://${isodir}/${entities[i]}" ${CATALOG} |
| 239 |
let j=j+1 |
| 240 |
let i=i+2 |
| 241 |
else |
| 242 |
echo "${0}: Whoah, shouldn't be here, aborting" >&2 |
| 243 |
exit 1 |
| 244 |
fi |
| 245 |
done |
| 246 |
} |
| 247 |
|
| 248 |
# |
| 249 |
# populate XSL stylesheets |
| 250 |
# |
| 251 |
populate_xsl() { |
| 252 |
typeset listed avail f |
| 253 |
|
| 254 |
# This is either xsl, xsl-ns, xsl-saxon or xsl-xalan |
| 255 |
local type=$1 |
| 256 |
|
| 257 |
# Delete current entries from the catalog (delete legacy versioned entries too) |
| 258 |
clean_catalog "${DOCBOOKDIR}/${type}-stylesheets(-[0-9\.]+)?" $CATALOG |
| 259 |
clean_catalog "${DOCBOOKDIR}/${type}-stylesheets(-[0-9\.]+)?" $ROOTCATALOG |
| 260 |
|
| 261 |
xsldir=/usr/share/sgml/docbook/${type}-stylesheets |
| 262 |
|
| 263 |
if [[ ! -d ${xsldir} ]]; then |
| 264 |
echo "DocBook XSL stylesheets (${type}) not found" >&2 |
| 265 |
return 1 |
| 266 |
fi |
| 267 |
|
| 268 |
if [[ ! -e ${xsldir}/html/docbook.xsl || ! -e ${xsldir}/common/l10n.xml ]]; then |
| 269 |
echo "DocBook XSL stylesheets are missing files from ${xsldir}" >&2 |
| 270 |
return 1 |
| 271 |
fi |
| 272 |
|
| 273 |
# Populate catalog with XSL entries |
| 274 |
echo "Found DocBook XSL stylesheets (${type}) in ${xsldir}" |
| 275 |
|
| 276 |
verb " Populating ${ROOTCATALOG} with XSL delegations" |
| 277 |
xmlcatalog --noout --add "delegateSystem" "http://docbook.sourceforge.net/release/${type}/" "file://${CATALOG}" ${ROOTCATALOG} |
| 278 |
xmlcatalog --noout --add "delegateURI" "http://docbook.sourceforge.net/release/${type}/" "file://${CATALOG}" ${ROOTCATALOG} |
| 279 |
|
| 280 |
verb " Populating ${CATALOG} with XSL stylesheets" |
| 281 |
xmlcatalog --noout --add "rewriteSystem" "http://docbook.sourceforge.net/release/${type}/current" "file://${xsldir}" ${CATALOG} |
| 282 |
xmlcatalog --noout --add "rewriteURI" "http://docbook.sourceforge.net/release/${type}/current" "file://${xsldir}" ${CATALOG} |
| 283 |
} |
| 284 |
|
| 285 |
# Call the main routine |
| 286 |
main "$@" |