@echo off
setlocal
set MPFILE=%1
set MPXFILE=%2
set MPXTMP=mpxerr

echo.
echo Running MPtoTeX...
mptotex %MPFILE %MPXTMP.tex >& nul
iff %? ne 0 then
  echo.
  echo Command failed: mptotex %MPFILE.
  goto error
endiff

echo.
echo Running TeX compiler...
keystack ! "q" Enter
%EMTEXDIR\compiler\tex386 \input %MPXTMP >& nul
keystack !
iff %? ne 0 then
  echo.
  echo Command failed: tex %MPXTMP.tex. See %MPXTMP.log.
  goto error
endiff

echo.
echo Running DVItoMP...
dvitomp %MPXTMP.dvi %MPXFILE >& nul
iff %? ne 0 then
  echo Command failed: dvitomp %MPXTMP.dvi.
  move %MPXFILE %MPXTMP.mpx >& nul
endiff

:error
if %? ne 0 pause Press any key to continue...
except (%MPXTMP.log) del %MPXTMP.* >& nul
quit
