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

set pspdir=
dir /b "*-*_00-*">pkg.txt&&set /p pspdir=<"pkg.txt"
del pkg.txt

if exist %pspdir% (goto :pic_yes)

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

set /p name=Input Game Name: 
SET /P GID=Do you want to change GameID? (eg for using several games with one placeholder) (Y/N): 
if /i {%GID%}=={y} (goto :gid_yes)
if /i {%GID%}=={yes} (goto :gid_yes)
if /i {%GID%}=={n} (goto :gid_no)
if /i {%GID%}=={no} (goto :gid_no)

:gid_yes
set /p pspdir_new=Input New GameID [eg ULUS10469]: 
ren %pspdir% %pspdir_new% 
set pspdir=%pspdir_new%

:gid_no
cd tools
PARAM_SFO_Editor.exe "PARAM.SFO" "TITLE_ID" "%pspdir%" "TITLE" "%name%" "CATEGORY" "MN"
cd..

:askpkg
set /p pkg=Input output package name WITHOUT extension: 
SET /P PIC=Do you want ICON0/PIC1/SND/PAMF from %name%? (Y/N): 
if /i {%PIC%}=={} (set PIC=y)
if /i {%PIC%}=={y} (goto :pic_yes)
if /i {%PIC%}=={yes} (goto :pic_yes)
if /i {%PIC%}=={n} (goto :pic_no)
if /i {%PIC%}=={no} (goto :pic_no)

:pic_yes
set iso=NP
if exist "%iso%.iso" goto get_pic
if not exist "%isoimg%" goto prompt_iso
set iso=%isoimg:~0,-4%
goto get_pic

:prompt_iso
set /p iso=Input name of ISO extracted by MAKE_EDATS_[CEX/DEX] without extension [eg UCES-00310] or original ISO: 
if {%iso%}=={} (set iso=NP)
if not exist "%iso%.iso" (goto :pic_no)

:get_pic
cd tools
7z.exe e -tiso -y -o..\%pspdir% -r ..\%iso%.iso ICON0.PNG SND0.AT3 PIC1.PNG ICON1.PMF
cd..
IF EXIST "%pspdir%/PIC0.PNG" del %pspdir%\PIC0.PNG
IF EXIST "%pspdir%/PIC2.PNG" del %pspdir%\PIC2.PNG
IF EXIST "%pspdir%/ICON1.PMF" ren %pspdir%\ICON1.PMF ICON1.PAM

:pic_no
if not exist "%pspdir%\PARAM.SFO" copy /Y tools\PARAM.SFO "%pspdir%\PARAM.SFO"
cd tools
cls
echo Creating package...
set cid=%pspdir:~0,7%
if %cid%==PSPPS3- (
  set cid=%pspdir%
  echo ContentID = %pspdir%> package.conf
) else (
  set cid=PSPPS3-%pspdir%_00-0000000000000001
  echo ContentID = PSPPS3-%pspdir%_00-0000000000000001> package.conf
)
echo Klicensee = 0x2A6AFBCF43D1579F7D738741A13BD42E>> package.conf
echo DRMType = Free>> package.conf
echo ContentType = minis>> package.conf
echo PackageVersion = 01.00>> package.conf

psn_package_npdrm package.conf ..\%pspdir%
del package.conf
move "%cid%.pkg" ..
cd..
if not {%pkg%}=={} ren "%cid%.pkg" "%pkg%.pkg"
if not {%pkg%}=={} set pkgname=%pkg%
if {%pkg%}=={} set pkgname=%cid%

cd tools
popup.vbs "Done! Package created as %pkgname%.pkg"
cd..

goto ftp_no

:ftp_question
cls
SET /P FTP=Do you want to transfer %pkgname%.pkg to PS3 via FTP? (Y/N) 
if /i {%FTP%}=={} (set /p FTP=n)
if /i {%FTP%}=={y} (goto :ftp_yes)
if /i {%FTP%}=={yes} (goto :ftp_yes)
if /i {%FTP%}=={n} (goto :ftp_no)
if /i {%FTP%}=={no} (goto :ftp_no)

:ftp_yes
cls
SET /P IP=Input PS3 IP: 
echo psp2ps3> transfer.ftp
echo psp2ps3>> transfer.ftp
echo cd dev_hdd0/packages>> transfer.ftp
echo binary>> transfer.ftp
echo mput "%pkgname%.pkg">> transfer.ftp
echo disconnect>> transfer.ftp
echo bye>> transfer.ftp
ftp.exe -i -v -s:transfer.ftp %IP%
del transfer.ftp
cd tools
popup.vbs "Done! Package was tranferred to PS3 (IP:%IP%) to dev_hdd0/packages/%pkgname%.pkg"
cd..
exit

:ftp_no
explorer .
exit
