@echo off
cls

set CURDIR=%~dp0
cd "%curdir%"
set CURDRIVE=%curdir:~0,2%
%CURDRIVE%

if exist "%1" copy /y "%1" EBOOT.PBP
if not exist EBOOT.PBP goto NoEBOOT

:set /p pspdir=Input Game/Placeholder directory name [or hit enter PSPP00000 will be used]: 
:if /i {%pspdir%}=={} (set pspdir=PSPP00000)

set pspdir=PSPP00000

if {%tmp_folders%}=={yes} goto extract_template
if not {%tmp_folders%}=={no} (set /p tmp_folders=Do you want to create the PKG with the save_tmp fix? [Y/N] [Default=NO]:)

:extract_template
if /i {%pspdir%}=={PSPP00000} (if exist PSPP00000 rd /s /q PSPP00000)
if /i {%pspdir%}=={PSPP00000} (cd tools)
if /i {%tmp_folders%}=={}     (set tmp_folders=n)
if /i {%tmp_folders%}=={y}    (7z.exe x -r -y -o.. PSPP00000.7z)
if /i {%tmp_folders%}=={yes}  (7z.exe x -r -y -o.. PSPP00000.7z)
if /i {%tmp_folders%}=={n}    (7z.exe x -r -y -o.. PSPP00001.7z)
if /i {%tmp_folders%}=={no}   (7z.exe x -r -y -o.. PSPP00001.7z)
if /i {%pspdir%}=={PSPP00000} (cd ..)

if /i {%tmp_folders%}=={n}    (goto ask_extract_iso)
if /i {%tmp_folders%}=={no}   (goto ask_extract_iso)

IF NOT EXIST "%pspdir%" "mkdir %pspdir%"
:IF NOT EXIST "%pspdir%\save_tmp" mkdir "%pspdir%\save_tmp"
:IF NOT EXIST "%pspdir%\open_tmp" mkdir "%pspdir%\open_tmp"
:IF NOT EXIST "%pspdir%\load_tmp" mkdir "%pspdir%\load_tmp"
IF NOT EXIST "%pspdir%\USRDIR" mkdir "%pspdir%\USRDIR"
IF NOT EXIST "%pspdir%\USRDIR\save_tmp" mkdir "%pspdir%\USRDIR\save_tmp"
IF NOT EXIST "%pspdir%\USRDIR\open_tmp" mkdir "%pspdir%\USRDIR\open_tmp"
IF NOT EXIST "%pspdir%\USRDIR\load_tmp" mkdir "%pspdir%\USRDIR\load_tmp
IF NOT EXIST "%pspdir%\USRDIR\CONTENT" mkdir "%pspdir%\USRDIR\CONTENT"

:ask_extract_iso
cls
if EXIST NP.ISO (goto iso_no)

:SET /P ISO=Do you want to extract ISO from EBOOT.PBP? (Y/N):  
:if /i {%ISO%}=={} (set ISO=n)
:if /i {%ISO%}=={y} (goto iso_yes)
:if /i {%ISO%}=={yes} (goto iso_yes)
:if /i {%ISO%}=={n} (goto iso_no)
:if /i {%ISO%}=={no} (goto iso_no)

:iso_yes
cls
copy tools\mongoose.exe .\mongoose.exe>nul
mongoose 1

del mongoose.exe
if not exist MINIS.BIN (goto :ErrorFound1bin)
if not exist ISO.BIN (goto :ErrorFound2bin)

cd tools
  cls
  if exist ..\UCES-00310.ISO 7z.exe e -tiso -y -o.. ..\UCES-00310.ISO PSP_GAME\PARAM.SFO
  if exist ..\UCES-00310.ISO goto Make_Edats_yes

  if exist ..\NPJH-90126.ISO 7z.exe e -tiso -y -o.. ..\NPJH-90126.ISO PSP_GAME\PARAM.SFO
  if exist ..\NPJH-90126.ISO goto Make_Edats_yes

  cd ..
  dir /b "*.iso">iso.txt&&set /p isoimg=<"iso.txt"
  del iso.txt
  cd tools
  7z.exe e -tiso -y -o.. ..\%isoimg% PSP_GAME\PARAM.SFO

:Make_Edats_yes
  cls
  copy MakeEDATs.exe .. >nul
  copy encrypt_edats.exe .. >nul
  copy rk .. >nul
  copy batch.cmd .. >nul
  cd..

  cls
  echo Creating ISO.BIN.EDAT and MINIS.EDAT...
  if exist PARAM.SFO (
    MakeEDATs.exe PARAM.SFO
  ) else (
    encrypt_edats.exe
  )
  del MakeEDATs.exe >nul
  del encrypt_edats.exe >nul
  del batch.cmd >nul
  del "rk" >nul
  del ISO.BIN >nul
  del MINIS.BIN >nul
  del PARAM.SFO >nul
  IF EXIST "%pspdir%\USRDIR\MINIS.EDAT" del %pspdir%\USRDIR\MINIS.EDAT >nul
  IF EXIST "%pspdir%\USRDIR\ISO.BIN.EDAT" del %pspdir%\USRDIR\ISO.BIN.EDAT >nul
  IF EXIST MINIS.EDAT move /y MINIS.EDAT %pspdir%\USRDIR\MINIS.EDAT >nul
  IF EXIST ISO.BIN.EDAT move /y ISO.BIN.EDAT %pspdir%\USRDIR\ISO.BIN.EDAT >nul
  IF EXIST EBOOT.PBP move /y EBOOT.PBP %pspdir%\USRDIR\CONTENT\EBOOT.PBP >nul
  "MAKE.bat"
exit

:iso_no
cls
copy tools\mongoose.exe .\mongoose.exe >nul
mongoose

del mongoose.exe
if not exist MINIS.BIN (goto :ErrorFound1bin)
if not exist ISO.BIN (goto :ErrorFound2bin)

cd tools
  cls
  if exist ..\NP.ISO 7z.exe e -tiso -y -o.. ..\NP.ISO PSP_GAME\PARAM.SFO
  if exist ..\NP.ISO goto Make_Edats_no

  cd ..
  dir /b "..\*.iso">iso.txt&&set /p isoimg=<"iso.txt"
  del iso.txt
  cd tools
  7z.exe e -tiso -y -o.. ..\%isoimg% PSP_GAME\PARAM.SFO

:Make_Edats_no
  cls
  copy MakeEDATs.exe .. >nul
  copy encrypt_edats.exe .. >nul
  copy rk .. >nul
  copy batch.cmd .. >nul
  cd..

  cls
  echo echo Creating ISO.BIN.EDAT and MINIS.EDAT...
  if exist PARAM.SFO (
    MakeEDATs.exe PARAM.SFO
  ) else (
    encrypt_edats.exe
  )
  del MakeEDATs.exe >nul
  del encrypt_edats.exe >nul
  del batch.cmd >nul
  del "rk" >nul
  del ISO.BIN >nul
  del MINIS.BIN >nul
  del PARAM.SFO >nul
  IF EXIST "%pspdir%\USRDIR\MINIS.EDAT" del %pspdir%\USRDIR\MINIS.EDAT >nul
  IF EXIST "%pspdir%\USRDIR\ISO.BIN.EDAT" del %pspdir%\USRDIR\ISO.BIN.EDAT >nul
  IF EXIST MINIS.EDAT move /y MINIS.EDAT %pspdir%\USRDIR\MINIS.EDAT >nul
  IF EXIST ISO.BIN.EDAT move /y ISO.BIN.EDAT %pspdir%\USRDIR\ISO.BIN.EDAT >nul
  IF EXIST EBOOT.PBP move /y EBOOT.PBP %pspdir%/USRDIR\CONTENT\EBOOT.PBP >nul
  "MAKE.bat"
  exit

:ErrorFound1bin
echo ERROR: MINIS.BIN was not created...
pause
exit

:ErrorFound2bin
echo ERROR: ISO.BIN was not created...
pause
exit

:ErrorFound1
echo ERROR: MINIS.EDAT was not created...
pause
exit

:ErrorFound2
echo ERROR: ISO.BIN.EDAT was not created...
pause
exit

:NoEBOOT
echo ERROR: Missing EBOOT.PBP
echo Make sure to put the EBOOT.PBP in PSP2PS3 folder...
pause
exit

:end
