@echo off 

if not exist \Emu\UKNCBTL\UKNCBTL.exe goto EmulatorFail

set BuildFile=%1
set BuildPath=%2
if exist \utils\SelectedBuildFile.bat call \utils\SelectedBuildFile.bat 
cd %BuildPath%
if not %BuildPath:~0,1%==%CD:~0,1% goto InvalidPath
set BuildFile=%BuildFile: =%
set myasm=%BuildFile%
set lencheck=%myasm:~,-4%
set lencheck=%lencheck:~6,1%
if not "%lencheck%"=="" goto LengthError

@if exist MAIN.LST del MAIN.LST
@if exist MAIN.OBJ del MAIN.OBJ
@if exist MAIN.SAV del MAIN.SAV
@if exist MAIN.BIN del MAIN.BIN
@if exist OUTPUT.MAP del OUTPUT.MAP

rem "\Utils\R11\rt11.exe" MACRO/NOOBJECT/LIST:DK:/EN:ABS %BuildFile%
"\Utils\R11\rt11.exe" MACRO/LIST:DK: %BuildFile%

if not "%errorlevel%"=="0" goto AsmFail




rem cd M:\Utils\PDPpy11\PDPy11-master
rem ..\python -m pdpy11 M:\Sources\%1 --sav -o M:\Sources\MAIN.SAV --lst
@echo on
"\Utils\R11\rt11.exe" LINK %myasm:~,-4%  /MAP:OUTPUT.MAP
@if not "%errorlevel%"=="0" goto Abandon
@if exist OUTPUT.MAP type OUTPUT.MAP
\Utils\R11\Sav2Cart.exe %myasm:~,-4%.SAV \RelUKN\CART.BIN


move \sources\*.MAP \BldUKN
move \sources\*.obj \BldUKN
move \sources\*.lst \BldUKN
move \sources\*.sav \RelUKN
move \sources\*.bin \RelUKN

\Emu\UKNCBTL\UKNCBTL.exe /boot2
exit
:LengthError
@echo off
echo *** Don't give your file a name longer than 6 letters - it will make R11 angry! ***
echo eg: abcdef.asm is OK
goto Abandon

:AsmFail
@if not exist %myasm:~,-4%.Lst goto done
\Utils\CS_R11ListErrors.exe %myasm:~,-4%.Lst
goto Abandon

:InvalidPath
echo Error: ASM file must be on same drive as devtools 
echo File: %BuildPath%\%BuildFile% 
echo Devtools Drive: %CD:~0,1% 
goto Abandon
:EmulatorFail
echo Error: Can't find \Emu\UKNCBTL\UKNCBTL.exe
:Abandon
if "%3"=="nopause" exit
pause
