@echo off

setlocal
:Environment changes made after setlocal are local to the batch file.
:Cmd.exe restores previous settings when it either encounters an endlocal command or reaches the end of the batch file.
:This feature allows keeping long names in current dir variable (%CD%,%~dp0,%~f0) and thus in prompt
:even if they are switched to short because of running 'old' DOS executable like AS65.

:use this line if assembler executable in project's main folder
:set asmexe=%CD%\mads.exe
:set asmexe=..\mads.exe

:use this line if assembler executable on system path
set asmexe=mads.exe

cd src

set xexname=basstest
if not "%1"=="" (
	set xexname=%1
)
if not "%2"=="" (
	set labeldef=-d:%2
)
call :ASM bassTest %xexname% %labeldef%
if errorlevel 1 goto :EOF

echo Assembly OK.            

goto :EOF

:ASM
echo Assembling '%1.asm'
call %asmexe% %1.asm -l:..\%2.lst -o:..\%2.xex %3
call :HND_ERRORS
goto :EOF

:ASM_BIN
echo Assembling '%1.asm'
call %asmexe% %1.asm -l:..\%1.lst -o:..\%1.bin -d:bin
call :HND_ERRORS
goto :EOF

:HND_ERRORS
if errorlevel 3 goto INV_PARAM
if errorlevel 2 goto ASM_ERR
if errorlevel 1 goto OTHER_ERR
goto :EOF

:ASM_ERR
echo.
echo Assembly gave errors.
goto :EOF

:INV_PARAM
echo Incorrect parameter specified on the commandline.
goto :EOF

:OTHER_ERR
echo Unexpected error occurred.
goto :EOF
