@REM
@REM	This file cleans up anything done prior, pulls the .nbh and renames, and slices
@REM	it into a pkg'd dump. Thanks to sfaure3 and tadzio and bepe for the basis of this .bat
@REM	and all the awesome tools.
@REM

@echo Processing Input.nbh

@REM
@REM	Clean up anything prior
@REM

@echo Cleaning up prior runs....

@rd /s /q .\OEM
@rd /s /q .\SYS
@rd /s /q .\ROM
@rd /s /q .\Temp
@rd /s /q .\Temp2
@rd /s /q .\Output
@rd /s /q .\Logs
@mkdir .\Temp
@echo .... Done.

@echo Locating Starting .nbh...
@ren *.nbh Input.nbh
@cd .\Temp
@echo .... Done.

@echo Extracting the OS from the ROM...
@mkdir extract
@cd extract

@..\..\tools\NBHextract.exe ..\..\Input.nbh > nbhExtract.log

@REM Rename and Move OS.nb, then Clean up Junk.
@move *_OS.nb OS.nb
@move OS.nb ..
@cd ..
@rd /s /q extract

@echo .... Done.

@REM Split the ROM OS file.

@..\Tools\NBSplit.exe -titan OS.nb > nbsplit.log
@copy OS.nb.payload OS.nb.xip

@if errorlevel 1 goto error

@REM Cleaning again.
@del OS.nb.extra

@echo Pulling boot.rgu....
@start /wait ..\Tools\RomMaster.exe -x -w 5 OS.nb.xip -o xip.bin
@mkdir XIP
@start /wait ..\Tools\dumprom.exe -d XIP -x -5 xip.bin
@cd ..
@mkdir ROM
@mkdir ROM\XIP
@move Temp\XIP\boot.rgu ROM\XIP
@cd Temp
@rd /s /q .\dump
@rd /s /q .\XIP
@copy OS.nb.payload ..\ROM\8004000-OS.nb
@echo ... Done.

@REM Cleaning again.
@del OS.nb
@del xip.bin
@del OS.nb.xip

@echo Creating imgfs.bin...
@..\Tools\ImgfsFromNB.exe OS.nb.payload imgfs.bin > imgfsfromnb.log

@if errorlevel 1 goto error

@echo Building the dump folder....
@..\Tools\ImgfstoDump.exe imgfs.bin > imgfsdump.log

@if errorlevel 1 goto error

@echo .... Done.

@REM Cleaning again.
@del imgfs.bin
@del OS.nb.payload

@echo Packaging...
@start /wait ..\Tools\pkgtool.exe .\dump
@echo .... Done.

@REM Prepare for kitchen.
@echo Preparing the dump for the kitchen...
@cd dump
@attrib /s -s -h -r .\OEM\*.dsm
@attrib /s -s -h -r .\SYS\*.dsm
@echo .... Done.

@REM Create the kitchen and move appropriately.
@echo Building the kitchen...
@cd ..\..
@move Temp\dump\OEM
@move Temp\dump\SYS
@rd /s /q Temp\dump
@ren Temp Logs
@echo .... Done.

@echo Building and Packaging, Complete!
@pause

@goto:eof

:error
@echo ---> Error <---
@echo An error occured while processing '%1'.
@echo Processing was aborted; Any generated files are probably unusable!
@goto:eof
