@echo off
set ZZZerror=false
if exist CopyFromHere rmdir /S /Q CopyFromHere
set F1=150
set F2=330
set Type=OEA
set xxxcount=
set xxxwin=

if not (%1)==(/?) goto NextQ
echo.
echo This program is an install generator for 
echo the new Dark_AleX Custom Firmware, %F2:~0,1%.%F2:~1,2% %Type%.
echo.
echo Future updates may or may not occur.
echo.
echo This program can also have values passed into it,
echo and create upgrades for future firmwares.
echo.
echo Format to run with command line:
echo AutoUpgrader Firmware1 Firmware2 TypeOfCustom
echo By Default:
echo AutoUpgrader 150 330 oea
echo.
echo For example:
echo AutoUpgrader 150 540 OEG 	  
echo AutoUpgrader.bat 321 333 	  
echo AutoUpgrader	      	  
echo AutoUpgrader.bat 150 271 SEA 
echo.
goto end
:NextQ

if (%1)==() goto NoPassedValues
echo.
echo Recieved passed values, setting:
if not (%1)==() set F1=%1
if not (%2)==() set F2=%2
if not (%3)==() set Type=%3
echo Done.
echo.
goto afterDoneSelecting
:NoPassedValues

color 3f
title=TheAceOfFire's AutoUpgrader for %F2:~0,1%.%F2:~1,2% %Type%
echo.
echo                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo                     ~~~~~~~~~~~~~  AutoUpgrader  ~~~~~~~~~~~~~
echo                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo                     TheAceOfFire's AutoUpgrader for %F2:~0,1%.%F2:~1,2% %Type%, 
echo                            Dark_AleX's new custom firmware.
echo.
echo                  This will generate the files in the correct folders,
echo                                  for easy upgrading. 
echo.
pause
cd CoreData
set xxxcount=
dir /b ".\Base*.*" |find /c /v "Base*">12345xxxxxxtemp.txt
for /F "delims=" %%i in (12345xxxxxxtemp.txt) do set xxxcount=%%i
if exist 12345xxxxxxtemp.txt del /Q 12345xxxxxxtemp.txt
for %%i in (Base*.*) do set xxxwin=%%i
cd..

if not (%xxxcount%)==(0) goto onePlusFound
echo.
echo ERROR: No Base archive found.
echo.
goto afterDoneSelecting
:onePlusFound

if not (%xxxcount%)==(1) goto selectWhich
:selectDone
if not (%xxxwin:~4,3%)==() set F2=%xxxwin:~4,3%
title=TheAceOfFire's AutoUpgrader for %F2:~0,1%.%F2:~1,2% %Title%
goto afterDoneSelecting

:selectWhich
if exist Base*.* move Base*.* CoreData
echo.
echo                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo                   Enter the full name of one of the following archives:
dir /b CoreData\Base*.*
echo                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo.
set xxxwin=
set /p xxxwin=                  Which Archive is to be used?=
goto selectDone

:afterDoneSelecting
echo Checking folder for required files:
if exist eboot.pbp move eboot.pbp CoreData\eboot.pbp
if not exist CoreData\eboot.pbp goto nexteboot
echo.
echo Unknown eboot.pbp found in CoreData, please relable 
echo to correct value (Either %F2%.pbp or %F1%.pbp).
echo.
:nexteboot

if exist %F1% move %F1% CoreData\%F1%.pbp
if exist %F1%.pbp move %F1%.pbp CoreData\%F1%.pbp
if exist %F1:~0,1%.%F1:~1,2%.pbp move %F1:~0,1%.%F1:~1,2%.pbp CoreData\%F1%.pbp
if exist %F1:~0,1%,%F1:~1,2%.pbp move %F1:~0,1%,%F1:~1,2%.pbp CoreData\%F1%.pbp
if exist CoreData\%F1:~0,1%.%F1:~1,2%.pbp move CoreData\%F1:~0,1%.%F1:~1,2%.pbp CoreData\%F1%.pbp
if exist CoreData\%F1:~0,1%,%F1:~1,2%.pbp move CoreData\%F1:~0,1%,%F1:~1,2%.pbp CoreData\%F1%.pbp
if exist CoreData\%F1% move CoreData\%F1% CoreData\%F1%.pbp
if exist CoreData\%F1%.pbp goto nextBase
echo.
echo %F1%.pbp not found in CoreData, please place
echo a valid copy of the %F1:~0,1%.%F1:~1,2% firmware Eboot in
echo CoreData labled as "%F1%.pbp".
echo.
set ZZZerror=true
:nextBase

if exist %F2% move %F2% CoreData\%F2%.pbp
if exist %F2%.pbp move %F2%.pbp CoreData\%F2%.pbp
if exist %F2:~0,1%.%F2:~1,2%.pbp move %F2:~0,1%.%F2:~1,2%.pbp CoreData\%F2%.pbp
if exist %F2:~0,1%,%F2:~1,2%.pbp move %F2:~0,1%,%F2:~1,2%.pbp CoreData\%F2%.pbp
if exist CoreData\%F2:~0,1%.%F2:~1,2%.pbp move CoreData\%F2:~0,1%.%F2:~1,2%.pbp CoreData\%F2%.pbp
if exist CoreData\%F2:~0,1%,%F2:~1,2%.pbp move CoreData\%F2:~0,1%,%F2:~1,2%.pbp CoreData\%F2%.pbp
if exist CoreData\%F2% move CoreData\%F2% CoreData\%F2%.pbp
if exist CoreData\%F2%.pbp goto nextCurrent
echo.
echo %F2%.pbp not found in CoreData, please place
echo a valid copy of the %F2:~0,1%.%F2:~1,2% firmware Eboot in
echo CoreData labled as "%F2%.pbp".
echo.
set ZZZerror=true
:nextCurrent

if exist 7za.exe move 7za.exe CoreData\7za.exe
if exist CoreData\7za.exe goto next7zip
echo.
echo 7za.exe was not found in CoreData, Please reinstall
echo or replace 7za.exe with a valid 7za.exe file.
echo.
set ZZZerror=true
:next7zip

if exist UnRAR.exe move UnRAR.exe CoreData\UnRAR.exe
if exist CoreData\UnRAR.exe goto nextUnRar
echo.
echo UnRAR.exe was not found in CoreData, Please reinstall
echo or replace UnRAR.exe with a valid UnRAR.exe file.
echo.
set ZZZerror=true
:nextUnRar

cd CoreData
for %%i in (*.rar) do (
echo Converting %%i to [%%~ni.7z]...
if not exist Converting mkdir Converting
unrar.exe x -y -inul -- %%i Converting
cd Converting
..\7za.exe a -t7z ..\%%~ni.7z * -mx=9
cd..
rmdir /S /Q Converting
del /Q %%i
)
cd..

if exist CoreData\Base%F2%.* goto nextBase
echo.
echo Base%F2% was not found in CoreData, Please reinstall
echo or take a zipped copy of the %F2:~0,1%.%F2:~1,2% %Type% upgrade,
echo move it to CoreData, and rename it to "Base%F2%".
echo.
echo For example:
echo "%F2%oe.zip" Changed To "Base%F2%.zip"
echo "%F2%Firmware.rar" Changed To "Base%F2%.rar"
echo "%F2%.7z" Changed To "Base%F2%.7z"
echo.
set ZZZerror=true
:nextBase

if (%ZZZerror%)==(true) goto end
echo Done.
echo.
echo Checking\Converting Base%F2% to correct format
echo.


echo.
echo Done.
echo.
echo Extracting needed data from Base%F2%
echo.
if not exist CopyFromHere\psp\game mkdir CopyFromHere\psp\game
if not exist CopyFromHere1\psp\game mkdir CopyFromHere1\psp\game
cd CoreData
7za.exe x Base%F2%.* -o..\CopyFromHere1\psp\game
cd..
if not exist CopyFromHere1\psp\game\*maker goto WrongFormatError

cd CopyFromHere1\psp\game\
for /D %%i in (*flasher* *dmaker*) do move %%i ..\..\..\CopyFromHere\psp\game\
cd ..\..\..\

if exist CopyFromHere1 rmdir /S /Q CopyFromHere1

echo Done.
echo.
echo Moving Eboots to correct folder.
echo.
cd CopyFromHere\psp\game\*maker\
copy ..\..\..\..\CoreData\%F1%.pbp %F1%.pbp
copy ..\..\..\..\CoreData\%F2%.pbp %F2%.pbp
cd ..\..\..\..\

REM End making proper data, move it to CopyFromHere.
echo Creating mini Readme:
if exist CopyFromHere\Readme.txt del CopyFromHere\Readme.txt
echo This will not work if your psp is not firmware 1.50 or a>>CopyFromHere\Readme.txt
echo custom firmware that allows the running of unsigned code.>>CopyFromHere\Readme.txt
echo.>>CopyFromHere\Readme.txt
echo Move all files/folders from here to the>>CopyFromHere\Readme.txt
echo Drive your PSP is connected to (E:\,F:\, etc)>>CopyFromHere\Readme.txt
echo.>>CopyFromHere\Readme.txt
echo Once the files are on your PSP, please run the update maker program.>>CopyFromHere\Readme.txt
echo You can find it under the "Memory Stick" folder in the "Game" menu.
echo.>>CopyFromHere\Readme.txt
echo When it finishes running, it will generate a file called "Data.dxar".>>CopyFromHere\Readme.txt
echo Just move that file from :\psp\game\updmaker to :\psp\game\%F2%flasher>>CopyFromHere\Readme.txt
echo.>>CopyFromHere\Readme.txt
echo Then run the flashing program. It is in the same menu as the updmaker:>>CopyFromHere\Readme.txt
echo It is under the "Memory Stick" folder in the "Game" menu.>>CopyFromHere\Readme.txt
echo.>>CopyFromHere\Readme.txt
echo Please make sure your PSP is fully charged (Just like any other flashing program).
echo.>>CopyFromHere\Readme.txt
echo Don't forget, homebrew runs with the %F2% core by default,>>CopyFromHere\Readme.txt
echo if it is placed in the \psp\game folder.>>CopyFromHere\Readme.txt
echo.>>CopyFromHere\Readme.txt
echo Any homebrew placed in \psp\game%F1% runs with the %F1% core,>>CopyFromHere\Readme.txt
echo Any homebrew placed in \psp\game%F2% runs with the %F2% core.>>CopyFromHere\Readme.txt
echo Done.
echo.
echo.
echo                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo                     ~~~~~~~~~~~~~~~  Success!  ~~~~~~~~~~~~~~~
echo                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo.
echo Simply move everything from the "CopyFromHere" folder to the 
echo same drive as your PSP (For example, D:\, F:\, etc).
echo.
pause
start CopyFromHere
goto end

:WrongFormatError
echo.
echo Sorry, but for whatever reason, we were unable to extract 
echo data from CoreData\Base%F2%. Try repackaging it as either a 
echo zip, 7z, or Tar file type.
echo.
echo Also, please make sure that the Base%F2% package contains the
echo Folders for flashing and the folders for making Data.Dxar.
echo.
goto end
:end
pause
title `
color
@echo on