#!/bin/bash
# /=====================================================================\ #
# |  compileschema                                                      | #
# | Convert compact RelaxNG (rnc) to various required formats           | #
# |=====================================================================| #
# | support tools for LaTeXML:                                          | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #

#======================================================================
# Convert & Compile LaTeXML's schema from the RelaxNG Compact Form
# generating the rng forms;
# the compiled model list
# & dtd.
#======================================================================

# bash analog of FindBin::RealBin
TOOLSDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# We're assuming this script is in latexml/tools
LATEXMLDIR=$TOOLSDIR/..
# Note that we go to lengths to work in the source lib directory,
# NOT in the blib/lib directory.
# We are pre-compiling schema that will be commited to the repository.
RESOURCEDIR=$LATEXMLDIR/lib/LaTeXML/resources
RELAXNGDIR=$RESOURCEDIR/RelaxNG
DTDDIR=$RESOURCEDIR/DTD

TRANG="trang -C $LATEXMLDIR/lib/LaTeXML/LaTeXML.catalog"

TMP=/tmp/LaTeXML$$
mkdir $TMP

SCHEMA=LaTeXML

# Point to catalog in lib directory (NOT in blib/lib)
export XML_CATALOG_FILES=$LATEXMLDIR/lib/LaTeXML/LaTeXML.catalog

#======================================================================
# Conversion of rnc (easy to write) to rng (easy to use)
# This is the important part.
#======================================================================
echo "Converting $SCHEMA.rnc to $SCHEMA.rng"
# Assuming trang executes trang-20091111
$TRANG $RELAXNGDIR/$SCHEMA.rnc $TMP/$SCHEMA.rng

# Silly trang puts all converted rng for all the modules in the same directory.
# Also mungs up the urn's naming them.
# Move them back into the lib (NOT blib/lib) directory & fix up the URN's

for RNG in $TMP/LaTeXML*.rng; do
  sed \
      -e "s/include href=\"LaTeXML-/include href=\"urn:x-LaTeXML:RelaxNG:LaTeXML-/" \
      -e "s/include href=\"svg/include href=\"urn:x-LaTeXML:RelaxNG:svg:svg/" \
      $RNG > $RELAXNGDIR/$(basename $RNG)
done

for RNG in $TMP/svg*.rng; do
  sed \
      -e "s/include href=\"LaTeXML-/include href=\"urn:x-LaTeXML:RelaxNG:LaTeXML-/" \
      -e "s/include href=\"svg/include href=\"urn:x-LaTeXML:RelaxNG:svg:svg/" \
      $RNG > $RELAXNGDIR/svg/$(basename $RNG)
done

#======================================================================
# Compiling schema to quick loading form.
#======================================================================
echo "Precompiling $SCHEMA.rng schema to $SCHEMA.model"
 perl -I $LATEXMLDIR/blib/lib -MLaTeXML \
       -e "LaTeXML::Core->new(searchpaths=>['$RELAXNGDIR'])       \
              ->withState( sub{                             \
              my \$latexml = \$_[0];                        \
              my \$model = \$latexml->getModel;             \
              \$model->registerNamespace(ltx=>'http://dlmf.nist.gov/LaTeXML');\
              \$model->registerNamespace(svg=>'http://www.w3.org/2000/svg');  \
              \$model->setRelaxNGSchema('$SCHEMA');         \
              \$model->compileSchema;                       \
              print STDERR \$latexml->getStatusMessage.\"\n\";});" \
       > $RELAXNGDIR/$SCHEMA.model

#======================================================================
# Conversion to DTD
# Really ought to just drop this....
#======================================================================
# Trang can't convert the schema to dtd mainly because we're gradually
# combining the defines for models.
# However, if we first simplify and flatten to a single rng, it can convert.
#
# The stylesheet simplification.xsl does the simplification.
# It is by Eric van der Vlist, from
#    http://downloads.xmlschemata.org/relax-ng/utilities/simplification.xsl
# [linked from http://www.relaxng.org]
#
# However, this stylesheet looses the namespace declaration!!!!!
#
# Simplify the rng

#export XML_CATALOG_FILES=$LATEXMLDIR/lib/LaTeXML/LaTeXML.catalog

echo "Simplifying $SCHEMA.rng for dtd"
xsltproc --stringparam out-name $TMP/$SCHEMA-simplified- \
    $TOOLSDIR/dtd-simplification.xsl $RELAXNGDIR/$SCHEMA.rng > $TMP/dummy.log
# The resulting simplified schema is now in /tmp/$SCHEMA-simplified-7-22.rng

echo "Dumbing down the schema for DTD conversion"
xsltproc $TOOLSDIR/dtd-dumbdown.xsl $TMP/$SCHEMA-simplified-7-22.rng > $TMP/$SCHEMA-dumbed.rng

echo "Converting simplified $SCHEMA.rng to $SCHEMA.dtd"
# Now, we can convert to dtd
##trang /tmp/$SCHEMA-dumbed.rng /tmp/$SCHEMA.dtd
$TRANG $TMP/$SCHEMA-dumbed.rng $TMP/$SCHEMA.dtd

echo "Patching $SCHEMA.dtd"
# However, as said, the namespace declaration got lost
# so we'll fix it up...
sed \
   -e "s/xmlns CDATA #FIXED ''/xmlns CDATA #FIXED 'http:\/\/dlmf.nist.gov\/LaTeXML'/" \
   -e 's/\bns1\b/ltx/g' \
   $TMP/$SCHEMA.dtd  > $DTDDIR/$SCHEMA.dtd

#======================================================================
# If ok...
echo "==============================="
echo "If that appears to have worked,"
echo "run make; make test and commit"
#======================================================================
