@echo off & setlocal EnableDelayedExpansion

set pathtmp=%path%
set C_INCLUDE_PATH_TMP=%C_INCLUDE_PATH%
set CPLUS_INCLUDE_PATH_TMP=%CPLUS_INCLUDE_PATH%

IF NOT EXIST C:\Saturn~1\SET_ELF.BAT goto satorb_alternate

@REM Set SaturnOrbit environment from normal installation directory (C:\SaturnOrbit\)
echo Set SaturnOrbit environment from C:\SaturnOrbit\
CALL ..\libgcc_path.bat
COPY C:\Saturn~1\SET_ELF.BAT .\TMP.BAT
set ip_path=C:/SaturnOrbit/COMMON/IP_GAS/IP_elf.BIN
goto satorb_end

:satorb_alternate
@REM Set SaturnOrbit environment from alternate installation directory (D:\dev\SaturnOrbit\)
@REM Useful when your C: drive is nearly full.
echo Set SaturnOrbit environment from D:\dev\SaturnOrbit\
CALL ..\libgcc_path.bat alternate
COPY D:\dev\Saturn~1\SET_ELF.BAT .\TMP.BAT
set ip_path=D:/dev/SaturnOrbit/COMMON/IP_GAS/IP_elf.BIN
goto satorb_end

:satorb_end
IF NOT EXIST TMP.BAT exit
CALL TMP.BAT
DEL TMP.BAT


REM Set the project name (= the name of the folder where this batch file is)
set pj_name=SCVBTtest

REM No command line parameter
If "%1%" == "" goto start

REM [Before processing]Command line parameter : parse it and exit.
set cm=%1%
goto parsecommand


:start
REM [After processing]If command line parameter, exit
If NOT "%1%" == "" goto end



echo --------------------------------------------
echo  Type `e' to end.
echo  Type `b' to burn iso.
echo  Type `r' to rebuild program.
echo  Type `rr' to rebuild program in release mode.
echo  Type `t' to test program under yabause.
echo  Type `s' to upload program to Saturn.
set cm=dummy
set /P cm="Command: "

:parsecommand
If "%cm%" == "e" goto end
If "%cm%" == "b" goto burn
If "%cm%" == "r" goto rebuild_debug
If "%cm%" == "rr" goto rebuild_release
If "%cm%" == "t" goto test
If "%cm%" == "s" goto upload
If "%cm%" == "s" goto uploadstable
goto start


:burn
cdrecord dev=1,0,0 speed=8 -v -data release\%pj_name%.iso
goto start


:rebuild_release
echo Build type: release
set scflags=-DSC_USE_CDROM -DSC_RELEASE
goto rebuild
:rebuild_debug
echo Build type: debug
set scflags=-DSC_USE_CDROM
goto rebuild

:rebuild



echo .-.
del /Q lowram.*
del /Q cd\0.bin release\%pj_name%*

echo ._.
..\tools\release.exe
more release.h
echo ._o
@REM make OBJFMT=elf
set flags=-Wall -Werror -m2 %scflags%
set ldflags=-nostartfiles --script bart.lnk -Map main_elf.map

@REM Compress embedded data
..\tools\comp -c cd\D0000384.BIN data\D0000384.CMP
..\tools\comp -c cd\D1000384.BIN data\D1000384.CMP
..\tools\bin2c data\D0000384.CMP data\D1000384.CMP data\output.c

@REM Compile each source file
set obj=./CRT0.o
..\tools\my_del.exe CRT0.o
sh-elf-as CRT0.S -o CRT0.o
if not exist CRT0.o goto start

dir /b *.c > objects.txt
echo ../satcom_lib/sc_common.c >> objects.txt
echo ../satcom_lib/saturn/sc_saturn.c >> objects.txt

FOR /f %%A IN (objects.txt) DO (
@echo Processing %%A ...
set obj=!obj! ./%%A.o
@REM Delete the old .o file, compile, and test if compilation failed or not
..\tools\my_del.exe %%A.o
sh-elf-gcc -c %%A -o %%A.o %flags%
if not exist %%A.o goto start
)


@echo Link object files ...
REM echo !obj!
..\tools\my_del.exe cd/0.bin
sh-elf-ld %ldflags% !obj! -lc %LGCC_PATH%libgcc.a -o cd/0.bin
@REM Test compilation
if not exist cd/0.bin goto start



@echo Compile OK

@echo Remove temporary files ...
del /Q objects.txt *.o ..\satcom_lib\sc_common.c.o ..\satcom_lib\saturn\sc_saturn.c.o
del /Q data\output.c data\output.h data\*.cmp
del /Q main_elf.map


@echo Build final iso ...
mkisofs -quiet -sysid "SEGA SATURN" -volid "%pj_name%" -volset "%pj_name%" -publisher "SEGA ENTERPRISES, LTD." -preparer "SEGA ENTERPRISES, LTD." -appid "%pj_name%" -abstract "ABS.TXT" -copyright "CPY.TXT" -biblio "BIB.TXT" -generic-boot %ip_path% -full-iso9660-filenames -o release\%pj_name%.iso cd
..\tools\ip_header release\%pj_name%.iso -today -man "SEGA TP ALSOU" -ver "V0.001" -prod "T-042G" -area "JTUBKAEL" -per "J" -title "SatCom dummy    sample          By cafe-alpha   ppcenter.free.fr"


echo o_o Compile successed :)
goto start


:test
cd yabause
start yabause.exe
cd ..
goto start

:upload
cd ..
start /B satlink.exe freewing.ini -x %pj_name%\cd\0.bin 0x06004000
cd %pj_name%
goto start


:end
REM Remove SaturnOrbit from environment (needed, because it interferes with MinGW environment).
set path=%pathtmp%
set C_INCLUDE_PATH=%C_INCLUDE_PATH_TMP%
set CPLUS_INCLUDE_PATH=%CPLUS_INCLUDE_PATH_TMP%

