@echo off
echo Signing EBOOT...
if not {%1}=={} set tmp_folders=%1

if exist EBOOT.BIN (goto sign_eboot)

if not exist NP.ISO (goto :NoISO)

:extract_EBOOT.OLD
del EBOOT.OLD >nul
tools\7z.exe e np.iso PSP_GAME\SYSDIR\EBOOT.OLD
if not exist EBOOT.OLD goto extract_EBOOT.BIN
ren EBOOT.OLD EBOOT.BIN>nul
tools\eboot_decrypt.exe EBOOT.BIN
if exist BOOT.BIN goto rename_EBOOT
del EBOOT.BIN>nul

:extract_EBOOT.BIN
tools\7z.exe e np.iso PSP_GAME\SYSDIR\EBOOT.BIN
if not exist EBOOT.BIN (goto :NoEBOOT)

:decrypt_EBOOT
tools\eboot_decrypt.exe EBOOT.BIN
if not exist BOOT.BIN goto sign_eboot

:rename_EBOOT
del EBOOT.BIN
ren BOOT.BIN EBOOT.BIN

:sign_eboot
copy tools\OPNSSMP.BIN . >nul
copy EBOOT.BIN EBOOT.BAK >nul
copy tools\PrxEncrypter.exe .\PrxEncrypter.exe >nul
copy tools\zlib1_.dll .\zlib1_.dll >nul
PrxEncrypter EBOOT.BIN
del PrxEncrypter.exe
del zlib1_.dll

set add_signed_eboot=n
if not exist NP.ISO goto sign_done
if not exist EBOOT.BIN goto sign_done
cls
set add_signed_eboot=y
:set /p add_signed_eboot=Do you want to add EBOOT to ISO [y/n]? 
if /i {%add_signed_eboot%}=={} set add_signed_eboot=y
if /i {%add_signed_eboot%}=={n} goto sign_done
if /i {%add_signed_eboot%}=={no} goto sign_done

tools\WQSG_UMD_R31.exe --iso=.\NP.ISO --file=.\EBOOT.BIN --importdir=PSP_GAME/SYSDIR --quiet
tools\WQSG_UMD_R31.exe --iso=.\NP.ISO --file=.\OPNSSMP.BIN --importdir=PSP_GAME/SYSDIR --quiet

del OPNSSMP.BIN >nul
del EBOOT.BIN >nul
del EBOOT.BAK >nul

exit

:sign_done
cd tools
popup.vbs "Done! EBOOT.BIN is signed :) (old/original EBOOT is now EBOOT.BAK)"
cd..
exit

:NoEBOOT
echo ERROR: EBOOT.BIN was not found...
pause
exit

:NoISO
echo ERROR: NP.ISO was not found...
pause
exit

