| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# Copyright (C) 2012-2020 SUSE Software Solutions Germany GmbH |
| 4 |
# |
| 5 |
# Author: |
| 6 |
# Thomas Schraitle <toms at opensuse dot org> |
| 7 |
# Frank Sundermeyer <fsundermeyer at opensuse dot org> |
| 8 |
# |
| 9 |
# Script to change DocBook 4 source to version 5 |
| 10 |
# |
| 11 |
|
| 12 |
# Name of this script: |
| 13 |
ME="$(basename "$0")" |
| 14 |
|
| 15 |
# TODO: Make it configurable by ~/.daps/config or ~/.config/daps/dapsrc |
| 16 |
# [[ -f ~/.daps/config ]] && source ~/.daps/config |
| 17 |
# [[ -f ~/.config/daps/dapsrc ]] && source ~/.config/daps/dapsrc |
| 18 |
|
| 19 |
# Contains the stylesheet to migrate DocBook 4 -> 5: |
| 20 |
MIGRATE_XSLT=${MIGRATE_XSLT:-/usr/share/xml/docbook/stylesheet/upgrade/db4-upgrade.xsl} |
| 21 |
|
| 22 |
# File extension |
| 23 |
EXT=${EXT:-.xml} |
| 24 |
|
| 25 |
# Where to write all xsl:message outputs from xsltproc: |
| 26 |
XSLTPROCLOG=${XSLTPROCLOG:-/tmp/daps-migrate.log} |
| 27 |
|
| 28 |
# Holds XSLT parameters which is passed to the XSLT processor |
| 29 |
declare -A PARAMS |
| 30 |
PARAMS=() |
| 31 |
|
| 32 |
# Verbosity |
| 33 |
# VERBOSE=0 |
| 34 |
|
| 35 |
# replacement string for root element in header file |
| 36 |
ROOTREPLACE=@RE@ |
| 37 |
|
| 38 |
## Functions |
| 39 |
function exit_on_error { |
| 40 |
echo -e "ERROR: $1" >&2 |
| 41 |
exit 1; |
| 42 |
} |
| 43 |
|
| 44 |
function dump { |
| 45 |
if [[ -n "$VERBOSE" ]]; then |
| 46 |
echo "$1" |
| 47 |
fi |
| 48 |
echo "$1" >> "$XSLTPROCLOG" 2>&1 |
| 49 |
} |
| 50 |
|
| 51 |
function get_rootelement { |
| 52 |
# |
| 53 |
# input: XML file name |
| 54 |
# output: root element |
| 55 |
# |
| 56 |
local FILE |
| 57 |
FILE=$1 |
| 58 |
if [[ -x /usr/bin/xmlstarlet ]]; then |
| 59 |
XMLSTARLET=/usr/bin/xmlstarlet |
| 60 |
elif [[ -x /usr/bin/xml ]]; then |
| 61 |
XMLSTARLET=/usr/bin/xml |
| 62 |
else |
| 63 |
exit_on_error "Cannot find xmlstarlet" |
| 64 |
fi |
| 65 |
|
| 66 |
ROOTELEMENT=$($XMLSTARLET sel -t -v "local-name(/*)" "$FILE") || exit_on_error "Cannot get root element from $FILE" |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
# Help function to preserve readability of the while loop |
| 71 |
function usage { |
| 72 |
cat <<EOF_helptext |
| 73 |
Usage: $ME [OPTIONS] INPUTDIR OUTPUTDIR |
| 74 |
|
| 75 |
Wrapper script to convert a set of DocBook documents |
| 76 |
|
| 77 |
Options: |
| 78 |
--header Path to a file containing a DocBook header. If specified, |
| 79 |
this header will be written to each file that is generated. |
| 80 |
The header needs to contain the placeholder "$ROOTREPLACE" in |
| 81 |
place of the root element |
| 82 |
-l LOGFILE, --log=LOGFILE |
| 83 |
Write all outputs of xsltproc to LOGFILE |
| 84 |
(default: $XSLTPROCLOG) |
| 85 |
-p, --param="XSLPARAM=VALUE" |
| 86 |
Defines parameter-value pair for the XSLT processor; |
| 87 |
can be used more than once |
| 88 |
-s XSLT, --stylesheet=XSLT |
| 89 |
Use stylesheet for transformation |
| 90 |
(default: $MIGRATE_XSLT) |
| 91 |
-h, --help Print this help |
| 92 |
-v, --verbose Be verbose |
| 93 |
|
| 94 |
Mandatory arguments: |
| 95 |
INPUTDIR Searches for all XML file in this directory |
| 96 |
OUTPUTDIR Write all XML file into the OUT directory |
| 97 |
EOF_helptext |
| 98 |
} |
| 99 |
|
| 100 |
function parse_param { |
| 101 |
# Syntax: parse_param "PARAMETER_AND_VALUE" |
| 102 |
# Extract parameter and value, syntax $1="param=value" |
| 103 |
local _PARAM=$1 |
| 104 |
[[ "$_PARAM" == *"="* ]] || exit_on_error "Option -p must adhere to the syntax PARAMETER=VALUE" |
| 105 |
p=${_PARAM%=*} |
| 106 |
v=${_PARAM#*=} |
| 107 |
# Define associative array: |
| 108 |
PARAMS["$p"]="$v" |
| 109 |
} |
| 110 |
|
| 111 |
## START |
| 112 |
# |
| 113 |
# TODO: getopts |
| 114 |
export POSIXLY_CORRECT=1 |
| 115 |
ARGS=$(getopt -o "hvl:s:p:" -l "header:,help,verbose,log:,stylesheet:,param:" -n "$ME" -- "$@") |
| 116 |
|
| 117 |
eval set -- "$ARGS" |
| 118 |
|
| 119 |
while true; do |
| 120 |
case "$1" in |
| 121 |
--header) |
| 122 |
if [[ -f $2 ]]; then |
| 123 |
if grep -q "$ROOTREPLACE" "$2" >/dev/null 2>&1; then |
| 124 |
HEADERFILE="$2" |
| 125 |
else |
| 126 |
exit_on_error "Header file does not contain the replacement string $ROOTREPLACE for the root element" |
| 127 |
fi |
| 128 |
else |
| 129 |
exit_on_error "$2 is not a regular file" |
| 130 |
fi |
| 131 |
shift 2 |
| 132 |
;; |
| 133 |
-h|--help) |
| 134 |
usage |
| 135 |
exit 1 |
| 136 |
shift |
| 137 |
;; |
| 138 |
-l|--log) |
| 139 |
XSLTPROCLOG="$2" |
| 140 |
shift 2 |
| 141 |
;; |
| 142 |
-p|--param) |
| 143 |
parse_param "$2" |
| 144 |
shift 2 |
| 145 |
;; |
| 146 |
-s|--stylesheet) |
| 147 |
MIGRATE_XSLT="$2" |
| 148 |
shift 2 |
| 149 |
;; |
| 150 |
-v|--verbose) |
| 151 |
VERBOSE=1 |
| 152 |
shift |
| 153 |
;; |
| 154 |
--) |
| 155 |
shift |
| 156 |
break;; |
| 157 |
*) exit_on_error "Internal error!" ;; |
| 158 |
esac |
| 159 |
done |
| 160 |
|
| 161 |
unset POSIXLY_CORRECT |
| 162 |
|
| 163 |
## Check for errors |
| 164 |
if [[ $# -eq 0 ]]; then |
| 165 |
usage |
| 166 |
exit 1 |
| 167 |
fi |
| 168 |
|
| 169 |
INPUTDIR=$1 |
| 170 |
OUTPUTDIR=$2 |
| 171 |
|
| 172 |
[[ $# -lt 2 ]] && exit_on_error "Missing arguments: Need both input and output directory!" |
| 173 |
[[ -d $INPUTDIR ]] || exit_on_error "First argument '$1' is not a directory" |
| 174 |
if [[ ! -d $OUTPUTDIR ]]; then |
| 175 |
mkdir -p "$OUTPUTDIR" |
| 176 |
fi |
| 177 |
|
| 178 |
[[ -e $XSLTPROCLOG ]] && rm "$XSLTPROCLOG" |
| 179 |
|
| 180 |
dump "** Starting DocBook Transformation **" |
| 181 |
dump "** Using stylesheet $MIGRATE_XSLT" |
| 182 |
p="" |
| 183 |
for i in "${!PARAMS[@]}"; do |
| 184 |
p="$i=${PARAMS[$i]} $p" |
| 185 |
done |
| 186 |
dump "** Parameters: $p" |
| 187 |
|
| 188 |
p="" |
| 189 |
for i in "${!PARAMS[@]}"; do |
| 190 |
p="--stringparam $i ${PARAMS[$i]} $p" |
| 191 |
done |
| 192 |
|
| 193 |
dump "** Command line: xsltproc --output FILE ${p} $MIGRATE_XSLT XMLINPUT" |
| 194 |
|
| 195 |
for xmlin in ${INPUTDIR}/*$EXT; do |
| 196 |
xmlout="$OUTPUTDIR/$(basename "$xmlin")" |
| 197 |
dump "$xmlin -> $xmlout" |
| 198 |
if [[ -n $HEADERFILE ]]; then |
| 199 |
cp "$HEADERFILE" "$xmlout" |
| 200 |
get_rootelement "$xmlin" |
| 201 |
sed -i "s/$ROOTREPLACE/$ROOTELEMENT/" "$xmlout" |
| 202 |
xsltproc --nonet ${p} "$MIGRATE_XSLT" "$xmlin" >> "$xmlout" 2>> "$XSLTPROCLOG" |
| 203 |
else |
| 204 |
xsltproc --nonet --output "$xmlout" ${p} "$MIGRATE_XSLT" "$xmlin" 2>> "$XSLTPROCLOG" |
| 205 |
fi |
| 206 |
# ERRCODE=$? |
| 207 |
dump "--------------------------------------------------" |
| 208 |
done |
| 209 |
|
| 210 |
dump "** Finished, see $XSLTPROCLOG for more information **" |
| 211 |
dump "" |
| 212 |
|
| 213 |
# EOF |