P4 enroute to P5.bash
This is a bash shell script for executing all of the various P4 to P5 stylesheets in a row. It is intended for use in Mac OS X, Debian GNU/Linux, or similar system.
#! /bin/bash
# P4_enroute_to_P5.bash
#
# Driver script to automate a large portion of the process to convert
# a TEI P4 (XML) instance into a TEI P5 instance.
#
# This script has been tested on only a Mac OS X and a Debian
# GNU/Linux system. It is not intended to run on any other, but it
# might.
#
# Copyleft 2006 by Syd Bauman and the Text Encoding Initiative
# Consortium
#set -o xtrace
# set up various constants for use later NB: Dot-two.xslt should be LAST!
STYLESHEETS=' Id-to-xml-id.xslt DateStructLess.xslt Change2change.xslt Remove-Default-Attributes.xsl Janus.xslt IDREFs2bareNames.xslt DescAttribute2Element.xslt Dot-two.xslt '
TMP=/tmp
TEMP=$TMP/`basename $0 .bash`_$$
TMPIN=${TEMP}_in
TMPOUT=${TEMP}_out
DEBUG=false
# initialize variables
N=0
time=""
# establish an error exit procedure
function error {
echo "---------"
D=`date "+%FT%T"`
echo "fatal error at $D: $@."
exit 1
}
# subroutine to derive an output filename from the input
# Note: this routine has the side-effect of setting $OUT
function outPath {
case "${1##*.}" in
xml ) OUT=`basename $1 .xml`.tei ;;
teip4 ) OUT=`basename $1 .teip4`.teip5 ;;
p4 ) OUT=`basename $1 .p4`.p5 ;;
tei ) OUT=`basename $1 .tei`.teip5 ;;
* ) OUT="${1}.xml" ;;
esac
}
# subroutine to execute one stylesheet
# presume input is in TMPIN, and return
# the output there. If debugging, also
# leave the output in an audit-trail file.
function transform {
N=$(($N+1))
if [ "$DEBUG" = "true" ] ; then
echo "xsltproc $1 $TMPIN > $TMPOUT"
else
echo "$1 ..."
fi
$time xsltproc $1 $TMPIN > $TMPOUT
if [ "$DEBUG" = "true" ] ; then
echo "leaving output in ${TEMP}_${N}_${1}.out"
cp $TMPOUT ${TEMP}_${N}_${1}.out
fi
mv $TMPOUT $TMPIN
}
# ensure that we have a working xsltproc
echo "Checking for executable \"xsltproc\":"
which xsltproc || error "I could not find an `xsltproc` command to run, so I'm giving up"
# process options
# Currently, there is only 1 option: -d for debug
while getopts ":d" opt; do
case $opt in
d ) DEBUG=true ;;
* ) echo "usge: $0 [-d] path/to/input [path/to/output]"
echo "(Any names can be used if both input & output"
echo "are specified; if input ends in a recognized"
echo "extension, the output is the same file with a"
echo "modified extension:"
echo " IN OUT"
echo ".xml .tei"
echo ".teip4 .teip5"
echo ".p4 .p5"
echo ".tei .teip5"
echo "If input extension is not recognized, output"
echo "has extension '.xml' appended"
exit 1
esac
done
shift $(( $OPTIND -1 ))
# ensure that we have the right number of arguments (1 or 2),
# and create an output filepath
case "$#" in
0 ) error "Input file not specified" ;;
1 ) outPath $1 ;;
2 ) OUT=$2 ;;
* ) error "Extraneous operand(s) ($2 $3 $4 ...)" ;;
esac
# Now that we've handled the arguments, get down to work
if [ "$DEBUG" = "true" ] ; then
which time && time=`which time`
fi
if [ ! -r $1 ] ; then
error "File $1 is not readable"
fi
if [ ! -s $1 ] ; then
error "File $1 is empty"
fi
cp $1 $TMPIN
for s in $STYLESHEETS ; do
if [ -r $s ] ; then
transform $s
else
echo "WARNING: skipping '$s' as I can't find it!!!"
fi
done
mv $TMPIN $OUT