#!/bin/sh
# dtdparseweave - weaves manually edited descriptions into the machine-generated dtdparse output files.
# Markus Hoenicka 1999-08-16
# $Id: dtdparseweave,v 1.1.2.1 2005/10/18 19:59:53 mhoenicka Exp $

# initialize variables
VERBOSE=f

# scan options
while getopts ":hv" opt; do
    case $opt in
	h  ) echo 1>&2 "USAGE: dtdparseweave [-h] [-v] dirname"
	     echo 1>&2 "Weaves hand-coded fragments into dtdparse-generated output."
	     echo 1>&2 "Command-line options:"
	     echo 1>&2 "-h: invokes this help text"
	     echo 1>&2 "-v: verbose"
	     exit 0 ;;
	v  ) VERBOSE=t;;
	\? ) echo 'usage: dtdparseweave [-h] [-v] dirname'
	     exit 1 ;;
    esac
done

# correct the index so the filename argument is always $1
shift $(($OPTIND - 1))

# test for correct number of arguments; should be exactly one directory name
if [ $# -ne 1 ]; then
         echo 1>&2 "Usage: dtdparseweave [-h] [-v] dirname"
         echo 1>&2 "type dtdparseweave -h to show help"
         exit 127
fi

# test for the existence and accessibility of the specified directory
if [ ! -d $1 ]; then
    echo 1>&2 "$1 does not exist or cannot be accessed."
    exit 1
fi

# copy the dtdparse-generated files to a different folder
cp -r $1/elements $1/olements

cd $1/ele-desc

for filename in *.html; do
    if [ $VERBOSE = t ]; then
	echo "weaving $filename ..."
    fi
    # extract first part of dtdparse-generated file
    sgrep '"<HTML>".."<H2>Description</H2>"' ../olements/$filename > $$.tmp

    # extract description
    sgrep '"<h2>Description</h2>"__"<h2>Example</h2>"' $filename >> $$.tmp

    # extract second part of dtdparse-generated file
    sgrep '"<H2>Description</H2>"__"<P></P>"' ../olements/$filename >> $$.tmp

    # extract example
    sgrep '"<h2>Example</h2>"._"</body>"' $filename >> $$.tmp

    # extract third part of dtdparse-generated file
    sgrep '"<P></P>".."</HTML>"' ../olements/$filename >> $$.tmp

    # make it look nice
    tidy -q < $$.tmp > ../elements/$filename
done

# clean up
rm $$.tmp
rm -r ../olements

# jump back to origin
cd -

exit 0