/*
 * This AREXX script simulates the UNIX-/bin/sh script of the same name.
 */

Options FailAt 21 /* Handle all error levels in this script */

/* Pragma('Directory','.') *//* You should have Martin Scott's `UnixDirsII' */

/*
 * MPLIB is where newer, mptotex, and dvitomp are installed.
 */
MPLIB = "TeXMF:bin/web2c61"
DOTEX = "virtex"

/*****************************************************
 * Everything below here should seldom need changing *
 *****************************************************/
ERRLOG = "mpxerr.log" /* file for an error log if necessary */
TEXERR = "mpxerr.tex" /* file for erroneous TeX file if any */
DVIERR = "mpxerr.dvi" /* troublesome dvi file if any */

Parse Arg MPFILE MPXFILE . /* input and output files */

MPX = "mpx" || time(seconds) /* Simulate `$$' = task number */
NL = '0A'X /* `new line' character */

Address Command "delete force quiet >NIL:" MPX || ".#?" ERRLOG TEXERR DVIERR

Address Command MPLIB || "/newer" MPFILE MPXFILE

If 0 = RC Then Do /* 0 = TRUE in UNIX */

   Address Command MPLIB || "/mptotex" MPFILE ">" MPX || ".tex -E" || ERRLOG

   If 0 ~= RC Then Do
      Say NL || "Command failed:" MPLIB || "/mptotex" MPFILE
      Address Command "type" ERRLOG
      Address Command "delete force quiet" MPX || ".tex"
      Exit 1
   End

   Address Command DOTEX "\batchmode \input" MPX "> NIL:"

   If 0 ~= RC Then Do
      Address Command "copy" MPX || ".tex" TEXERR
      Address Command "copy" MPX || ".log" ERRLOG
      Say NL || "Command failed:" DOTEX TEXERR NL || "See" ERRLOG
      Address Command "delete force quiet" MPX || ".#?"
      Exit 2
   End

   Address Command MPLIB || "/dvitomp" MPX || ".dvi" MPXFILE ">" ERRLOG

   If 0 ~= RC Then Do
      Address Command "copy" MPX || ".dvi" DVIERR
      Say NL || "Command failed:" MPLIB || "/dvitomp" DVIERR
      Address Command "type" ERRLOG
      Address Command "delete force quiet" MPX || ".#?"
      Exit 3
   End

   Address Command "delete force quiet" MPX || ".#?" ERRLOG

End

Exit 0
