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

set tmp_folders=no
if not {%2}=={} set tmp_folders=%2

if exist %1 set pkgpacked=%1
set pkgpacked=%pkgpacked:~1,-5%
if exist "%pkgpacked%.pkg" echo Copying %pkgpacked%.pkg...
if exist "%pkgpacked%.pkg" copy /y "%pkgpacked%.pkg" . >nul
for %%F in ("%pkgpacked%") do set pkgpacked=%%~nF
if exist "%pkgpacked%.pkg" goto unpack
set pkgpacked=

dir /b *.pkg>pkg.txt&&set /p pkgpacked=<pkg.txt
del pkg.txt>nul

cls
if not "%pkgpacked%"=="" echo Default PKG: %pkgpacked%

set pkgpacked=
set /p pkgpacked=Input name of PKG that you want to extract (without extension):  
if exist "%pkgpacked%.pkg" (goto :unpack)

set pkg=%pkgpacked%

if "%pkg%"=="" (
  dir /b "*.pkg">pkg.txt&&set /p pkgpacked=<"pkg.txt"
  del pkg.txt
)
if "%pkg%"=="" set pkgpacked=%pkgpacked:~0,-4%
set pkg=

if not exist "%pkgpacked%.pkg" (goto :nopkg)

:unpack
echo Extracting EBOOT.PBP from PKG...
cd tools
PkgView.exe -ex ../"%pkgpacked%.pkg"
cd..

del /q "%pkgpacked%\*.*"
cls
xcopy /y /s "%pkgpacked%\*.*" .
cls
dir /b "%pkgpacked%\*.">psp.txt&&set /p pkgdir=<"psp.txt"
cls
echo on
copy "%pkgpacked%\%pkgdir%\USRDIR\CONTENT\EBOOT.PBP" .
rd /s /q "%pkgpacked%"
if not exist "EBOOT.PBP" (goto :NoEBOOT)
del /q psp.txt
REN "%pkgdir%" PSPP00000
"CEX.bat"
exit

:nopkg
echo ERROR: Package file not found....
pause
exit
:NoEBOOT
echo ERROR: EBOOT.PBP was not found....
pause
exit
