@echo off

REM  RSDN.bat copy of rsdv.bat 
REM  Prestartnopsx RSD file (3D object data) in No$PSX
REM  Put this file where rsdv.bat is, ie c:\psx\psx\bin

REM v5 MarbleClay RSD Export/Obj batch script version 2 (May 2023) - added 3rd param startnopsx
REM v4 MarbleClay export version
REM v3 added scale
REM v2 added NY home env var check
REM v1 working

REM use NY var if set or default to NY installation 
if "%HOME%"=="" (set PSXBIN=c:\psx\psx\bin) else (set PSXBIN=%HOME%\bin)

set TMDINFO=%PSXBIN%\tmdinfo.exe
set RSDLINK=%PSXBIN%\rsdlink.exe
set TMDSORT=%PSXBIN%\tmdsort.exe
set PACKDATA=%PSXBIN%\packdata.exe
set DLOAD=%PSXBIN%\dload.exe
set LOAD=%PSXBIN%\load.exe
set WAIT=%PSXBIN%\wait.bat
set GO=%PSXBIN%\go
set SIOCONS=%PSXBIN%\siocons
set RSDVIEW=%PSXBIN%\rsdview
set scale=1.0
set startnopsx=1

if exist %1.TMD  del %1.TMD
if exist %1U.TMD  del %1U.TMD
if exist YARDATA.PCK  del YARDATA.PCK
if exist YARBATCH.SIO  del YARBATCH.SIO
if exist %1.EXE  del %1.EXE
if exist %1.TXT  del %1.TXT

IF NOT "%~2"=="", set scale=%~2

IF NOT "%~3"=="", set startnopsx=%~3

if exist "%1.rsd" goto RSDLINK
echo Error %*
echo rsdn.bat is a copy of rsdv.bat but lauches No$PSX instead of siocons.
echo Usage: rsdn yourRSDfile [scale] [launchNo$PSX]
echo Where 
echo       yourRSDfile is the rsd file without .rsd
echo       scale is a float value, default is 1.0. 
echo       launchNo$PSX is either 1 or 0, default is 1 to lauch emulator, 0 does not.
goto END

:RSDLINK
echo Starting %*
echo Starting %*  > %1.TXT
echo Converting RSD to TMD. 
echo Converting RSD to TMD. >> %1.TXT

echo Confirm latest versions are be used: Rsdlink v3.72 Tmdsort v1.25 

echo %RSDLINK% 
%RSDLINK% 

echo %TMDSORT% 
%TMDSORT% 

echo .
echo .
REM more complicated command can go here, see rsdform(moving) and rsdcat (joining)
echo %RSDLINK% -v -s %scale% -o %1U.TMD %1.rsd >> %1.TXT
%RSDLINK% -v -s %scale% -o %1U.TMD %1.rsd >> %1.TXT

echo %TMDSORT%  -o %1U.TMD  %1U.TMD  >> %1.TXT
%TMDSORT% -o %1.TMD  %1U.TMD  >> %1.TXT

if not errorlevel 1 goto PACKDATA
echo RSDLINK failed; abort!
echo RSDLINK failed; abort! >> %1.TXT
goto END

:PACKDATA
echo Packing TMD and TIM to create PCK ...
echo Packing TMD and TIM to create PCK >> %1.TXT
echo %PACKDATA% -o YARDATA.PCK %1.rsd %1.TMD
%PACKDATA% -o YARDATA.PCK %1.rsd %1.TMD >> %1.TXT

if not errorlevel 1 goto DLOAD
echo PACDATA failed; abort!
echo PACDATA failed; abort! >> %1.TXT
goto END

:DLOAD
echo Creating yaroze siocons batch YARBATCH.SIO
echo Creating yaroze siocons batch YARBATCH.SIO >> %1.TXT
echo %DLOAD% -nYARDATA.PCK -a80100000 >> %1.TXT
echo local dload  YARDATA.PCK 80100000 > YARBATCH.SIO
echo local load  %RSDVIEW% >> YARBATCH.SIO
echo go >> YARBATCH.SIO

if not errorlevel 1 goto LOAD
echo DLOAD failed; abort!
echo DLOAD failed; abort! >> %1.TXT
goto END

:LOAD
echo Loading program...
echo Loading program. >> %1.TXT
echo ---- YARBATCH.SIO ---- >> %1.TXT
type YARBATCH.SIO >> %1.TXT
echo ---- YARBATCH.SIO ---- >> %1.TXT
type YARBATCH.SIO
echo yarexe YARBATCH.SIO >> %1.TXT
yarexe YARBATCH.SIO  >> %1.TXT
move /Y psx.exe %1.EXE >>  %1.TXT

if not errorlevel 1 goto GO
echo LOAD failed; abort!
echo LOAD failed; abort! >> %1.TXT
goto END

:GO
REM UNCOMMENT TO LOG OBJECT DETAILS
REM %RSDLINK% -info %1.rsd  >> %1.TXT
REM %TMDINFO% -v  FINAL.TMD >> %1.TXT

REM  if 3param 0, skip nopsx
IF %startnopsx%==0,GOTO END

echo  no$psx %1.EXE
echo  no$psx %1.EXE >> %1.TXT
no$psx  %1.EXE


:END
