#!/bin/csh -f
set pname="`basename $0`"
#  You will need to edit this for names and paths.
#
#  IMPORTANT: The sgmlspl script "xxmlgart.pl" requires the enclosed
#  version of SGMLS.pm (the one file) that has been supplemented to provide
#  the method  $element->defempty .
#
# By gross default assume that this is run from a sub-sub-directory of the
#   unpacked tree root.
# Or one may set the environmental GELLMU_Dir
set emacs="emacs"
set nsgmls="nsgmls"
set sgmlspl="sgmlspl"
set latex="latex"
if ($?GELLMU_Dir) then
  set gdir="$GELLMU_Dir"
else if ( "$0" =~ *gellmu069n* ) then
  set gdir=`echo "$0" | sed -e 's/gellmu069n.*$/gellmu069n/'`
else if ( "`pwd`" =~ *gellmu069n* ) then
  set gdir=`pwd | sed -e 's/gellmu069n.*$/gellmu069n/'`
else
  # default
  set gdir="/math/local/lib/gellmu"
endif
echo "Using as GELLMU directory: $gdir"
if !( (-d "$gdir") && (-x "$gdir") ) then
  echo "${pname}: Cannot find root of GELLMU tree."
  echo "  ($gdir  is not available)."
  echo "  Either edit this script or set GELLMU_Dir in the environment."
  exit
endif
if (-x "/usr/local/xml/bin/validhtml") then
  set vhtml="/usr/local/xml/bin/validhtml"
else if (-x "/usr/local/bin/validhtml") then
  set vhtml="/usr/local/bin/validhtml"
else
  set vhtml=""
endif
if !($?GELLMU_AdminUrl) setenv GELLMU_AdminUrl "http://www.dev.null.org/~webmaster/"
if !($?GELLMU_CommentUrl) setenv GELLMU_CommentUrl "mailto:webadmin@mail.dev.null.org"
if !($?GELLMU_MadeUrl) setenv GELLMU_MadeUrl "mailto:user@mail.dev.null.org"
#
# Edits should not be required below here.
#
if (!((-d "$gdir/xml") && (-x "$gdir/xml"))) then
  echo "${pname}: No subdirectory 'xml' in $gdir"
  exit 1
endif
set xgdir="$gdir/xml"
set nsgmlsc="$nsgmls -l -oempty -c ${gdir}/catalog"
set xnsgmlsc="$nsgmls -l -oempty -wxml -c ${xgdir}/catalog"
set suffix=".glm"  # for GELLMU source
set elib="$gdir"
# This is for James Clark's version of SP, version 1.3
#   Version 1.3 handles two byte unicode -- see "gellmu.decl" (where the
#     charset code is taken from Clark's "declaration" for XML)
# For now I believe SGMLS.pm to handle ISO-8859-1
#   probably also other (unibyte) forms of ISO-8859-*
setenv SP_CHARSET_FIXED "YES"
setenv SP_ENCODING "ISO-8859-1"
#
setenv PERL5LIB "$gdir/perllib"
set tname="/tmp/${pname}$$"
if (($#argv == 0) || ($#argv > 2)) then
  echo "Usage:  $pname  <stem-name>"
  echo "  to build a GELLMU source file named  <stem-name>$suffix"
  exit
endif
set fncall="gellmu-trans"
set isok=1
if($#argv == 2) then
  set fncall="$2"
  if !("$fncall" =~ gellmu-*) then
    set fncall="gellmu-${2}"
  endif
endif
if !((-f "$elib/wgellmu.elc") && (-r "$elib/wgellmu.elc")) then
  emacs -batch -f batch-byte-compile "$elib/gellmu.el"
  # The normal name gellmu.elc is not safe in win32
  mv "$elib/gellmu.elc" "$elib/wgellmu.elc"
endif
set stem="$1"
set fname="${stem}$suffix"
if !((-r "$fname") && (-f "$fname")) then
  echo "${pname}: No $fname"
  if !((-r "${stem}") && (-f "${stem}")) then
    echo "${pname}: Cannot locate either ${stem} or $fname"
    exit
  else
    echo "${pname}: Found ${stem}"
    set fname="${stem}"
    # Too much sweat here to use $suffix; assume ".glm"
    set stem=`echo "$fname" | sed -e 's/\.glm$//'`
    echo "Using stem name: ${stem}"
  endif
endif
$emacs -batch -l "$elib/wgellmu.elc" -f "$fncall"  "${stem}$suffix"
if !($status == 0) then
  echo "${pname}:  GELLMU Syntactic Translator did not finish."
  exit 1
endif
chmod 644 "${stem}${suffix}" "${stem}.sgml"
echo "Making XML from ${stem}.sgml"
rm -f "${stem}.xml~"
if(-r "${stem}.xml") mv "${stem}.xml" "${stem}.xml~"
$nsgmlsc "${stem}.sgml" | $sgmlspl "${gdir}/xmlgart.pl" > "${stem}.xml"
if ($status != 0) then
  echo "${pname}: $sgmlspl with ${gdir}/xmlgart.pl may have failed"
  echo "    on ${stem}.sgml -- possibly a parsing error.  Trying to continue."
endif
chmod 644 "${stem}.xml" "${stem}.xcn" "${stem}.xet" "${stem}.xlb"
echo "Validating parse of XML"
$xnsgmlsc "${stem}.xml" > "$tname"
if ($status != 0) then
  echo "${pname}: validating parse of ${stem}.xml failed"
  exit 2
endif
echo "Making HTML"
rm -f "${stem}.html~"
if(-r "${stem}.html") mv "${stem}.html" "${stem}.html~"
$sgmlspl < "$tname" ${gdir}/htmlgart.pl > "${stem}.html"
chmod 644 "${stem}.html"
if !("$vhtml" == "") then
  echo "Validating HTML"
  $vhtml "${stem}.html"
else
  echo "Note: Not set up for validation of HTML."
endif
if !($status == 0) set isok=0
echo "Making LaTeX"
rm -f "${stem}.ltx~"
if(-r "${stem}.ltx") mv "${stem}.ltx" "${stem}.ltx~"
$sgmlspl < "$tname" ${gdir}/ltxgart.pl > "${stem}.ltx"
chmod 644 "${stem}.ltx"
if ($status == 0) rm "$tname"
latex "${stem}.ltx"
if !($status == 0) then
  set isok=0
  echo "${pname}: latex exited with an error code."
  exit 1
endif
grep -q ' Rerun to get ' "${stem}.log"
if !($status == 0) then
  chmod 644 "${stem}.dvi"
  if($isok == 1) echo "OK\!"
  exit
endif
latex "${stem}.ltx"
if !($status == 0) then
  set isok=0
  echo "${pname}: latex exited with an error code."
  exit 1
endif
grep -q ' Rerun to get ' "${stem}.log"
if !($status == 0) then
  chmod 644 "${stem}.dvi"
  if($isok == 1) echo "OK\!"
  exit
endif
latex "${stem}.ltx"
if !($status == 0) then
  set isok=0
  echo "${pname}: latex exited with an error code."
  exit 1
endif
grep -q ' Rerun to get ' "${stem}.log"
if !($status == 0) then
  chmod 644 "${stem}.dvi"
  if($isok == 1) echo "OK\!"
  exit
endif
latex "${stem}.ltx"
if !($status == 0) then
  set isok=0
  echo "${pname}: latex exited with an error code."
  exit 1
endif
grep -q ' Rerun to get ' "${stem}.log"
if !($status == 0) then
  chmod 644 "${stem}.dvi"
  if($isok == 1) echo "OK\!"
  exit
endif