:: =====================================================================================
:: Inka tape image builder
::
:: The output will be a file "tape.tap" that can be run with an empty cartridge inserted
:: to write the machine code portion, the loading screen and all the compressed in-game
:: screens to this cartridge.
::
:: The finalizing steps are:
:: - Copy run.tap to the cartridge by running it from line 9999
:: - Copy inka.tap to the cartridge by running it from line 9999
:: - Save the cartridge somewhere as inka.mdr.
:: =====================================================================================

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:: Constants & file names =============================================
SET BINTAP=.\bintap\Release\BinTap.exe
SET TNIASM=tniasm
SET BAS2TAP=.\bas2tap\Release\bas2tap.exe
SET ZX0=.\zx0\Release\zx0.exe
SET TAPCAT=catalog
SET TAP2MDV=tap2mdv
SET RUN=run
SET TAPE=tape
SET MC=MC
SET MAINPRG=INKA
SET ORG=64537
SET /a CLR=%ORG%-1

:: Prepare ============================================================
DEL /q "%TAPCAT%.bas" >NUL 2>&1
DEL /q "%TAPE%.tap" >NUL 2>&1
DEL /q "%TAP2MDV%.bas" >NUL 2>&1
fsutil file createnew "%TAPE%.tap" 0 >NUL
SET LINE=1000

:: Compile M/C ========================================================
ECHO Compiling machine code...
"%TNIASM%" "%MC%.asm" > NUL
ECHO Creating .tap file...
"%BINTAP%" "%MC%.bin" "%MC%.tap" %MC% %ORG% > NUL
COPY /b "%TAPE%.tap" + "%MC%.tap" "%TAPE%.tap" >NUL
FOR /f "usebackq" %%A IN ('"%MC%.bin"') DO set SIZE=%%~zA
ECHO %LINE% DATA ^"%MC%^",%ORG%,%SIZE% >> "%TAPCAT%.bas"
SET /a LINE=LINE+1
DEL /q "%MC%.tap" >NUL
DEL /q "%MC%.bin" >NUL

:: Loading Screen =====================================================
ECHO Creating loading screen...
"%BINTAP%" .\images\RING$.scr .\images\RING$.tap RING$ 16384 > NUL
COPY /b "%TAPE%.tap" + .\images\RING$.tap "%TAPE%.tap" >NUL
ECHO %LINE% DATA ^"RING$^",16384,4096 >> "%TAPCAT%.bas"
SET /a LINE=LINE+1
DEL /q .\images\RING$.tap >NUL

:: Iterate in-game screens ============================================
FOR /r %%i in (".\images\*.SC_") do CALL :process %%i

ECHO %LINE% DATA ^"#^",0,0 >> %TAPCAT%.bas
SET /a LINE=LINE+1

:: Compile final tape =================================================
ECHO Compiling %TAP2MDV%.bas...
ECHO 10 CLEAR %CLR% > "%TAP2MDV%.bas"
COPY /b "%TAP2MDV%.bas" + "_%TAP2MDV%.bas" + "%TAPCAT%.bas" "~%TAP2MDV%.bas" >NUL
MOVE /y "~%TAP2MDV%.bas" "%TAP2MDV%.bas" >NUL
DEL /q "%TAPCAT%.bas" >NUL
"%BAS2TAP%" -q -s%TAP2MDV% -a0 "%TAP2MDV%.bas" "%TAP2MDV%.tap"
COPY /b "%TAP2MDV%.tap" + "%TAPE%.tap" "~%TAP2MDV%.tap" >NUL
MOVE /y "~%TAP2MDV%.tap" "%TAPE%.tap" >NUL
DEL /q "%TAP2MDV%.tap" >NUL

:: Compile loader =====================================================
ECHO Compiling loader...
ECHO 10 BORDER 0: PAPER 0: INK 6: CLEAR %CLR% > "%RUN%.bas"
COPY /b "%RUN%.bas" + "_%RUN%.bas" "~%RUN%.bas" >NUL
MOVE /y "~%RUN%.bas" "%RUN%.bas" >NUL
"%BAS2TAP%" -q -s%RUN% -a9999 "%RUN%.bas" "%RUN%.tap"

:: Compile game =======================================================
ECHO Compiling game...
"%BAS2TAP%" -q -s%MAINPRG% -a9999 "%MAINPRG%.bas" "%MAINPRG%.tap"

GOTO :EOF


:: Subroutines ========================================================

:process
ECHO Compressing %~n1%~x1...
"%ZX0%" -f "%~1" "%~d1%~p1%~n1.zx0" >NUL

ECHO Converting %~n1%~x1.zx0...
"%BINTAP%" "%~d1%~p1%~n1.zx0" "%~d1%~p1%~n1.tap" "%~n1" 20480 >NUL

ECHO Appending %~d1%~p1%~n1.tap...
COPY /b "%TAPE%.tap" + "%~d1%~p1%~n1.tap" "%TAPE%.tap" >NUL

ECHO Updating %TAPCAT%.bas...
FOR /f "usebackq" %%A IN ('%~d1%~p1%~n1.zx0') DO set SIZE=%%~zA
ECHO %LINE% DATA ^"%~n1^",20480,%SIZE% >> "%TAPCAT%.bas"
SET /a LINE=LINE+1

ECHO Cleaning up...
DEL /q "%~d1%~p1%~n1.zx0" >NUL
DEL /q "%~d1%~p1%~n1.tap" >NUL

GOTO :EOF
