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 simiilar system. #! /bin/bash


 * 1) P4_enroute_to_P5.bash
 * 2) Driver script to automate a large portion of the process to convert
 * 3) a TEI P4 (XML) instance into a TEI P5 instance.
 * 4) This script has been tesetd on only a Mac OS X and a Debian
 * 5) GNU/Linux system. It is not intended to run on any other, but it
 * 6) might.
 * 7) Copyleft 2006 by Syd Bauman and the Text Encoding Initiative
 * 8) Consortium
 * 1) Copyleft 2006 by Syd Bauman and the Text Encoding Initiative
 * 2) Consortium
 * 1) Consortium


 * 1) set -o xtrace

STYLESHEETS=' Id-to-xml-id.xslt DateStructLess.xslt Change2change.xslt Remove-Default-Attributes.xsl Janus.xslt Dot-two.xslt ' TMP=/tmp TEMP=$TMP/`basename $0 .bash`_$$ TMPIN=${TEMP}_in TMPOUT=${TEMP}_out DEBUG=false
 * 1) set up various constants for use later NB: Dot-two.xslt should be LAST!

N=0 time=""
 * 1) initialize variables

function error { echo "-" D=`date "+%FT%T"` echo "fatal error at $D: $@." exit 1 }
 * 1) establish an error exit procedure

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 }
 * 1) subroutine to derive an output filename from the input
 * 2) Note: this routine has the side-effect of setting $OUT

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 }
 * 1) subroutine to execute one stylesheet
 * 2) presume input is in TMPIN, and return
 * 3) the output there. If debugging, also
 * 4) leave the output in an audit-trail file.

echo "Checking for executable \"xsltproc\":" which xsltproc || error "I could not find an `xsltproc` command to run, so I'm giving up"
 * 1) ensure that we have a working xsltproc

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 ))
 * 1) process options
 * 2) Currently, there is only 1 option: -d for debug

case "$#" in    0 ) error "Input file not specified" ;;    1 ) outPath $1 ;; 2 ) OUT=$2 ;;   * ) error "Extraneous operand(s) ($2 $3 $4 ...)" ;; esac
 * 1) ensure that we have the right number of arguments (1 or 2),
 * 2) and create an output filepath


 * 1) 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