@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=SCFirmWare

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 `src' to rebuild source archive.
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
If "%cm%" == "src" goto rebuild_src
If "%cm%" == "t" goto test
If "%cm%" == "s" goto upload
goto start


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



:rebuild_src
REM build source file in order to add it to the ISO
@echo Archive source files ...
cd ..
start /MIN /wait archive.bat i
cd %pj_name%
copy ..\source.zip cd\

goto start



:rebuild
set scflags=-Wall -Werror -m2 
set scldflags=-nostartfiles --script bart.lnk -Map main_elf.map
set ip_head_params=-today -man "SEGA TP ALSOU" -ver "V0.010" -area "JTUBKAEL" -per "J" -title "PC  <=>  SATURN TRANSFER UTILITYBy cafe-alpha   ppcenter.free.fr"

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

echo ._.
..\tools\release.exe
more release.h
echo ._o

@REM -----------------------------------------------------------------------------------------------------------
@REM Build ARP firmware.
set flags=%scflags% -DSC_EXEC_IN_LRAM -DSC_USE_CDROM
set ldflags=%scldflags%

@REM Compile each source file
sh-elf-as CRT0.S -o CRT0.o
set obj=./CRT0.o

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!
sh-elf-ld %ldflags% !obj! -lc %LGCC_PATH%libgcc.a -o lowram.bin
@REM Test compilation
if not exist lowram.bin goto start


@REM Make ARP firmware binary
@echo Make firmware binary loader ...
@echo .

@REM As lowram.bin needs to be loaded in lowram, specify exec address as 0x00200000 when building firmware.
..\tools\mkfirm.exe lowram.bin cd\ARP\firm.bin 0x00200000 256
..\tools\ip_header cd\ARP\firm.bin %ip_head_params% -prod "T-053G"



@REM -----------------------------------------------------------------------------------------------------------
@REM For people who don't want to use CD-R, make standalone Saturn binary to be executed from 0x06004000.
set flags=%scflags% -DSC_EXEC_IN_LRAM -DFLASHMENU_INC
set ldflags=%scldflags%

@echo Re-Processing sc_saturn.c ...
..\tools\my_del.exe ../satcom_lib/saturn/sc_saturn.c.o
sh-elf-gcc -c ../satcom_lib/saturn/sc_saturn.c -o ../satcom_lib/saturn/sc_saturn.c.o %flags%
if not exist ../satcom_lib/saturn/sc_saturn.c.o goto start

@echo Re-Processing main.c ...
..\tools\my_del.exe main.c.o
sh-elf-gcc -c main.c -o main.c.o %flags%
if not exist main.c.o goto start

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



@REM Program in lowram.bin needs to be loaded in lowram, so include it in a loader in order to load the binary from the usual 0x06004000 address.
@echo Convert LowRam binary ...
..\tools\bin2c.exe lowram.bin lowram.c
@echo Build LowRam loader (standalone) ...
set flags=%scflags%
set ldflags=-nostartfiles --script loader/bart.lnk -Map main_elf.map

..\tools\my_del.exe loader/CRT0.o
sh-elf-as loader/CRT0.S -o loader/CRT0.o
if not exist loader/CRT0.o goto start

..\tools\my_del.exe loader/main.c.o
sh-elf-gcc -c loader/main.c -o loader/main.c.o %flags%
if not exist loader/main.c.o goto start

..\tools\my_del.exe lowram.c.o
sh-elf-gcc -c lowram.c -o lowram.c.o %flags%
if not exist lowram.c.o goto start

@echo Link LowRam loader (standalone) ...
..\tools\my_del.exe release/SCProgram.bin
sh-elf-ld %ldflags% ./loader/CRT0.o ./loader/main.c.o ./lowram.c.o -lc %LGCC_PATH%libgcc.a -o release/SCProgram.bin
if not exist release/SCProgram.bin goto start



@REM -----------------------------------------------------------------------------------------------------------
@REM Rebuild the binary in order to enable ARP flashing from CD
@echo .
set flags=%scflags% -DSC_EXEC_IN_LRAM -DFLASHMENU_INC -DFLASHMENU_CD_ADD -DSC_USE_CDROM
set ldflags=%scldflags%

@echo Re-Re-Processing sc_saturn.c ...
..\tools\my_del.exe ../satcom_lib/saturn/sc_saturn.c.o
sh-elf-gcc -c ../satcom_lib/saturn/sc_saturn.c -o ../satcom_lib/saturn/sc_saturn.c.o %flags%
if not exist ../satcom_lib/saturn/sc_saturn.c.o goto start

@echo Re-Re-Processing main.c ...
..\tools\my_del.exe main.c.o
sh-elf-gcc -c main.c -o main.c.o %flags%
if not exist main.c.o goto start

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



@REM Program in lowram.bin needs to be loaded in lowram, so include it in a loader in order to load the binary from the usual 0x06004000 address.
@echo Convert LowRam binary ...
..\tools\bin2c.exe lowram.bin lowram.c
@echo Build LowRam loader (CD-ROM) ...
set flags=%scflags%
set ldflags=-nostartfiles --script loader/bart.lnk -Map main_elf.map

..\tools\my_del.exe loader/CRT0.o
sh-elf-as loader/CRT0.S -o loader/CRT0.o
if not exist loader/CRT0.o goto start

..\tools\my_del.exe loader/main.c.o
sh-elf-gcc -c loader/main.c -o loader/main.c.o %flags%
if not exist loader/main.c.o goto start

..\tools\my_del.exe lowram.c.o
sh-elf-gcc -c lowram.c -o lowram.c.o %flags%
if not exist lowram.c.o goto start

@echo Link LowRam loader (CD-ROM) ...
..\tools\my_del.exe cd/0.bin
sh-elf-ld %ldflags% ./loader/CRT0.o ./loader/main.c.o ./lowram.c.o -lc %LGCC_PATH%libgcc.a -o cd/0.bin
if not exist cd/0.bin goto start





@echo Compile OK

@echo Remove temporary files ...
del /Q lowram.*
del /Q objects.txt *.o ..\satcom_lib\sc_common.c.o ..\satcom_lib\saturn\sc_saturn.c.o
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 %ip_head_params% -prod "T-042G"

del /Q cd\source.zip

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%\release\SCProgram.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%

