@echo OFF

color F0

if .==.%1           goto error1
if not exist %1.nap goto error2

if exist %1.tmp     del %1.tmp
if exist %1.c       del %1.c
if exist %1.exe     del %1.exe
if exist %1.out     del %1.out

:init1

rem  Initialize variables if not already done
call \Simulate\Napados\Dos\UserInit.bat

echo ===============================================================================
echo.

echo %1   **** MAC PREPROCESSOR     running
\Simulate\Macdos\Dos\Macdos.exe -l -c %1.nap > %1.tmp

if errorlevel 1     goto error0
if not exist %1.tmp goto error0

title [ %1.nap ]

echo %1   **** NAPA COMPILER        running

\Simulate\Napados\Dos\Napados.exe %1.tmp -dir /Simulate/Napados -user %OWNER% > %1.c

if errorlevel 1     goto error0
if not exist %1.c   goto error0



gcc    -%TARG% -O2 -D__USE_MINGW_ANSI_STDIO=1 -finline-functions -pipe -std=c11 -fstrict-aliasing -D%SYST% -Wmissing-prototypes -Wredundant-decls -Wunreachable-code -fno-common -lm -c %1.c -o %1.o  -I%2/Hdr
find  "ask4sarc"  %1.o  >> NUL
if %ERRORLEVEL% == 0  goto link2

:link1
rem   linking without the SARC engine
gcc -s -%TARG% -Wall -static -ansi -std=c11 -o %1.exe %1.o
goto end1

:link2
rem   linking  -with- the SARC engine
gcc -s -%TARG% -Wall -static -ansi -std=c11 -o %1.exe %1.o  -static /Simulate/SarcDos/OFile/SARC_api.o  /Simulate/SarcDos/OFile/SARC_matlib.o  -lstdC++

:end1

if errorlevel 1     goto error0
if not exist %1.exe goto error0

rem   token ? to avoid error when building stand alone compilator
echo %1   **** USER'S SIMULATOR     running
%1.exe ?  > %1.out

if exist %1.tmp     del %1.tmp
title [ Job Done ]

goto end

:error0

echo.
echo %1   **** ABNORMAL TERMINATION
color FC
title [ Error ]
pause

color F0

goto end

:error1
echo %1   **** Input file is missing. No Processing.
color FC
title [ No process ]

goto end

:error2
color FC
echo %1   **** Input file %1.nap does not exist. No Processing.
title [ No process ]

:end

if exist %1.o    del %1.o
echo.
