#!/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
#======================================================================

# 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

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->registerNamespace(xhtml => 'http://www.w3.org/1999/xhtml'); \
              \$model->setRelaxNGSchema('$SCHEMA');         \
              \$model->compileSchema;                       \
              print STDERR \$latexml->getStatusMessage.\"\n\";});" \
       > $RELAXNGDIR/$SCHEMA.model

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