Blame SOURCES/pacemaker-borrow-schemas

f3ee57
#!/bin/sh
f3ee57
# Copyright 2017 Red Hat, Inc.
f3ee57
# Part of clufter project
f3ee57
# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
f3ee57
f3ee57
# A helper for "borrow validation schemas from pacemaker" process.
f3ee57
f3ee57
die() { echo; echo "$@"; exit 1; }
f3ee57
f3ee57
# $1 ... input directory with original pacemaker schemas
f3ee57
# $2 ... output directory with consolidated schemas
f3ee57
# $3 ... schemas to skip (as posix-egrep expression)
f3ee57
# $4 ... postprocess XSLT
f3ee57
# $5 ... clobber existing files?  (true if set and non-null)
f3ee57
consolidate() {
f3ee57
	inputdir="${1}"; outputdir="${2}"; skipschemas="${3}"; postprocess="${4}"
f3ee57
	test "${#}" -lt 5 || clobber="${5}"
f3ee57
	mkdir -p -- "${outputdir}"
f3ee57
	# for all the schema versions at the boundary of the "major" bump,
f3ee57
	# except for the lower boundary of the first one (i.e. pacemaker-1.0)
f3ee57
	# -- the versions in between are not interesting from validation POV
f3ee57
	for base in $(
f3ee57
	  find "${inputdir}" -regextype posix-egrep -regex "${skipschemas}" -prune \
f3ee57
	    -o -name 'pacemaker-*.rng' -printf '%P\n' | sort -V \
f3ee57
	  | sed -e 'N;/^\(pacemaker-[0-9]\)\.\([0-9][0-9]*\)\.rng\n\1\.\([0-9][0-9]*\)\.rng$/!p;D'); do
f3ee57
		f="${inputdir}/${base}"
f3ee57
		printf "processing: ${f} ... "
f3ee57
		test -f "${f}" || continue
f3ee57
		sentinel=10; old=/dev/null; new="${f}"
f3ee57
		# until the jing output converged (simplification gets idempotent)
f3ee57
		# as prescribed by did-size-change heuristic (or sentinel is hit)
f3ee57
		while [ "$(stat -c '%s' "${old}")" != "$(stat -c '%s' "${new}")" ]; do
f3ee57
			[ "$((sentinel -= 1))" -gt 0 ] || break
f3ee57
			[ "${old}" = "${f}" ] && old="${outputdir}/${base}";
f3ee57
			[ "${new}" = "${f}" ] \
f3ee57
			  && { old="${f}"; new="${outputdir}/${base}.new"; } \
f3ee57
			  || cp -f "${new}" "${old}"
f3ee57
			jing -is "${old}" > "${new}"
f3ee57
			#printf "(%d -> %d) " "$(stat -c '%s' "${old}")" "$(stat -c '%s' "${new}")"
f3ee57
		done
f3ee57
		printf "%d iterations" "$((10 - ${sentinel}))"
f3ee57
		test -z "${clobber-}" && test -s "${old}" && die "file ${old} already exists" || :
f3ee57
		if [ -z "${postprocess}" ]; then
f3ee57
			mv "${new}" "${old}"
f3ee57
			printf ", moved\n"
f3ee57
		else
f3ee57
			# xmllint drops empty lines caused by the applied transformation
f3ee57
			xsltproc --stringparam filename-or-version "${base}" \
f3ee57
			  "${postprocess}" "${new}" \
f3ee57
			  | xmllint --format - > "${old}"
f3ee57
			rm -f -- "${new}"
f3ee57
			printf ", postprocessed\n"
f3ee57
		fi
f3ee57
	done
f3ee57
}
f3ee57
f3ee57
which jing >/dev/null 2>&1 || die "jing (from jing-trang project) required"
f3ee57
f3ee57
: "${INPUTDIR=$(pkg-config --variable schemadir pacemaker)}"
f3ee57
test -n "${INPUTDIR}" || die "Input dir with pacemaker schemas not known"
f3ee57
f3ee57
: "${OUTPUTDIR=schemas-consolidated}"
f3ee57
test -n "${OUTPUTDIR}" || die "Output dir for consolidated schemas not known"
f3ee57
f3ee57
: "${POSTPROCESS=$(dirname "${0}")/fix-jing-simplified-rng.xsl}"
f3ee57
f3ee57
# skip non-defaults of upstream releases
f3ee57
#: "${SKIPSCHEMAS=.*/pacemaker-(1\.0|2\.[126]).rng}"
f3ee57
: "${SKIPSCHEMAS=".*/pacemaker-next\.rng"}"  # only skip WIP schema by default
f3ee57
f3ee57
consolidate "${INPUTDIR}" "${OUTPUTDIR}" "${SKIPSCHEMAS}" "${POSTPROCESS}" "${@}"