@echo off

set PREP=n

goto begin

:Usage
echo Usage: tc file
goto end

:nofile
echo %NAME%: no such file
goto end

:preserve
echo preserving %1.s
goto end

:error
if exist TXPP.ERR del TXPP.ERR
if exist TXTRN.ERR del TXTRN.ERR
if exist TXCG.ERR del TXCG.ERR
echo terminating.
goto end

:begin
if exist TXPP.ERR del TXPP.ERR
if exist TXTRN.ERR del TXTRN.ERR
if exist TXCG.ERR del TXCG.ERR

if x%1 == x goto Usage

if not x%1 == x-P goto noP
set PREP=y
shift
:noP

if x%1 == x goto Usage

set NAME=%1.t
if not exist %NAME% goto nofile
if exist %1.s goto preserve

if %PREP% == n goto noprep
echo preprocessing %NAME% - %1.pp
..\build\txx ..\build\txpp.tco <%NAME% >%1.pp
if exist TXPP.ERR goto error
echo compiling %1.pp - %1.tco
..\build\txx ..\build\txtrn.tco <%1.pp >%1.tco
if exist TXTRN.ERR goto error
del %1.pp
echo generating %1.tco - %1.s
..\build\txx ..\build\txcg86a.tco <%1.tco >%1.s
if exist TXCG.ERR goto error
del %1.tco
if errorlevel == 1 goto error
goto endprep

:noprep
echo compiling %1.t - %1.tco
..\build\txx ..\build\txtrn.tco <%NAME% >%1.tco
if exist TXTRN.ERR goto error
echo generating %1.tco - %1.s
..\build\txx ..\build\txcg86a.tco <%1.tco >%1.s
if exist TXCG.ERR goto error
del %1.tco
if errorlevel == 1 goto error

:endprep

echo assembling %1.s - %1.o
..\build\txx ..\build\s86b.tco <%1.s >%1.o
del %1.s

echo loading %1.x
echo -o %1.x >__ldctrl
echo %1.o >>__ldctrl
echo ../build/libtx86.o >>__ldctrl
..\build\txx ..\build\sld.tco <<__ldctrl
del __ldctrl
del %1.o

echo converting %1.x - %1.exe
..\build\txx ..\build\cvexe.tco <%1.x >%1.exe
del %1.x

:end
set NAME=
set PREP=
