@ECHO off
IF not exist Temp\dump\default.hv (
  ECHO.
  ECHO Error: Execute BuildOS.exe first!
  ECHO.
  PAUSE
  goto end
)

ECHO First: Prep ROM...
PATH %path%;.\tools\
XCOPY .\rom\NK.fat .\temp\ /y
CD .\Temp\
..\Tools\prepare_imgfs NK.fat -nosplit
PATH %path%;..\Tools\

ECHO Next: Build the ROM...
ATTRIB .\Dump\*.rgu -s -r -h
DEL .\Dump\*.rgu
ATTRIB .\Dump\*.* +s +r
BUILDIMGFS
MAKE_IMGFS nk.fat -nosplit
REN nk.fat nk.nba

ECHO.
ECHO Next: Encode the ROM...
ECHO --------------
ECHO !!!WARNING!!!!
ECHO --------------
ECHO Select your appropriate operator to avoid country error...
ECHO.
ECHO --------------
ECHO Steps...
ECHO --------------
ECHO * Press "Encode"
ECHO * Press "..."
ECHO * Double click your operator name. Be sure you are selecting the .prj file from the approrpiate "\Tools" directory.
ECHO * Press "Next->"
ECHO * Press "Encode"
ECHO * Press "OK" on any errors or further dialogs.
ECHO * Press "Exit"
@ECHO OFF
CD ..\Tools\
CALL HTC64_Extended_ROM_Tool.exe
GOTO Menu

:Menu
ECHO.
ECHO Next: Flash the ROM...
ECHO --------------
ECHO !!!WARNING!!!!
ECHO -------------
ECHO If Activesync, or the ROM loader tool cannot find your device, set the device to bootloader mode and try again. To do so, press "Note" and "Power" on your device at the same time and then press reset with the stylus.
ECHO.
ECHO --------------
ECHO Steps...
ECHO --------------
ECHO * Press Next
ECHO * Press Update
ECHO * Press Finish
ECHO.
ECHO But first, press the key corresponding to the program you want to flash with:
ECHO.
ECHO 1 for the Standard RUU (if your Apache has the 1.0/1.02/2.0/2.02 bootloader)
ECHO 2 for the NoID RUU  (if you get Country Code errors with the standard app)
ECHO 3 for the New RUU (if your Apache has the new 1.04/2.04 bootloader)
ECHO.
SET /p userinp=Well, which will it be then?
SET userinp=%userinp:~0,1%
IF /I "%userinp%"=="1" goto RUU
IF /I "%userinp%"=="2" goto NoID
IF /I "%userinp%"=="3" goto NewBL
ECHO Invalid choice
GOTO Menu

:RUU
ECHO.
ECHO Wait a moment
ECHO.
COPY ..\temp\nk.nbf ..\Upgrade\RUU\nk.nbf /y
CD ..\Upgrade\RUU\
CALL ROMUpgradeUt.exe
GOTO LeaveTemp

:NoID
ECHO.
ECHO Wait a moment
ECHO.
COPY ..\temp\nk.nbf ..\Upgrade\NoID_RUU\nk.nbf /y
CD ..\Upgrade\NoID_RUU\
CALL APACUpgradeUt_noID.exe
GOTO LeaveTemp

:NewBL
ECHO.
ECHO Wait a moment
ECHO.
COPY ..\temp\nk.nbf ..\Upgrade\NewBootloader_RUU\nk.nbf /y
CD ..\Upgrade\NewBootloader_RUU\
CALL ROMUpgradeUt.exe
GOTO LeaveTemp

:LeaveTemp
ECHO.
ECHO.
ECHO Done! To delete your temp folder, press Y. Press any other key to exit.
ECHO.
ECHO.
SET /p userinp=
SET userinp=%userinp:~0,1%
IF /I "%userinp%"=="y" goto purge
GOTO end

:purge
ECHO.
ECHO Wait a moment
ECHO.
ECHO OFF
CD ..\..
RMDIR .\TEMP /S /Q
ECHO.
ECHO All Done!
ECHO.
PAUSE
GOTO end


:error
ECHO.
ECHO !!!!!!!!!!!!!
ECHO !!!!ERROR!!!!
ECHO !!!!!!!!!!!!!
ECHO.
@ECHO OFF
DEL NK.nba
DEL imgfs_raw_data.bin
DEL imgfs_removed_data.bin
COPY nk.nbf ..\Upgrade\nk.nbf /y
DEL nk.nbf
CD ..
RMDIR .\TEMP /S /Q
:end
