@echo off
rem **********************************************************************
rem *
rem * OPLBATCH 1.1
rem *
rem **********************************************************************
rem * DATE: 24-APR-2020
rem *
rem * After running HDLBATCH, load OPL Manager and use the batch tool to
rem * download any desired art files. Make sure to run OPL Manager as an
rem * administrator, and set its mode to Local PS2 HDD (hdl_dump 0.9.2).
rem * Once all the art are configured as desired, exit OPL Manager and
rem * find the hdl_hdd folder.
rem *
rem * Extract the OPLBATCH files into the hdl_hdd folder, and run the
rem * OPLBATCH.BAT file using right-click > "Run as Administrator". 
rem *
rem * If existing VMC and CFG folders are found on the PS2 HDD, their
rem * contents will be imported to the hdl_hdd folders (to prevent
rem * these files from being overwritten by newly created ones).
rem *
rem * Answer the few questions, and let the batch file run.
rem *
rem * The general process is:
rem * 1)	import existing VMC and CFG files from PS2
rem * 2)	generate a list of installed games; use this list to
rem *		create VMC and CFG files that are missing; existing
rem * 		CFG files are updated to reflect changes in the VMC
rem *		files being used
rem * 3)	copy over all files in the ART, CFG, and POPS folders
rem * 4)	copy over only newly created VMC files; PS2 and OPL Manager
rem *		side are essentially synced upon completion
rem *
rem * All intermediate files are in the TMP sub-folder, and may be deleted.
rem *
rem * NOTE:	If VMC files exist for slot two, and you elect to only
rem *		create new VMCs for slot one, the slot two VMCs will be
rem * 		left untouched on the PS2, but the CFG file will be
rem *		changed to use only slot one. This way, should you wish
rem *		to use slot two later on, a simple change in the per-game
rem *		setting inside OPL will re-enable slot two.

cls

setlocal enabledelayedexpansion

cd /d "%~dp0"
set oplpath=%~dp0%

if NOT "!oplpath:~-8,7!"=="hdl_hdd" (
	echo.
	echo ERROR: Please make sure to run this batch file from the hdl_hdd folder.
	pause
	goto :eof
)

if not exist TMP mkdir TMP
hdl_dump query > TMP\hdddevices.txt 
set "hdd="
for /f "tokens=1,*" %%A in ( 'findstr /e /c:"formatted Playstation 2 HDD" TMP\hdddevices.txt' ) do set hdd=%%A
del TMP\hdddevices.txt >nul 2>&1

if not defined hdd (
	echo No Playstation 2 HDD found; exiting...
	pause
	goto :eof
)

echo Using "!hdd!" as target PS2 device.
(
	echo device !hdd!
	echo ls
	echo exit
) > TMP\lshdd.pfs
type TMP\lshdd.pfs | pfsshell > TMP\lshdd.txt
del TMP\lshdd.pfs >nul 2>&1

set "oplpart="
set "popspart="
for /f "tokens=1,2* skip=13" %%A in ( TMP\lshdd.txt ) do (
	if "%%C"=="+OPL" set oplpart=1
	if "%%C"=="__.POPS" set popspart=1
)
del TMP\lshdd.txt >nul 2>&1

if not defined oplpart (
	echo ERROR: No +OPL partition found. Please create a partition named +OPL
	echo        using uLaunchELF on your PS2.
	pause
	goto :eof
)

if not defined popspart echo WARNING: No __.POPS partition exists; disabling POPS options.

set "assets="
choice /c NY /m "Do you wish to copy ART files: "
if errorlevel 2 set "assets=ART !assets!"

if defined popspart (
	set "dopops="
	choice /c NY /m "Do you wish to copy POPS (VCD) files: "
	if errorlevel 2 set dopops=1
)

choice /c NY /m "Do you wish to create and copy missing VMC files: "
if errorlevel 2 (
	set "assets=CFG !assets!"
	choice /c 12 /m "Select [1] to create VMC only for slot 1, or [2] for both slots: "
	if errorlevel 2 (set vmc=2) else (set vmc=1)
) else (
	set vmc=0
)

(
	echo device !hdd!
	echo mount +OPL
	echo ls
	echo umount
	echo exit
) > TMP\lsopl.pfs
type TMP\lsopl.pfs | pfsshell > TMP\lsopl.txt 2>nul
del TMP\lsopl.pfs >nul 2>&1

set "oplfolders="
for /f "tokens=1,2,3,4,5* skip=15" %%A in ( TMP\lsopl.txt ) do (
	if "%%E"=="CFG" set "oplfolders=CFG !oplfolders!"
	if "%%E"=="VMC" set "oplfolders=VMC !oplfolders!"
)
rem del TMP\lsopl.txt >nul 2>&1

echo 	...moving existing VMC and CFG directories on computer to VMC.bkp and CFG.bkp, respectively...
if exist VMC.bkp (
	echo 		...computer directory VMC.bkp already exists; exiting.
	pause
	goto :eof
)

if exist CFG.bkp (
	echo 		...computer directory CFG.bkp already exists; exiting.
	pause
	goto :eof
)

if exist VMC (move VMC VMC.bkp) else mkdir VMC.bkp
mkdir VMC

if exist CFG (move CFG CFG.bkp) else mkdir CFG.bkp
mkdir CFG


echo 	...done.

if defined oplfolders (
	echo The following folders already exist on the PS2 HDD: !oplfolders!
	choice /c NY /m "Do you wish to back up the contents of these folders: "
	if errorlevel 2 (
		echo 	...copying contents of !oplfolders! onto computer...
		for %%I in (!oplfolders!) do (
			set folder=%%I
			if "!folder!"=="CFG" set "types=.cfg"
			if "!folder!"=="VMC" set "types=.bin"
			(
				echo device !hdd!
				echo mount +OPL
				echo cd !folder!
				echo ls
				echo umount
				echo exit
			) > TMP\ls!folder!.pfs
			type TMP\ls!folder!.pfs | pfsshell > TMP\ls!folder!.txt 2>nul
			rem del TMP\ls!folder!.pfs >nul 2>&1
			(
				echo device !hdd!
				echo mount +OPL
				echo cd !folder!
				echo lcd !folder!
				for /f "tokens=1,2,3,4*" %%A in ( 'findstr /i "!types!" TMP\ls!folder!.txt' ) do (echo get "%%E")
				echo umount
				echo exit
			) > TMP\get!folder!.pfs
			echo 		...backing up !folder!...
			type TMP\get!folder!.pfs | pfsshell >nul 2>&1
		)
		echo 	...done.
	)
)

if "!vmc!"=="0" goto :skippedVMC

echo Getting list of installed games...
hdl_dump hdl_toc !hdd! > TMP\toc.txt

echo Setting up VMC and CFG files...
(
	echo device !hdd!
	echo mount +OPL
	echo mkdir VMC
	echo cd VMC
	echo lcd VMC
) > TMP\VMC.pfs

set "xfervmc="

for /f "tokens=1,2,3,4*" %%A in (TMP\toc.txt) do (
	set "gameid="
	if "%%A" == "CD" set gameid=%%D
	if "%%A" == "DVD" set gameid=%%D
	if defined gameid (
		if not exist VMC\!gameid!_0.bin (
			if exist VMC.bkp\!gameid!_0.bin (
				copy VMC.bkp\!gameid!_0.bin VMC\!gameid!_0.bin >nul 2>&1
			) else (
				copy VMC.bin VMC\!gameid!_0.bin >nul 2>&1
			)
			echo put !gameid!_0.bin >> TMP\VMC.pfs
			set xfervmc=1
		)
		if "!vmc!"=="2" (
			if not exist VMC\!gameid!_1.bin (
				if exist VMC.bkp\!gameid!_1.bin (
					copy VMC.bkp\!gameid!_1.bin VMC\!gameid!_1.bin >nul 2>&1
				) else (
					copy VMC.bin VMC\!gameid!_1.bin >nul 2>&1
				)
				echo put !gameid!_1.bin >> TMP\VMC.pfs
				set xfervmc=1
			)
		)

		if not exist CFG\!gameid!.cfg (
			if exist CFG.bkp\!gameid!.cfg copy CFG.bkp\!gameid!.cfg CFG\!gameid!.cfg >nul 2>&1
		)

		if not exist CFG\!gameid!.cfg (
			echo $VMC_0=!gameid!_0> CFG\!gameid!.cfg
			if "!vmc!"=="2" echo $VMC_1=!gameid!_1>> CFG\!gameid!.cfg
		) else (
			set hasvmc0=0
			set hasvmc1=0
			copy /y nul CFG\!gameid!.cfg.new >nul
			for /f "tokens=*" %%I in ( CFG\!gameid!.cfg ) do (
				set line=%%I
				if "!line:~0,7!"=="$VMC_0=" (
					if exist VMC\!gameid!_0.bin (
						echo $VMC_0=!gameid!_0>> CFG\!gameid!.cfg.new
						set hasvmc0=1
					)
				) else (
					if "!line:~0,7!"=="$VMC_1=" (
						if "!vmc!"=="2" (
							if exist VMC\!gameid!_1.bin (
								echo $VMC_1=!gameid!_1>> CFG\!gameid!.cfg.new
								set hasvmc1=1
							)
						)
					) else echo !line!>> CFG\!gameid!.cfg.new			
				)
			)
	
			if "!hasvmc0!"=="0" echo $VMC_0=!gameid!_0>> CFG\!gameid!.cfg.new
			if "!hasvmc1!"=="0" ( if "!vmc!"=="2" echo $VMC_1=!gameid!_1>> CFG\!gameid!.cfg.new)
			del CFG\!gameid!.cfg >nul 2>&1
			move CFG\!gameid!.cfg.new CFG\!gameid!.cfg >nul 2>&1
		)
	)
)

if not defined xfervmc (
	echo 	...no new VMCs to transfer to PS2.
) else (
	(
		echo umount
		echo exit
	) >> TMP\VMC.pfs
	echo 	...running PFS SHELL script to copy VMC files...
	type TMP\VMC.pfs | pfsshell >nul 2>&1
)
echo 	...done
rem del TMP\VMC.pfs >nul 2>&1
rem del TMP\toc.txt >nul 2>&1
cd /d !oplpath!

:skippedVMC

for %%A in ( !assets! ) do (
	set folder=%%A

	if "!folder!"=="ART" set "types=*.jpg *.png"
	if "!folder!"=="CFG" set "types=*.cfg"

	if exist !folder! (
		echo Creating !folder! script...
		(
			echo device !hdd!
			echo mount +OPL
			echo mkdir !folder!
			echo cd !folder!
			echo lcd !folder!
			cd !folder!
			for %%X in ( !types! ) do (echo put "%%X")
			echo umount
			echo exit
		) > TMP\!folder!.pfs
		cd /d !oplpath!
		echo 	...running PFS SHELL script to copy !folder! files...
		type TMP\!folder!.pfs | pfsshell >nul 2>&1
		echo 	...done.
rem		del TMP\!folder!.pfs >nul 2>&1
	) else ( echo No !folder! folder found; skipping !folder!...)
)

if defined dopops (
	(
		echo device !hdd!
		echo mount __.POPS
		echo lcd POPS
		for %%A in (*.vcd) do (echo put "%%A")
		echo umount
		echo exit
	) > TMP\POPS.pfs
	echo 	...running PFS SHELL script to copy POPS files...
	type TMP\POPS.pfs | pfsshell >nul 2>&1
rem	del TMP\POPS.pfs 2>nul
	echo 	...done.
	echo	NOTE: Make sure you also install POPS.ELF, IOPRP252.IMG, and POPSTARTER.ELF
	echo 	      in the proper locations. The first two files are copyrighted and not
	echo 	      included in this package, while the third file's name and destination
	echo 	      will depend on your version of OPL, namely daily build vs stable.
)

echo OPLBATCH operations completed.
pause