#!/bin/csh -f
# mmkg  GELLMU build script for 3 outputs:
#        LaTeX, classic HTML, and XHTML+MathML
# beginning with an XML version of GELLMU "article" conforming
# to the document type definition in the file "axgellmu.dtd"
#
# NOTES:
#
#   0. This driver assumes that the source file is UTF-8; it's
#      good with ascii source but not with latin-1 or some other
#      text-encoding.  For, say, latin-1 either modify this driver
#      or front it with GNU "recode".
#
#        Example:  recode < foolatin1.glm l1..u8 > fooutf8.glm
#           or
#                  recode  l1..u8  foo.glm      (modifies file)
#
#   1. Classic HTML output will have the UTF-8 output encoding.
#
#   2. The LaTeX file will have the UTF-8 output encoding (though
#      not importantly so unless the author has introduced non-ASCII
#      characters in source).  If the non-ASCII characters are used
#      (but not TeX-like stand-ins for them), the LaTeX file will only
#      run successfully through latex when the utf8 input encoding is
#      used.  This script recognizes the environmental variable
#      "GELLMU_LaTeXUTF8" for that purpose.  Of course, one's TeX
#      installation must be equipped with TeX font coverage for
#      characters used.
#
#  IMPORTANT: The sgmlspl script "xmlgart.pl" requires a version of SGMLS.pm
#  (the one file) that has been supplemented to provide the method
#  $element->defempty .
#########################################################

# Turn off use of Universal MathML StyleSheets if next line is uncommented
setenv GELLMU_NoUMSS 1

# Things to check up on locally
#
# IF YOU DON'T HAVE html OR xhtml VALIDATION, change vhtml, xvhtml to ""
#
set gdir=/usr/share/local/xml/gellmu
if ($?GELLMU_Dir) then
  set gdir="$GELLMU_Dir"
endif
set pname="`basename $0`"
if (!((-d "$gdir/xml") && (-x "$gdir/xml"))) then
  echo "${pname}: No subdirectory 'xml' in $gdir"
  exit 1
endif
set xgdir="$gdir/xml"
set emacs=/usr/local/bin/emacs
set onsgmls=/usr/share/local/xml/build/opensp/bin/onsgmls
# set vhtml=""
set vhtml=/usr/local/xml/bin/ovalidhtml
# set xvhtml=""
set xvhtml=/usr/local/xml/bin/ovalidhtml
#
set osversion=`uname -r` # Local issue: looking for 5.9 vs 5.10
if (("$osversion" == "5.10") && (-x "/usr/bin/perl")) then
  set perl=/usr/bin/perl
else if(-x "/usr/local/bin/perl") then
  set perl=/usr/local/bin/perl
else if(-x "/usr/bin/perl") then
  set perl=/usr/bin/perl
else
  set defperl="`which perl`"
  if(-x "$defperl") set perl="$defperl"
endif
if(!(-x "$perl")) then
  echo "${pname}: Cannot find required Perl executable"
  echo "          Maybe you need to use a modified driver"
  exit
endif
setenv PERL5LIB "${gdir}/perllib"
# set texbin="/usr/share/texmf/teTeX/bin/i586-linux-libc6/"
set texbin="/usr/local/tex/bin/"
set kpsewhich="${texbin}kpsewhich"
set latex="${texbin}latex"
set pdflatex="${texbin}pdflatex"
set xmlwf=/usr/local/bin/xmlwf
#
# IF you are able to get good font results with LaTeX's T1 font encoding
# (REQUIRED if your GELLMU source is not Latin-1) do this:
# setenv GELLMU_LaTeXUTF8 1
#
# Location of CSS and XSL(T) style files:
# This is relevant for rendering of HTML and XHTML, whether on the web
# or through the local filesystem.  The value is a URI.
if !($?GELLMU_StyleDir) then
  setenv GELLMU_StyleDir "../webstyle"
endif
#
#########################################################
#
set isok=1
#
set elib="$gdir"
set sgmlspl="${gdir}/perllib/sgmlspl.pl"
set nsgmls="${onsgmls} -l -oempty -c ${gdir}/ucatalog"
set xnsgmls="${onsgmls} -l -oempty -wxml -c ${xgdir}/ocatalog"
set nvxnsgmls="${onsgmls} -l -oempty -wxml -wno-valid -c ${xgdir}/ocatalog"
# For this try to avoid external fetching:
set pxhnsgmls="${onsgmls} -l -wxml -wno-valid -c ${xgdir}/ocatalog"
# This is for OpenSP, version 1.5
setenv SP_CHARSET_FIXED "YES"
setenv SP_ENCODING "UTF-8"
#
# Notify Perl scripts about UTF-8
#
setenv GELLMU_UTF8 "1"
#
# Quietest location for GELLMU LaTeX package is in a texmf tree such as
#    $HOME/texmf/tex/latex/misc   OR    $TEXMFLOCAL/tex/latex/misc
set Gsty="`${kpsewhich} gellmu.sty`"
if ("$Gsty" == "") then
  echo "*** Consider installing gellmu.sty in your local texmf tree"
  setenv GELLMU_LaTeXStyle "${gdir}/style/gellmu"
else
  setenv GELLMU_HaveStyle 1
endif
set xhtmlsuffix=".xhtml"
if ($?GELLMU_XhtmlSuffix) then
  set xhtmlsuffix="$GELLMU_XhtmlSuffix"
endif
set tsname="/tmp/${pname}S$$"
set tpname="/tmp/${pname}P$$"
set txname="/tmp/${pname}X$$"
set tyname="/tmp/${pname}Y$$"

# Behavior of this script -- where it begins -- depends on its name
# Default suffix for the source form of an article
set suffix=".glm"
if ("$pname" =~ *froms) then
  set suffix=".sgml"
endif
if ("$pname" =~ *fromx) then
  set suffix=".xml"
endif

if ( ($#argv == 0) || ($#argv > 2) ) then
  echo "Usage:  $pname  <stem-name>"
  echo "  to build a GELLMU article named  <stem-name>$suffix"
  exit 1
endif

set stem="${1}"

if ("$suffix" == ".glm") then
# Translate LaTeX-like article source to SGML

if(!(-r "${stem}${suffix}") && (-r "$stem")) then
  if("$stem" =~ *.glm) then
    set found="$stem"
    set stem=`echo "$found" | sed -e 's/\.glm//'`
  else
    echo "${pname}: No file named $stem"
    exit 1
  endif
endif

echo ""
echo "Source in ${stem}${suffix}"

set fncall=gellmu-trans
if($#argv == 2) then
  set fncall="$2"
else
  set firstline="`head -1 ${stem}${suffix}`"
  echo "$firstline" | grep -q '^%\!'
  if($status == 0) then
    echo "${pname}: Function call from source file"
    echo "   which reads ${firstline}"
    set fncall=`echo "$firstline" | sed -e 's/^..//'`
  endif
endif
echo "${pname}: Using gellmu.el function $fncall"
if !("$fncall" =~ gellmu-*) then
  set fncall="gellmu-${fncall}"
endif
$emacs -batch --no-site-file -l "$elib/gellmu" \
  --eval '(progn (setq coding-system-for-read (quote utf-8)) \
  (setq coding-system-for-write (quote utf-8)))' -f "$fncall" ${stem}${suffix}
if !($status == 0) then
  echo "${pname}:  GELLMU Syntactic Translator did not finish."
  exit 1
endif

# End of translation from LaTeX-like source to SGML
endif

#
set encaltlib="${gdir}/perlaltlib"
set enclibname="${encaltlib}/encoding.pm"
set perlv=`${perl} -e '$v=$];chomp $v;if($v<5.006001){print "A"}elsif($v>=5.008001){print "B"}else{print "C"};'`
if ("$perlv" == "A") then
  echo "Perl version code ${perlv}: version less than 5.6.1"
  echo "WARNING: UTF-8 support may not be adequate with this version of Perl"
else
  if ("$perlv" == "B" ) then
    echo "Perl version code ${perlv}: version at least 5.8.1"
    if (-f "$enclibname") then
      echo "File ${enclibname} should NOT be used."
    else
      echo "Correctly do not  have $enclibname"
    endif
  else
    if ("$perlv" == "C" ) then
      echo "Perl version code ${perlv}: at least 5.6.1 but less than 5.8.1"
      if !(-f "$enclibname") then
        echo "Dubious GELLMU installation:"
        echo "   file ${enclibname} appears to be missing"
      else
        echo "Correctly have $enclibname"
        setenv PERL5LIB "${encaltlib}:$PERL5LIB"
      endif
    else
        echo "WARNING: Perl version code, ${perlv}, not recognized."
    endif
  endif
endif
#
echo "PERL5LIB is ${PERL5LIB}"
if ( ("$suffix" == ".glm") || ("$suffix" == ".sgml") ) then
  chmod 644 "${stem}${suffix}" "${stem}.sgml"
  echo ""
  echo "Validating parse of SGML for making author-level XML"
  $nsgmls "${stem}.sgml" > "$tsname"
  if($status != 0) then
    echo "${pname}: Parsing errors on ${stem}.sgml"
    exit 2
  endif
  rm -f "${stem}.xml~"
  if(-r "${stem}.xml") mv "${stem}.xml" "${stem}.xml~"
  $perl < "$tsname" "${sgmlspl}" "${gdir}/xplaingart.pl" > "${stem}.xml"
  if ($status != 0) then
    echo "${pname}: sgmlspl failed on ${stem}.sgml with"
    echo "    ${gdir}/xplaingart.pl"
    exit 3
  endif
  rm "$tsname"
endif

chmod 644 "${stem}.xml"
echo ""
echo "Parsing author-level XML to make elaborated XML"
$xnsgmls "${stem}.xml" > "$tpname"
if($status != 0) then
  echo "${pname}: Parsing errors on ${stem}.xml"
  exit 2
endif

rm -f "${stem}.exml~"
if(-r "${stem}.exml") mv "${stem}.exml" "${stem}.exml~"
$perl < "$tpname" "${sgmlspl}" "${gdir}/xmlgart.pl" > "${stem}.exml"
if ($status != 0) then
  echo "${pname}: sgmlspl failed on ${stem}.exml with"
  echo "    ${gdir}/xmlgart.pl"
  exit 3
endif
chmod 644 "${stem}.exml" "${stem}.xcn" "${stem}.xet" "${stem}.xlb"
echo ""
echo "Validating parse of elaborated XML"
$xnsgmls "${stem}.exml" > "$txname"
if ($status != 0) then
  echo "${pname}: validating parse of ${stem}.exml failed"
  exit 4
endif
rm "$tpname"

echo ""
echo "Making Classical HTML"
rm -f "${stem}.html~"
if(-r "${stem}.html") mv "${stem}.html" "${stem}.html~"
$perl < "$txname"  "${sgmlspl}" ${gdir}/htmlgart.pl > "${stem}.html"
chmod 644 "${stem}.html"
if !($status == 0) set isok=0
if($vhtml != "") then
  echo ""
  echo "Validating HTML"
  $vhtml "${stem}.html"
  if !($status == 0) set isok=0
endif

echo ""
echo "Making LaTeX"
rm -f "${stem}.ltx~"
if(-r "${stem}.ltx") mv "${stem}.ltx" "${stem}.ltx~"
$perl < "$txname" "${sgmlspl}" ${gdir}/ltxgart.pl > "${stem}.ltx"
chmod 644 "${stem}.ltx"
$latex "${stem}.ltx"
grep -q ' Rerun to get ' "${stem}.log"
if ($status == 0) $latex "${stem}.ltx"
grep -q ' Rerun to get ' "${stem}.log"
if ($status == 0) $latex "${stem}.ltx"
if !($status == 0) set isok=0
chmod 644 "${stem}.dvi"
if !($status == 0) set isok=0

if ((-f "${stem}.prx") && (-r "${stem}.prx")) then
  foreach k (`cat "${stem}.prx"`)
    mv "${k}" "${k}prx"
  end
endif
$pdflatex "${stem}.ltx"
if ((-f "${stem}.prx") && (-r "${stem}.prx")) then
  foreach k (`cat "${stem}.prx"`)
    mv "${k}prx" "${k}"
  end
endif

if !($status == 0) set isok=0
chmod 644 "${stem}.pdf"
if !($status == 0) set isok=0

# If you have no interest in XHTML+MathML, exit here
# exit 0
#
if (!("$pname" =~ *mm*)) then
  echo "${pname}: Omitting XHTML+MathML output"
  if ($isok == 1) then
    echo "  Done"
  endif
  exit 0
endif

# Now toward XHTML + MathML
echo ""
echo "Crude parse of CDATA in XML version"
rm -f "${stem}.yml~"
if(-r "${stem}.yml") mv "${stem}.yml" "${stem}.yml~"
$perl < "$txname" "${sgmlspl}" ${gdir}/mathcdata.pl > "${stem}.yml"
if !($status == 0) set isok=0
chmod 644 "${stem}.yml"
if !($status == 0) set isok=0
$xnsgmls "${stem}.yml" > "$tyname"
if ($status != 0) then
  echo "${pname}: validating parse of ${stem}.yml failed"
  exit 5
endif
if($isok == 1) then
  rm "$txname"
endif

echo ""
echo "Expanding math in XML version"
rm -f "${stem}.zml~"
if(-r "${stem}.zml") mv "${stem}.zml" "${stem}.zml~"
$perl < "$tyname" "${sgmlspl}" ${gdir}/mathprep.pl > "${stem}.zml"
if !($status == 0) set isok=0
chmod 644 "${stem}.zml"
if !($status == 0) set isok=0
$xmlwf "${stem}.zml"
if !($status == 0) then
  echo "${pname}: conformance check on ${stem}.zml failed"
  exit 6
endif
if($isok == 1) then
  rm "$tyname"
endif

echo ""
echo "Making XHTML"
if(-r "${stem}${xhtmlsuffix}~") then
  rm -f "${stem}${xhtmlsuffix}~"
endif
if(-r "${stem}.xhtml~") then
  rm -f "${stem}.xhtml~"
endif
if(-r "${stem}${xhtmlsuffix}") then
  mv "${stem}${xhtmlsuffix}" "${stem}${xhtmlsuffix}~"
else
  if(-r "${stem}.xhtml") then
  mv "${stem}.xhtml" "${stem}${xhtmlsuffix}~"
  endif
endif
$nvxnsgmls "${stem}.zml" \
 | $perl "${sgmlspl}" ${gdir}/htmlgart.pl -mathml > "${stem}${xhtmlsuffix}"
if !($status == 0) set isok=0
chmod 644 "${stem}${xhtmlsuffix}"
if !($status == 0) set isok=0
$xmlwf "${stem}${xhtmlsuffix}"
if !($status == 0) then
  echo "${pname}: conformance check on ${stem}${xhtmlsuffix} failed"
  exit 7
endif
echo ""
echo "Checking for MathML glitches"
$pxhnsgmls "${stem}${xhtmlsuffix}" | perl "${sgmlspl}" ${gdir}/mval.pl
if !($status == 0) set isok=0
if ($xvhtml != "") then
  echo ""
  echo "Validating XHTML"
  $xvhtml "${stem}${xhtmlsuffix}"
  if !($status == 0) set isok=0
endif
if($isok == 1) then
  echo "${pname} done"
endif
