@echo off
echo Prepare ROM file...
cd BaseROM
copy "*.bin" "./../Tools/" >nul
cd ./../Tools
rename *.bin os.nb

echo Extract ImgFS...
NBSplit -emu os.nb > nul
prepare_imgfs.exe os.nb.payload -nosplit > nul

echo Dump ImgFS...
viewimgfs.exe imgfs_raw_data.bin  > nul

echo Create ROM folder and boot.rgu...
cd dump
mkdir ROM
cd ROM
mkdir XIP
cd ./../..
set _FLATRELEASEDIR=.
echo REGEDIT4 > dump/ROM/XIP/boot.ascii
rgucomp.exe -o dump/default.hv -nologo >> dump/ROM/XIP/boot.ascii
stringconverter.exe dump/ROM/XIP/boot.ascii dump/ROM/XIP/boot.rgu /unicode
del dump\ROM\XIP\boot.ascii

echo Create OEM and SYS...
pkgtool.exe "./dump/"

cd dump
move "ROM" "./../../ROM" > nul
move "OEM" "./../../OEM" > nul
move "SYS" "./../../SYS" > nul
cd ..
rmdir dump > nul
cd ..
rem echo Have to move some misplaces OEM folders
cd SYS
FOR /F %%f IN ('dir OEM* /b') DO MOVE %%f .\..\OEM\%%f > nul
cd..

echo Done.