@echo off
set BatVer=0.89
title %~n0   ( ver %BatVer% )

setlocal enableextensions enabledelayedexpansion
set Xp=0
ver | findstr /i /c:"Version 5." > NUL
if not errorlevel 1 set Xp=1
set X86=0
if /i "%PROCESSOR_ARCHITECTURE%" == "x86" set X86=1

set Scc=
if %Xp% == 0 (
	rem Character encoding: UTF-8
	rem In Windows Vista or later, this file can contain non-English characters as UTF-8.
	chcp 65001 > NUL
	set "Scc=-sccUTF-8"
)
set CodePage=
call :GET_CODE_PAGE CodePage

set Ps=1
call :CHECK_PROGRAM "powershell.exe"
if errorlevel 1 set Ps=0



:MAIN
	set SetupFile=
	set "Arg=%~1"
	set SetupFolder=
	call :GET_FULL_PATH SetupFolder "%~dp0.."
	set Unreg=0
	set B64=0
	set B32=0
	
	set Uac=1
	if %Xp% == 1 set Uac=0
	if %Ps% == 0 set Uac=0
	
	if %Uac% == 0 (
		call :CHECK_ARG
		if errorlevel 2 exit /b 2
		if errorlevel 1 exit /b 1
	) else (
		call :GET_FULL_PATH ArgFile "%TMP%\prArg.txt"
		call :CHECK_UAC_HIGH
		if errorlevel 1 (
			rem Before reopen
			call :CHECK_ARG
			if errorlevel 2 exit /b 2
			if errorlevel 1 exit /b 1
			call :UAC_POST
			if errorlevel 1 exit /b 3
			exit -1
		) else (
			rem After reopen or UAC high
			call :UAC_RECEIVE
			if errorlevel 3 exit /b 3
			if errorlevel 2 exit /b 2
			if errorlevel 1 exit /b 1
		)
	)
	
	if %Unreg% == 1 (
		call :UNREG_MAIN
		if errorlevel 1 exit /b 1
		exit /b 0
	)
	
	set "IncList=%~pd0\potRipper_inc.txt"
	call :CHECK_7Z
	if errorlevel 1 exit /b 4
	
	set LiveVer64=0
	set LiveVer32=0
	call :CHECK_LIVEAPP
	
	echo  ^<^< Extraction of PotPlayer ^>^>
	echo.
	echo  Setup file:
	echo  "%SetupFile%"
	
	set StdInst=0
	set SetupVer=
	call :CHECK_LIST
	if errorlevel 1 exit /b 5
	
	set DllVer64=
	set DllVer32=
	set DllDate64=
	set DllDate32=
	set DllSize64=
	set DllSize32=
	set Company64=
	set Company32=
	call :CHECK_DLL
	if errorlevel 1 exit /b 5
	
	echo.
	echo.
	echo  Installation folder ^( Parent of potRipper's folder ^):
	echo  "%SetupFolder%"
	echo.
	
	call :CHECK_DLL_OLD
	
	set First64=0
	set First32=0
	set FirstOrRepair64=0
	set FirstOrRepair32=0
	call :CHECK_FIRST
	
	call :CHECK_SHARE
	if errorlevel 1 exit /b 6
	
	echo.
	echo.
	echo  Note that exisiting files will be overwritten.
	echo  Close PotPlayer first if it is running.
	echo.
	echo  Press any key to start.
	pause > NUL
	
	set IniFile64=
	set IniFile32=
	call :CHECK_INI_FILE
	
	set Portable64=0
	set Portable32=0
	call :CHECK_PORTABLE
	if errorlevel 1 exit /b 7
	
	call :CHECK_RUNNING
	
	echo.
	echo.
	echo  Now in progress...
	echo.
	echo.
	
	call :EXPAND
	if errorlevel 1 exit /b 8
	
	call :EDIT
	
	echo.
	echo.
	echo  Creation of files completed.
	
	set Reg64=0
	set Reg32=0
	call :CHECK_REG
	call :REG_APP
	if not errorlevel 1 (
		echo.
		echo.
		echo  Registration completed.
	)
	
	echo.
	echo.
	echo  Please confirm the installation / update.
	pause > NUL
	
	rem Stop for arrow keys
	pause > NUL
	
	exit /b 0
	
	
	
rem ------- End MAIN ------------------
	
	
	
:HELP
	set "BatName=%~n0"
	if /i "%BatName:~-2,2%" == "Xp" set "BatName=%BatName:~0,-2%"
	if not exist "%~dp0\%BatName%.txt" (
		echo.
		echo  ERROR: Fail to find the help file.
		echo.
		echo  Please put "%BatName%.txt" in the same folder with "%~nx0".
		pause > NUL
		exit /b 1
	)
	echo.
	type "%~dp0\%BatName%.txt"
	pause > NUL
	exit /b 0
	
	
	
:CHECK_ARG
	if "%Arg%" == "" (
		call :HELP
		exit /b 1
	) else if /i "%Arg%" == "/Unreg64" (
		set Unreg=1
		set B64=1
	) else if /i "%Arg%" == "/Unreg32" (
		set Unreg=1
		set B32=1
	) else if exist "%Arg%" (
		call :GET_FULL_PATH SetupFile "%Arg%"
	) else (
		echo.
		echo  "%Arg%"
		echo.
		echo  ERROR: Fail to find this specified file.
		echo.
		echo  The operation aborted.
		pause > NUL
		exit /b 2
	)
	exit /b 0
	
	
	
:CHECK_PROGRAM
	if "%~$PATH:1" == "" exit /b 1
	exit /b 0
	
	
	
:CHECK_UAC_HIGH
	if %Xp% == 1 exit /b -1
	for /f "tokens=3 delims=\ " %%i in ('whoami /groups ^| findstr /i "Mandatory"') do set UacLevel=%%i
	if /i not "%UacLevel%" == "High" exit /b 1
	exit /b 0
	
	
	
:UAC_POST
	echo.
	echo.
	echo.
	echo  Now reopening as administrator...
	echo.
	echo  Please allow potRipper to make changes by UAC.
	echo.
	echo.
	echo.
	powershell "start-process '%~f0' -verb runas"
		rem powershell -noprofile -executionpolicy unrestricted -command "start-process '%~f0' -verb runas"
		rem The path can not include a symbol character such as !, @, &, %, $, ,...
	if errorlevel 1 (
		echo.
		echo.
		echo  ERROR: Fail to run as administrator.
		echo.
		echo.
		echo  The operation aborted.
		pause > NUL
		exit /b 1
	)
	call :GET_TIME Now0
	echo %Now0%^>%Arg%> "%ArgFile%"
	exit /b 0
	
	
	
:UAC_RECEIVE
	if not "%Arg%" == "" (
		del "%ArgFile%" 2> NUL
		call :CHECK_ARG
		if errorlevel 1 exit /b 2
		exit /b 0
	)
	if not exist "%ArgFile%" (
		call :HELP
		exit /b 1
	)
	for /f "usebackq tokens=1,2 delims=>" %%i in ("%ArgFile%") do (
		set Now3=%%i
		set "Arg=%%j"
	)
	del "%ArgFile%" 2> NUL
	
	call :GET_TIME Now2
	set /a Diff=Now2-Now3
rem echo diff: %Diff%
	if %Diff% LSS 0 set /a Diff += 3600*24*100
	if %Diff% GTR 150 (
		echo.
		echo  ERROR: Fail to receive the argument.
		echo.
		echo  Please start over again.
		pause > NUL
		exit /b 3
	)
	call :CHECK_ARG
	if errorlevel 1 exit /b 2
	exit /b 0
	
	
	
:GET_NUM
	rem Remove top-zero from a 2-digit number to assure non-octal
	set "n=%~2"
	if "%n:~0,1%" == "0" set "n=%n:~1%"
	set %1=%n%
	exit /b 0
	
	
	
:GET_TIME
	set "t=%TIME: =0%"
	set chr=%t:~2,1%
	for /f "tokens=1,2,3,4 delims=%chr%." %%1 in ("%t%") do (
		call :GET_NUM t_h %%1
		call :GET_NUM t_m %%2
		call :GET_NUM t_s %%3
		call :GET_NUM t_ss %%4
	)
	set /a t = %t_h%*3600
	set /a t += %t_m%*60
	set /a t += %t_s%
	set /a t *= 100
	set /a t += %t_ss%
	set %1=%t%
	exit /b 0
	
	
	
:GET_FULL_PATH
	set "%1=%~f2"
	exit /b 0
	
	
	
:GET_FILE_SIZE
	set _Size=0
	if exist "%~2" (
		set "_Size=%~z2"
		if /i "%3" == "k" (
			set /a _Size *= 100
			set /a _Size /= 1024
			set _Size=!_Size:~0,-2!.!_Size:~-2,2!
		) else if /i "%3" == "M" (
			set /a _Size /= 1024
			set /a _Size *= 100
			set /a _Size /= 1024
			set _Size=!_Size:~0,-2!.!_Size:~-2,2!
		) else if /i "%3" == "G" (
			set /a _Size /= 1024
			set /a _Size /= 1024
			set /a _Size *= 100
			set /a _Size /= 1024
			set _Size=!_Size:~0,-2!.!_Size:~-2,2!
		)
	)
	set "%1=%_Size%"
	exit /b 0
	
	
	
:GET_CODE_PAGE
	set _CodePage=
	for /f "tokens=1,2 delims=:" %%i in ('chcp') do (
		if "%%j" == "" exit /b 1
		set "_CodePage=%%j"
	)
	if "%_CodePage:~0,1%" == " " set "_CodePage=%_CodePage:~1%"
	set "%1=%_CodePage%"
	exit /b 0
	
	
	
:CHECK_7Z
	set Path7zip=
	call :_CHECK_7Z Path7zip
	path=%~pd0;%Path7zip%;%ProgramFiles%\7-Zip;%ProgramFiles(x86)%\7-Zip;%PATH%
	call :CHECK_PROGRAM "7z.exe"
	if errorlevel 1 (
		echo.
		echo.
		echo  ERROR: Fail to find "7z.exe".
		echo.
		echo  If you installed "7-Zip" into a location other than the default program folder,
		echo  please add the path of 7-Zip into the environment variable "Path".
		echo.
		echo  Or, put "7z.exe" in the same folder with "%~nx0".
		echo.
		echo.
		echo  The operation aborted.
		pause > NUL
		exit /b 1
	)
	if not exist "%IncList%" (
		echo.
		echo.
		echo  ERROR: Fail to find the list of including files "potRipper_inc.txt".
		echo  Please put it in the same folder with "%~nx0".
		echo.
		echo  "%IncList%"
		echo.
		echo  The operation aborted.
		pause > NUL
		exit /b 1
	)
	exit /b 0
	
	
	
:_CHECK_7Z
	cd /d "%TMP%"
	reg query "HKLM\SOFTWARE\7-Zip" /v "Path" 1> "prReg1.txt" 2> NUL
	if errorlevel 1 (
		if %X86% == 0 (
			reg query "HKLM\SOFTWARE\7-Zip" /v "Path" /reg:32 1> "prReg1.txt" 2> NUL
		)
	)
	if not errorlevel 1 (
		for /f "usebackq tokens=1,2,*" %%i in ("prReg1.txt") do (
			if /i "%%i" == "Path" set "%1=%%k"
		)
	)
	del "prReg1.txt" 2> NUL
	exit /b 0
	
	
	
:CHECK_LIST
	cd /d "%TMP%"
	set Avail=0
	7z l "%SetupFile%" %Scc% 1> "prList1.txt"
	if not errorlevel 1 (
		rem type "prList1.txt" & pause
		echo.
		for /f "usebackq tokens=1,2,4,*" %%a in ("prList1.txt") do (
			if /i "%%d" == "PotPlayer64.dll" (
				if %X86% == 1 (
					echo.
					echo  You can not use 64-bit version in this Windows.
					echo.
				) else (
					call :CHECK_STDINST 1
					if errorlevel 1 (
						echo.
						echo  You can not install 64-bit version in this folder.
						echo  ^( the parent folder of potRipper's location ^)
						echo.
					) else (
						set Avail=1
						set B64=1
					)
				)
			)
			if /i "%%d" == "PotPlayer.dll" (
				call :CHECK_STDINST 0
				if errorlevel 1 (
					echo.
					echo  You can not install 32-bit version in this folder.
					echo  ^( the parent folder of potRipper's location ^)
					echo.
				) else (
					set Avail=1
					set B32=1
				)
			)
		)
	)
	if %Avail% == 1 (
		for /f "usebackq tokens=1,*" %%i in ("prList1.txt") do (
			if /i "%%i" == "ProductVersion:" (
				call :GET_PARSE_POTVER SetupVer "%%j" 0
				if not errorlevel 1 echo  ProductVersion: !SetupVer!
			) else if /i "%%i" == "Comments:" (
				set dt=
				call :GET_PARSE_PM dt "%%j"
				echo  SetupFileDate:  !dt!
			) else if /i "%%i" == "LegalCopyright:" (
				set com=
				call :GET_PARSE_COMPANY com "%%j"
				echo  LegalCopyright: !com!
			)
		)
	)
	del "prList1.txt" 2> NUL
	if %Avail% == 0 (
		call :INFO_STOP
		exit /b 1
	)
	exit /b 0
	
	
	
:INFO_STOP
	echo.
	echo.
	echo  "%SetupFile%"
	echo  This file is not available.
	echo.
	echo.
	echo  Confirm the setup file of PotPlayer.
	echo  Or, change the installation folder.
	echo.
	echo.
	echo  The operation aborted.
	pause > NUL
	exit /b 0
	
	
	
:CHECK_DLL
	if %B64% == 1 (
		7z e "%SetupFile%" -o"%TMP%" -y "PotPlayer64.dll" 1> NUL
		if not errorlevel 1 (
			rem 7z t "PotPlayer64.dll" %Scc% & pause
			for /f "tokens=1,* delims== " %%1 in ('7z t "%TMP%\PotPlayer64.dll" %Scc%') do (
				if /i "%%1" == "Created" set "DllDate64=%%2"
				if /i "%%1" == "FileVersion:" call :GET_PARSE_POTVER DllVer64 "%%2" 0
				if /i "%%1" == "CompanyName:" set "Company64=%%2"
			)
			call :GET_FILE_SIZE DllSize64 "%TMP%\PotPlayer64.dll" M
			del /f "%TMP%\PotPlayer64.dll" 2> NUL
		)
		if "!DllVer64!" == "" (
			echo.
			echo  ERROR: Fail to get the information from DLL.
			echo.
			call :INFO_STOP
			pause > NUL
			exit /b 1
		)
	)
	if %B32% == 1 (
		7z e "%SetupFile%" -o"%TMP%" -y "PotPlayer.dll" 1> NUL
		if not errorlevel 1 (
			rem 7z t "PotPlayer.dll" %Scc% & pause
			for /f "tokens=1,* delims== " %%1 in ('7z t "%TMP%\PotPlayer.dll" %Scc%') do (
				if /i "%%1" == "Created" set "DllDate32=%%2"
				if /i "%%1" == "FileVersion:" call :GET_PARSE_POTVER DllVer32 "%%2" 0
				if /i "%%1" == "CompanyName:" set "Company32=%%2"
			)
			call :GET_FILE_SIZE DllSize32 "%TMP%\PotPlayer.dll" M
			del /f "%TMP%\PotPlayer.dll" 2> NUL
		)
		if "!DllVer32!" == "" (
			echo.
			echo  ERROR: Fail to get the information from DLL.
			echo.
			call :INFO_STOP
			pause > NUL
			exit /b 1
		)
	)
	if "!SetupVer!" == "" (
		if not "!DllVer64!" == "" (
			call :GET_PARSE_POTVER SetupVer "!DllVer64!" 1
		) else if not "!DllVer32!" == "" (
			call :GET_PARSE_POTVER SetupVer "!DllVer32!" 1
		)
		if not "!SetupVer!" == "" echo  ProductVersion: !SetupVer!
	)
	echo.
	if %B64% == 1 (
		if not "!DllDate64!" == "" echo  PotPlayer64.dll Date:    !DllDate64! ^(SystemTime^)
		echo  PotPlayer64.dll Version: !DllVer64!  ^(64-bit^)
		if not "!DllSize64!" == "" echo  PotPlayer64.dll Size:    !DllSize64! MB
	)
	if %B32% == 1 (
		if not "!DllDate32!" == "" echo  PotPlayer.dll Date:    !DllDate32! ^(SystemTime^)
		echo  PotPlayer.dll Version: !DllVer32!  ^(32-bit^)
		if not "!DllSize32!" == "" echo  PotPlayer.dll Size:    !DllSize32! MB
	)
	echo.
	exit /b 0
	
	
	
:CHECK_DLL_OLD
	if %B64% == 1 (
		if exist "%SetupFolder%\PotPlayer64.dll" (
			set DllDateOld64=
			set DllVerOld64=
			for /f "tokens=1,* delims== " %%1 in ('7z t "%SetupFolder%\PotPlayer64.dll" %Scc%') do (
				if /i "%%1" == "Created" set "DllDateOld64=%%2"
				if /i "%%1" == "FileVersion:" call :GET_PARSE_POTVER DllVerOld64 "%%2" 0
			)
			set DllSizeOld64=
			call :GET_FILE_SIZE DllSizeOld64 "%SetupFolder%\PotPlayer64.dll" M
			if not "!DllDateOld64!" == "" echo  Your Present DLL Date:    !DllDateOld64! ^(SystemTime^)
			if not "!DllVerOld64!" == "" echo  Your Present DLL Version: !DllVerOld64!  ^(64-bit^)
			if not "!DllSizeOld64!" == "" echo  Your Present DLL Size:    !DllSizeOld64! MB
		)
	)
	if %B32% == 1 (
		if exist "%SetupFolder%\PotPlayer.dll" (
			set DllDateOld32=
			set DllVerOld32=
			for /f "tokens=1,* delims== " %%1 in ('7z t "%SetupFolder%\PotPlayer.dll" %Scc%') do (
				if /i "%%1" == "Created" set "DllDateOld32=%%2"
				if /i "%%1" == "FileVersion:" call :GET_PARSE_POTVER DllVerOld32 "%%2" 0
			)
			set DllSizeOld32=
			call :GET_FILE_SIZE DllSizeOld32 "%SetupFolder%\PotPlayer.dll" M
			if not "!DllDateOld32!" == "" echo  Your Present DLL Date:    !DllDateOld32! ^(SystemTime^)
			if not "!DllVerOld32!" == "" echo  Your Present DLL Version: !DllVerOld32!  ^(32-bit^)
			if not "!DllSizeOld32!" == "" echo  Your Present DLL Size:    !DllSizeOld32! MB
		)
	)
	exit /b 0
	
	
	
:GET_PARSE_POTVER
	set "_Ver=%~2"
	if not "%_Ver:~0,2%" == "1." exit /b 1
	if "%_Ver:~-2,2%" == ".0" set "_Ver=%_Ver:~0,-2%"
	for /f "tokens=1,2,3 delims=." %%i in ("%_Ver%") do (
		if "%%k" == "" exit /b 1
		if "%%k" == "0" exit /b 1
		if "%3" == "1" (
			rem get SetupVer from DllVer
			if "%_Ver:~0,4%" == "1.6." (
				if %%k LSS 55478 (
					if %%k NEQ 55390 (
						if %%k GTR 48845 (
							rem In old versions, the product number is higher than the dll number by one.
							set n=%%k
							set /a n += 1
							set "_Ver=1.6.!n!"
						) else if %%k EQU 48845 (
							rem The number of this version is quite abnormal.
							set "_Ver=1.6.48850"
						)
					)
				)
			) else if "%_Ver:~0,4%" == "1.7." (
				if %%k GTR 3795 (
					exit /b 2
				)
			)
		)
	)
	set "%1=%_Ver%"
	exit /b 0
	
	
	
:GET_PARSE_PM
	set "_dt=%~2"
	for /f "tokens=2 delims=(" %%i in ("%_dt%") do (
		set "_dt=%%i"
	)
	if not "%CodePage%" == "65001" (
		rem Can not read unicode.
		set "%1=%_dt:~0,10% (UTC +09:00)"
		exit /b 0
	)
	for /f "tokens=1,2 delims=:" %%1 in ("%_dt:~14,-1%") do (
		set "t_h=%%1"
		set "t_m=%%2"
	)
	if "%_dt:~11,2%" == "오후" (
		call :GET_NUM t_h "%t_h%"
		set /a t_h += 12
	)
	set "%1=%_dt:~0,10% %t_h%:%t_m% (UTC +09:00)"
	exit /b 0
	
	
	
:GET_PARSE_COMPANY
	set "_com=%~2"
	if not "%CodePage%" == "65001" (
		if "%_com:~0,2%" == "_ " set _com=%_com:~2%
	)
	if /i "%_com:~-21%" == " All rights reserved." (
		set "_com=%_com:~0,-21%"
	)
	set "%1=%_com%"
	exit /b 0
	
	
	
:CHECK_STDINST
	if %Xp% == 1 exit /b 0
	echo "%SetupFolder%\" | findstr /i /c:"%ProgramFiles%\\" > NUL
	if not errorlevel 1 (
		set StdInst=1
		if %1 == 0 (
			if %X86% == 0 exit /b 1
		)
	)
	echo "%SetupFolder%\" | findstr /i /c:"%ProgramFiles(x86)%\\" > NUL
	if not errorlevel 1 (
		set StdInst=1
		if %1 == 1 exit /b 1
		if %X86% == 1 exit /b 1
	)
	exit /b 0
	
	
	
:CHECK_FIRST
	if %B64% == 1 (
		if not exist "%SetupFolder%\PotPlayer64.dll" set First64=1
		if !First64! == 1 set FirstOrRepair64=1
		if "%DllDate64%" == "%DllDateOld64%" set FirstOrRepair64=1
	)
	if %B32% == 1 (
		if not exist "%SetupFolder%\PotPlayer.dll" set First32=1
		if !First32! == 1 set FirstOrRepair32=1
		if "%DllDate32%" == "%DllDateOld32%" set FirstOrRepair32=1
	)
	exit /b 0
	
	
	
:CHECK_SHARE
	if %First64% == 1 (
		if exist "%SetupFolder%\PotPlayer.dll" (
			call :ASK_SHARE 1
			if errorlevel 1 exit /b 1
		)
	) else if %First32% == 1 (
		if exist "%SetupFolder%\PotPlayer64.dll" (
			call :ASK_SHARE 0
			if errorlevel 1 exit /b 1
		)
	)
	exit /b 0
	
	
	
:ASK_SHARE
	echo.
	echo.
	if %1 == 1 (
		echo  This folder already has PotPlayer 32-bit.
		echo  Will you really install PotPlayer 64-bit in the same folder?
	) else (
		echo  This folder already has PotPlayer 64-bit.
		echo  Will you really install PotPlayer 32-bit in the same folder?
	)
	echo  ^( Not recommended ^)
	echo.
	echo  Input 'S' to continue, or another character to abort.
	set Ret=
	set /p Ret=">"
	if /i not "%Ret%" == "S" (
		echo.
		echo.
		echo  The operation aborted.
		pause > NUL
		exit /b 1
	)
	exit /b 0
	
	
	
:CHECK_LIVEAPP
	cd /d "%~dp0"
	rem "findstr" can not open a file if the path of it includes non-ASCII characters.
	if %B64% == 1 (
		if %Xp% == 1 (
			findstr /i /r /c:"^ *PotPlayerXP64\.exe *$" "potRipper_inc.txt" > NUL
		) else (
			findstr /i /r /c:"^ *PotPlayer64\.exe *$" "potRipper_inc.txt" > NUL
		)
		if not errorlevel 1 set LiveVer64=1
	)
	if %B32% == 1 (
		if %Xp% == 1 (
			findstr /i /r /c:"^ *PotPlayerXP\.exe *$" "potRipper_inc.txt" > NUL
		) else (
			findstr /i /r /c:"^ *PotPlayer\.exe *$" "potRipper_inc.txt" > NUL
		)
		if not errorlevel 1 set LiveVer32=1
	)
	exit /b 0
	
	
	
:CHECK_INI_FILE
	if %StdInst% == 1 (
		if %B64% == 1 (
			set "IniFile64=%APPDATA%\PotPlayerMini64\PotPlayerMini64.ini"
		)
		if %B32% == 1 (
			set "IniFile32=%APPDATA%\PotPlayerMini\PotPlayerMini.ini"
		)
	) else (
		if %B64% == 1 (
			set "IniFile64=%SetupFolder%\PotPlayerMini64.ini"
		)
		if %B32% == 1 (
			set "IniFile32=%SetupFolder%\PotPlayerMini.ini"
		)
	)
	exit /b 0
	
	
	
:CHECK_PORTABLE
	if %B64% == 1 (
		call :GET_FILE_SIZE Size "%IniFile64%"
		if not !Size! == 0 (
			if %StdInst% == 0 (
				set Portable64=1
			) else (
				set Portable64=2
			)
		) else if %First64% == 1 (
			if %StdInst% == 0 (
				call :ASK_PORTABLE "%IniFile64%" 1
				if errorlevel 2 exit /b 1
				if errorlevel 1 set Portable64=1
			)
		)
	)
	if %B32% == 1 (
		call :GET_FILE_SIZE Size "%IniFile32%"
		if not !Size! == 0 (
			if %StdInst% == 0 (
				set Portable32=1
			) else (
				set Portable32=2
			)
		) else if %First32% == 1 (
			if %StdInst% == 0 (
				call :ASK_PORTABLE "%IniFile32%" 0
				if errorlevel 2 exit /b 1
				if errorlevel 1 set Portable32=1
			)
		)
	)
	exit /b 0
	
	
	
:ASK_PORTABLE
	echo.
	echo.
	if %2 == 1 (
		echo  Which do you like to store settings of PotPlayer 64-bit?
	) else (
		echo  Which do you like to store settings of PotPlayer 32-bit?
	)
	echo.
	echo  R: Windows registry ^( shared in PC ^)
	echo  I: New INI file ^( for portable use ^)
	echo.
	echo  Even if you choose the registry,
	echo  You can transfer settings to the INI file later by Preferences panel.
	echo.
	echo  If you choose the INI file and keep using it,
	echo  settings existing in the registry would be ignored ^( except for File
	echo  Association ^) and potRipper will not edit the registry for PotPlayer.
	echo.
	:jump_01
	echo  Input only one character, 'R' or 'I'.
	set Ret=
	set /p Ret=">"
	echo.
	echo.
	if /i "%Ret%" == "I" (
		copy NUL "%~f1" /a 1> NUL
		if errorlevel 1 (
			echo  ERROR: Fail to create a new INI file in this folder;
			echo  "%~pd1"
			echo.
			echo  The operation aborted.
			pause > NUL
			exit /b 2
		)
		echo  A new INI file created;
		echo  "%~f1"
		echo.
		echo  Press any key to continue.
		pause > NUL
		exit /b 1
	) else if /i not "%Ret%" == "R" (
		goto jump_01
	)
	exit /b 0
	
	
	
:CHECK_RUNNING
	if not exist "%SystemRoot%\System32\tasklist.exe" exit /b 0
	if %B64% == 1 (
		:jump_11
		tasklist | findstr /i "PotPlayerMini64.exe PotPlayer64.exe" > NUL
		if not errorlevel 1 (
			echo.
			echo.
			echo  PotPlayer 64-bit is running now.
			echo  Close PotPlayer first, and press any key to continue.
			pause > NUL
			goto jump_11
		)
	)
	if %B32% == 1 (
		:jump_12
		tasklist | findstr /i "PotPlayerMini.exe PotPlayer.exe" > NUL
		if not errorlevel 1 (
			echo.
			echo.
			echo  PotPlayer 32-bit is running now.
			echo  Close PotPlayer first, and press any key to continue.
			pause > NUL
			goto jump_12
		)
	)
	exit /b 0
	
	
	
:EXPAND
	cd /d "%SetupFolder%"
	if %B64% == 1 call :_EXPAND "PotPlayer64.dll"
	if errorlevel 1 exit /b 1;
	if %B32% == 1 call :_EXPAND "PotPlayer.dll"
	if errorlevel 1 exit /b 1;
	7z x "%SetupFile%" -o"%SetupFolder%" -r -y -ir@"%IncList%" -scsUTF-8 %Scc% 1> NUL
	if errorlevel 1 (
		echo.
		echo.
		echo  ERROR: Fail to create some file^(s^) by 7-Zip.
		echo  Confirm that the files are not being used.
		echo.
		echo  Other files are already updated if accessible.
	)
	exit /b 0
	
	
	
:_EXPAND
	7z x "%SetupFile%" -o"%SetupFolder%" "%~1" -r -y %Scc% 1> NUL
	if errorlevel 1 (
		echo.
		echo  ERROR: Fail to create "%~1" by 7-Zip.
		echo  Confirm that the file is not being used.
		call :CHECK_UAC_HIGH
		if errorlevel 1 (
			echo.
			echo.
			echo  ======================================================================
			echo     If files are not accessible even though they are not being used,
			echo     please install "Windows PowerShell" correctly.
			echo  ======================================================================
			echo.
		)
		echo.
		echo  The operation aborted.
		pause > NUL
		pause > NUL
		exit /b 1
	)
	exit /b 0
	
	
	
:EDIT
	rem For a security reason, the "dead" programs are removed.
	cd /d "%SetupFolder%"
rem	del /f uninstall.exe 2> NUL
	if %Xp% == 0 (
		del /f PotPlayerXP.exe 2> NUL
		del /f PotPlayerXP64.exe 2> NUL
		del /f PotPlayerMiniXP.exe 2> NUL
		del /f PotPlayerMiniXP64.exe 2> NUL
		if "X86" == 1 (
			del /f PotPlayer64.exe 2> NUL
			del /f PotPlayerMini64.exe 2> NUL
		) else if %LiveVer64% == 0 (
			del /f PotPlayer64.exe 2> NUL
		)
		if %LiveVer32% == 0 (
			del /f PotPlayer.exe 2> NUL
		)
		exit /b 0
	)
	
	rem For Windows XP only from here
	if exist PotPlayerMiniXP.exe (
		del /f PotPlayerMini.exe 2> NUL
		rename PotPlayerMiniXP.exe PotPlayerMini.exe 2> NUL
		if errorlevel 1 (
			echo.
			echo.
			echo  ERROR: Fail to change the file name of "PotPlayerMiniXP.exe" into "PotPlayerMini.exe".
			echo  Please use "PotPlayerMiniXP.exe" to run PotPlayerMini.
		)
	)
	if %LiveVer32% == 0 (
		del /f PotPlayer.exe 2> NUL
		del /f PotPlayerXP.exe 2> NUL
	) else (
		if exist PotPlayerXP.exe (
			del /f PotPlayer.exe 2> NUL
			rename PotPlayerXP.exe PotPlayer.exe 2> NUL
			if errorlevel 1 (
				echo.
				echo.
				echo  ERROR: Fail to change the file name of "PotPlayerXP.exe" into "PotPlayer.exe".
				echo  Please use "PotPlayerXP.exe" to run PotPlayer.
			)
		)
	)
	if "X86" == 1 (
		del /f PotPlayer64.exe 2> NUL
		del /f PotPlayerXP64.exe 2> NUL
		del /f PotPlayerMini64.exe 2> NUL
		del /f PotPlayerMiniXP64.exe 2> NUL
		exit /b 0
	)
	
	if exist PotPlayerMiniXP64.exe (
		del /f PotPlayerMini64.exe 2> NUL
		rename PotPlayerMiniXP64.exe PotPlayerMini64.exe 2> NUL
		if errorlevel 1 (
			echo.
			echo.
			echo  ERROR: Fail to change the file name of "PotPlayerMiniXP64.exe" into "PotPlayerMini64.exe".
			echo  Please use "PotPlayerMiniXP64.exe" to run PotPlayerMini 64-bit.
		)
	)
	if %LiveVer64% == 0 (
		del /f PotPlayer64.exe 2> NUL
		del /f PotPlayerXP64.exe 2> NUL
	) else (
		if exist PotPlayerXP64.exe (
			del /f PotPlayer64.exe 2> NUL
			rename PotPlayerXP64.exe PotPlayer64.exe 2> NUL
			if errorlevel 1 (
				echo.
				echo.
				echo  ERROR: Fail to change the file name of "PotPlayerXP64.exe" into "PotPlayer64.exe".
				echo  Please use "PotPlayerXP64.exe" to run PotPlayer 64-bit.
			)
		)
	)
	exit /b 0
	
	
	
:CHECK_REG
	if %Xp% == 1 exit /b 1
	
	call :CHECK_UAC_HIGH
	if errorlevel 1 (
		echo.
		echo.
		echo  Can not edit the registry for PotPlayer.
		set /a FirstOrRepair = %FirstOrRepair64% | %FirstOrRepair32%
		if !FirstOrRepair! == 1 (
			echo.
			echo  Please install "Windows PowerShell" to edit the registry
			echo  when sending a setup file to potRipper from File Explorer.
		)
		exit /b 1
	)
	
	if %B64% == 1 (
		if not %Portable64% == 1 (
			set RegFolder64=
			call :GET_REG_FOLDER RegFolder64 1
			if errorlevel 1 (
				set Reg64=2
			) else (
				if /i "!RegFolder64!" == "%SetupFolder%" (
					set Reg64=1
				) else (
					if %FirstOrRepair64% == 1 (
						call :ASK_REG 1 1
						if not errorlevel 1 set Reg64=2
					)
				)
			)
		)
	)
	if %B32% == 1 (
		if not %Portable32% == 1 (
			set RegFolder32=
			call :GET_REG_FOLDER RegFolder32 0
			if errorlevel 1 (
				set Reg32=2
			) else (
				if /i "!RegFolder32!" == "%SetupFolder%" (
					set Reg32=1
				) else (
					if %FirstOrRepair32% == 1 (
						call :ASK_REG 0 1
						if not errorlevel 1 set Reg32=2
					)
				)
			)
		)
	)
	exit /b 0
	
	
	
:GET_REG_FOLDER
	cd /d "%TMP%"
	set _RegFolder=
	if %2 == 1 (
		reg query "HKLM\SOFTWARE\DAUM\PotPlayer64" /v "ProgramFolder" /reg:64 1> "prReg2.txt" 2> NUL
	) else (
		reg query "HKLM\SOFTWARE\DAUM\PotPlayer" /v "ProgramFolder" /reg:32 1> "prReg2.txt" 2> NUL
	)
	if not errorlevel 1 (
		for /f "usebackq tokens=1,2,*" %%i in ("prReg2.txt") do (
			if /i "%%i" == "ProgramFolder" set "_RegFolder=%%k"
		)
	)
	del "prReg2.txt" 2> NUL
	if "%_RegFolder%" == "" exit /b 1
	call :GET_FULL_PATH %1 "%_RegFolder%"
	exit /b 0
	
	
	
:ASK_REG
	echo.
	echo.
	if %1 == 1 (
		if %2 == 1 (
			echo  The registry already has the key of PotPlayer 64-bit in another folder.
			echo  "%RegFolder64%"
			echo.
			echo  Do you want to change the registered path to this new one?
			echo  "%SetupFolder%"
		) else (
			echo  Will you add keys / values about PotPlayer 64-bit in the registry?
		)
	) else (
		if %2 == 1 (
			echo  The registry already has the key of PotPlayer 32-bit in another folder.
			echo  "%RegFolder32%"
			echo.
			echo  Do you want to change the registered path to this new one?
			echo  "%SetupFolder%"
		) else (
			echo  Will you add keys / values about PotPlayer 32-bit in the registry?
		)
	)
	echo.
	echo  Input only one character, 'R' ^(Register^) or another ^(skip this^).
	set Ret=
	set /p Ret=">"
	echo.
	echo.
	if /i not "%Ret%" == "R" exit /b 1
	exit /b 0
	
	
	
:REG_APP
	if %Reg64% == 0 (
		if %Reg32% == 0 exit /b 1
	)
	call :CHECK_RUNNING
	set Fsize=0
	call :GET_FOLDER_SIZE Fsize "%SetupFolder%"
	set /a Fsize /= 1024
	set "PrBat=%~f0"
	if %Reg64% GTR 0 (
		if %Reg64% == 2 (
			call :UNREG_ASC 1
			if errorlevel 1 (
				echo.
				echo  The registration aborted.
				exit /b 1
			)
		)
		
		call :SHORTCUT 1
		
		reg add "HKLM\SOFTWARE\DAUM\PotPlayer64" /v "ProgramFolder" /d "%SetupFolder%" /f /reg:64 1> NUL
		if errorlevel 1 (
			echo.
			echo.
			echo  ERROR: Fail to edit the registry.
			exit /b 1
		) else (
			reg add "HKLM\SOFTWARE\DAUM\PotPlayer64" /v "ProgramPath" /d "%SetupFolder%\PotPlayerMini64.exe" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini64.exe" /ve /d "%SetupFolder%\PotPlayerMini64.exe" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini64.exe" /v "Path" /d "%SetupFolder%" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini64.exe" /ve /d "%SetupFolder%\PotPlayerMini64.exe" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini64.exe" /v "Path" /d "%SetupFolder%" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer64" /v "DisplayIcon" /d "%SetupFolder%\PotPlayerMini64.exe" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer64" /v "DisplayName" /d "PotPlayer 64-bit" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer64" /v "DisplayVersion" /d "%SetupVer%" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer64" /v "EstimatedSize" /t REG_DWORD /d "%Fsize%" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer64" /v "Publisher" /d "%Company64% Corp." /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer64" /v "UninstallString" /d "\"%PrBat:)=^)%\" /Unreg64" /f /reg:64 1> NUL
			reg add "HKCU\Software\Daum\PotPlayer64" /v "ProgramFolder" /d "%SetupFolder%" /f 1> NUL
			reg add "HKCU\Software\Daum\PotPlayer64" /v "ProgramPath" /d "%SetupFolder%\PotPlayerMini64.exe" /f 1> NUL
			reg add "HKCU\Software\Daum\PotPlayer64" /v "ProgramVersion" /d "v%SetupVer%" /f 1> NUL
			reg add "HKCR\Applications\PotPlayerMini64.exe\shell\open\command" /ve /d "\"%SetupFolder:)=^)%\PotPlayerMini64.exe\" \"%%1\"" /f 1> NUL
			)
		)
	)
	if %Reg32% GTR 0 (
		if %Reg32% == 2 (
			call :UNREG_ASC 1
			if errorlevel 1 (
				echo.
				echo  The registration aborted.
				exit /b 1
			)
		)
		
		call :SHORTCUT 0
		
		rem Redirect from "HKLM\SOFTWARE\" to "HKLM\SOFTWARE\WOW6432Node\" if Windows x64
		reg add "HKLM\SOFTWARE\DAUM\PotPlayer" /v "ProgramFolder" /d "%SetupFolder%" /f /reg:32 1> NUL
		if errorlevel 1 (
			echo.
			echo.
			echo  ERROR: Fail to edit the registry.
			exit /b 1
		) else (
			reg add "HKLM\SOFTWARE\DAUM\PotPlayer" /v "ProgramPath" /d "%SetupFolder%\PotPlayerMini.exe" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini.exe" /ve /d "%SetupFolder%\PotPlayerMini.exe" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini.exe" /v "Path" /d "%SetupFolder%" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini.exe" /ve /d "%SetupFolder%\PotPlayerMini.exe" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini.exe" /v "Path" /d "%SetupFolder%" /f /reg:64 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer" /v "DisplayIcon" /d "%SetupFolder%\PotPlayerMini.exe" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer" /v "DisplayName" /d "PotPlayer 32-bit" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer" /v "DisplayVersion" /d "%SetupVer%" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer" /v "EstimatedSize" /t REG_DWORD /d "%Fsize%" /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer" /v "Publisher" /d "%Company32% Corp." /f /reg:32 1> NUL
			reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer" /v "UninstallString" /d "\"%PrBat:)=^)%\" /Unreg32" /f /reg:32 1> NUL
			reg add "HKCU\Software\Daum\PotPlayer" /v "ProgramFolder" /d "%SetupFolder%" /f  1> NUL
			reg add "HKCU\Software\Daum\PotPlayer" /v "ProgramPath" /d "%SetupFolder%\PotPlayerMini.exe" /f  1> NUL
			reg add "HKCU\Software\Daum\PotPlayer" /v "ProgramVersion" /d "v%SetupVer%" /f  1> NUL
			reg add "HKCR\Applications\PotPlayerMini.exe\shell\open\command" /ve /d "\"%SetupFolder:)=^)%\PotPlayerMini.exe\" \"%%1\"" /f 1> NUL
		)
	)
	exit /b 0
	
	
	
:SHORTCUT
	if %Ps% == 0 exit /b 1
	set "StartDaumFolder=%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Daum"
	if %1 == 1 (
		powershell "$wsh=New-Object -ComObject Wscript.Shell; $sc=$wsh.CreateShortCut('%SetupFolder%\Unreg64.lnk'); $sc.TargetPath='%PrBat%'; $sc.Arguments='/Unreg64'; $sc.save()" 2> NUL
		if errorlevel 1 (
			if %FirstOrRepair64% == 1 (
				echo.
				echo.
				echo  ERROR: Fail to make shortcut files.
				echo  Confirm that unexpected characters are not included in the path.
			)
			exit /b 1
		) else (
			if not exist "%StartDaumFolder%\PotPlayer 64 bit" mkdir "%StartDaumFolder%\PotPlayer 64 bit"
			powershell "$wsh=New-Object -ComObject Wscript.Shell; $sc=$wsh.CreateShortCut('%StartDaumFolder%\PotPlayer 64 bit\PotPlayer 64 bit.lnk'); $sc.TargetPath='%SetupFolder%\PotPlayerMini64.exe'; $sc.save()"
			powershell "$wsh=New-Object -ComObject Wscript.Shell; $sc=$wsh.CreateShortCut('%StartDaumFolder%\PotPlayer 64 bit\Unreg64.lnk'); $sc.TargetPath='%PrBat%'; $sc.Arguments='/Unreg64'; $sc.save()"
		)
	) else (
		powershell "$wsh=New-Object -ComObject Wscript.Shell; $sc=$wsh.CreateShortCut('%SetupFolder%\Unreg32.lnk'); $sc.TargetPath='%PrBat%'; $sc.Arguments='/Unreg32'; $sc.save()" 2> NUL
		if errorlevel 1 (
			if %FirstOrRepair32% == 1 (
				echo.
				echo.
				echo  ERROR: Fail to make shortcut files.
				echo  Confirm that unexpected characters are not included in the path.
			)
			exit /b 1
		) else (
			if not exist "%StartDaumFolder%\PotPlayer" mkdir "%StartDaumFolder%\PotPlayer"
			powershell "$wsh=New-Object -ComObject Wscript.Shell; $sc=$wsh.CreateShortCut('%StartDaumFolder%\PotPlayer\PotPlayer.lnk'); $sc.TargetPath='%SetupFolder%\PotPlayerMini.exe'; $sc.save()"
			powershell "$wsh=New-Object -ComObject Wscript.Shell; $sc=$wsh.CreateShortCut('%StartDaumFolder%\PotPlayer\Unreg32.lnk'); $sc.TargetPath='%PrBat%'; $sc.Arguments='/Unreg32'; $sc.save()"
		)
	)
	exit /b 0
	
	
	
:GET_FOLDER_SIZE
	set _Fsize=
	set _Num=0
	if exist "%~f2" (
		for /f "tokens=1,2,3" %%i in ('dir "%~f2" /s /-c') do (
			if %CodePage% == 932 (
				if "%%j" == "個のファイル" (
					set _Fsize=%%k
					set _Num=%%i
				)
			) else (
				if /i "%%j" == "File(s)" (
					set _Fsize=%%k
					set _Num=%%i
				)
			)
		)
	)
	if /i "%1" == "0" (
		if %_Num% == 0 exit /b 1
		exit /b 0
	)
	if "%_Fsize%" == "" exit /b 1
	set %1=%_Fsize%
	exit /b 0
	
	
	
:CHECK_DIR_EMPTY
	call :GET_FOLDER_SIZE 0 "%~1"
	if errorlevel 1 exit /b 1
	exit /b 0
	
	
	
:UNREG_MAIN
	echo.
	call :CHECK_UAC_HIGH
	if errorlevel 1 (
		echo  ^<^< Unregister PotPlayer ^>^>
		echo.
		echo.
		echo  Can not edit the registry.
		echo  Run potRipper as administrator with the command-line switch,
		echo  "/Unreg64" or "/Unreg32".
		echo.
		echo  The operation aborted.
		pause > NUL
		pause > NUL
		exit /b 1
	)
	
	if %B64% == 1 (
		echo  ^<^< Unregister PotPlayer 64-bit ^>^>
		echo.
		echo  PotPlayer 64-bit in this folder will be unregistered.
	) else if %B32% == 1 (
		echo  ^<^< Unregister PotPlayer 32-bit ^>^>
		echo.
		echo  PotPlayer 32-bit in this folder will be unregistered.
	) else (
		exit /b 1
	)
	echo.
	echo  "%SetupFolder%"
	echo.
	
	if %B64% == 1 (
		call :CHECK_UNREG 1
	) else (
		call :CHECK_UNREG 0
	)
	if errorlevel 1 exit /b 1
	
	echo.
	echo  PotPlayer's settings in the registry will be also deleted.
	echo  But any folders or files will NOT be deleted by this operation.
	echo  ^(Except for shortcuts^)
	echo.
	echo  Close PotPlayer first if it is running.
	echo.
	echo  Press any key to start.
	pause > NUL
	
	call :UNREG_ASC 0
	call :UNREG_APP
	call :UNREG_SET
	echo.
	echo.
	echo  Unregistration completed.
	echo.
	echo.
	call :INFO_FOLDER
	echo.
	echo.
	echo  All completed.
	pause > NUL
	pause > NUL
	exit /b 0
	
	
	
:CHECK_UNREG
	set RegFolder=
	call :GET_REG_FOLDER RegFolder %1
	if not "%RegFolder%" == "" (
		if /i not "%RegFolder%" == "%SetupFolder%" (
			echo  This installation folder is not registered with the system.
			echo  To remove registration here may affect another PotPlayer.
			echo.
			if %1 == 1 (
				echo  Will you unregister PotPlayer 64-bit anyway?
			) else (
				echo  Will you unregister PotPlayer 32-bit anyway?
			)
			echo.
			echo  Input 'U' to continue, or another character to abort.
			set Ret=
			set /p Ret=">"
			if /i not "%Ret%" == "U" (
				echo.
				echo.
				echo  The operation aborted.
				pause > NUL
				exit /b 1
			)
		)
	)
	exit /b 0
	
	
	
:UNREG_ASC
	rem Unregister file association
	call :CHECK_RUNNING
	if %B64% == 1 (
		"%SetupFolder%\PotPlayerMini64.exe" /Unregister
		if errorlevel 1 (
			echo.
			echo.
			echo  ERROR: Fail to unregister file association with PotPlayerMini64.
			echo.
		) else (
			if %1 == 1 (
				echo.
				echo.
				echo  File association with PotPlayer 64-bit is cleared now.
				echo  Use Association settings in Preferences panel of PotPlayer.
				echo.
			)
		)
	)
	if %B32% == 1 (
		"%SetupFolder%\PotPlayerMini.exe" /Unregister
		if errorlevel 1 (
			echo.
			echo.
			echo  ERROR: Fail to unregister file association with PotPlayerMini.
			echo.
			exit /b 1
		) else (
			if %1 == 1 (
				echo.
				echo.
				echo  File association with PotPlayer 32-bit is cleared now.
				echo  Use Association settings in Preferences panel of PotPlayer.
				echo.
			)
		)
	)
	exit /b 0
	
	
	
:UNREG_APP
	rem Remove application from the system
	call :CHECK_RUNNING
	set "StartDaumFolder=%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Daum"
	if %B64% == 1 (
		rem Delete start menu or shortcuts.
		rmdir /s /q "%StartDaumFolder%\PotPlayer 64 bit"
		call :CHECK_DIR_EMPTY "%StartDaumFolder%"
		if errorlevel 1 rmdir /s /q "%StartDaumFolder%"
		del /f "%SetupFolder%\Unreg64.lnk"
		
		rem Delete reg keys.
		reg query "HKLM\SOFTWARE\DAUM" /f "PotPlayer64" /k /e /reg:64 1> NUL 2> NUL
		if not errorlevel 1 (
			reg delete "HKLM\SOFTWARE\DAUM\PotPlayer64" /f /reg:64 1> NUL
			if errorlevel 1 (
				echo.
				echo  ERROR: Fail to unregister PotPlayerMini64.
				echo.
				exit /b 1
			)
		)
		reg query "HKLM\SOFTWARE\DAUM" /f * /reg:64 1> NUL 2> NUL
		if errorlevel 1 reg delete "HKLM\SOFTWARE\DAUM" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer64" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini64.exe" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini64.exe" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\RegisteredApplications" /v "PotPlayerMini64" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\RegisteredApplications" /v "PotPlayerMini64" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayerMini64" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayerMini64" /f /reg:32 1> NUL 2> NUL
		reg delete "HKCR\Applications\PotPlayerMini64.exe" /f 1> NUL 2> NUL
		reg delete "HKCR\Applications\PotPlayer64.exe" /f 1> NUL 2> NUL
		reg delete "HKCU\Software\Daum" /v "GUID_POTPLAYER" /f 1> NUL 2> NUL
		
		rem For PotPlayer Live version. (cope partially)
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayer64" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayer64" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayerLite64" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayerLite64" /f /reg:32 1> NUL 2> NUL
		reg delete "HKCU\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\PotPlayerMini64.exe" /f /reg:64 1> NUL 2> NUL
		reg delete "HKCU\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION\PotPlayerMini64.exe" /f /reg:64 1> NUL 2> NUL
		reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "PotNotify64" /f 1> NUL 2> NUL
	)
	if %B32% == 1 (
		rem Delete start menu or shortcuts.
		rmdir /s /q "%StartDaumFolder%\PotPlayer"
		call :CHECK_DIR_EMPTY "%StartDaumFolder%"
		if errorlevel 1 rmdir /s /q "%StartDaumFolder%"
		del /f "%SetupFolder%\Unreg32.lnk"
		
		rem Delete reg keys.
		reg query "HKLM\SOFTWARE\DAUM" /f "PotPlayer" /k /e /reg:32 1> NUL 2> NUL
		if not errorlevel 1 (
			reg delete "HKLM\SOFTWARE\DAUM\PotPlayer" /f /reg:32 1> NUL
			if errorlevel 1 (
				echo.
				echo  ERROR: Fail to unregister PotPlayerMini.
				echo.
				exit /b 1
			)
		)
		reg query "HKLM\SOFTWARE\DAUM" /f * /reg:32 1> NUL 2> NUL
		if errorlevel 1 reg delete "HKLM\SOFTWARE\DAUM" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PotPlayer" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini.exe" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\PotPlayerMini.exe" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\RegisteredApplications" /v "PotPlayerMini" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\RegisteredApplications" /v "PotPlayerMini" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayerMini" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayerMini" /f /reg:64 1> NUL 2> NUL
		reg delete "HKCR\Applications\PotPlayerMini.exe" /f 1> NUL 2> NUL
		reg delete "HKCR\Applications\PotPlayer.exe" /f 1> NUL 2> NUL
		reg delete "HKCU\Software\Daum" /v "GUID_POTPLAYER" /f 1> NUL 2> NUL
		
		rem For PotPlayer Live version. (cope partially)
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayer" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayer" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayerLite" /f /reg:32 1> NUL 2> NUL
		reg delete "HKLM\SOFTWARE\Clients\Media\PotPlayerLite" /f /reg:64 1> NUL 2> NUL
		reg delete "HKCU\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\PotPlayerMini.exe" /f /reg:64 1> NUL 2> NUL
		reg delete "HKCU\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION\PotPlayerMini.exe" /f /reg:64 1> NUL 2> NUL
		reg delete "HKLM\Software\Microsoft\Internet Explorer\Low Rights\ElevationPolicy\{13E0C436-2D13-4aae-8580-152920AB419F}" /f /reg:32 1> NUL 2> NUL
		reg delete "HKCU\Software\Microsoft\Internet Explorer\Low Rights\ElevationPolicy\{13E0C436-2D13-4aae-8580-152920AB419F}" /f 1> NUL 2> NUL
		reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "PotNotify" /f 1> NUL 2> NUL
	)
	exit /b 0
	
	
	
:UNREG_SET
	rem Delete settings in the registry
	call :CHECK_RUNNING
	if %B64% == 1 (
		reg query "HKCU\Software\Daum" /f "PotPlayerMini64" /k /e 1> NUL 2> NUL
		if not errorlevel 1 (
			reg delete "HKCU\Software\Daum\PotPlayerMini64" /f 1> NUL
			if errorlevel 1 (
				echo.
				echo  ERROR: Fail to delete settings of PotPlayer 64-bit in the registry.
				echo.
				exit /b 1
			)
		)
		reg delete "HKCU\Software\Daum\PotPlayer64" /f 1> NUL 2> NUL
		reg delete "HKCU\Software\Daum\PotPlayerMiniXP64" /f 1> NUL 2> NUL
		reg delete "HKCU\Software\Daum\PotScreenSaver64" /f 1> NUL 2> NUL
		reg query "HKCU\Software\Daum" /f "PotPlayer" /k 1> NUL 2> NUL 2> NUL
		if errorlevel 1 (
			reg query "HKCU\Software\Daum" /f "PotPlayerMini" /k 1> NUL 2> NUL
			if errorlevel 1 (
				reg query "HKCU\Software\Daum" /f "PotPlayerMiniXP" /k 1> NUL 2> NUL
					if errorlevel 1 reg delete "HKCU\Software\Daum" /f 1> NUL 2> NUL
				)
			)
		)
	)
	if %B32% == 1 (
		reg query "HKCU\Software\Daum" /f "PotPlayerMini" /k /e 1> NUL 2> NUL
		if not errorlevel 1 (
			reg delete "HKCU\Software\Daum\PotPlayerMini" /f 1> NUL
			if errorlevel 1 (
				echo.
				echo  ERROR: Fail to delete settings of PotPlayer 32-bit in the registry.
				echo.
				exit /b 1
			)
		)
		reg delete "HKCU\Software\Daum\PotPlayer" /f 1> NUL 2> NUL
		reg delete "HKCU\Software\Daum\PotPlayerMiniXP" /f 1> NUL 2> NUL
		reg delete "HKCU\Software\Daum\PotScreenSaver" /f 1> NUL 2> NUL
		reg query "HKCU\Software\Daum" /f "PotPlayer64" /k 1> NUL 2> NUL
		if errorlevel 1 (
			reg query "HKCU\Software\Daum" /f "PotPlayerMini64" /k 1> NUL 2> NUL
			if errorlevel 1 (
				reg query "HKCU\Software\Daum" /f "PotPlayerMiniXP64" /k 1> NUL 2> NUL
					if errorlevel 1 reg delete "HKCU\Software\Daum" /f 1> NUL 2> NUL
				)
			)
		)
	)
	exit /b 0
	
	
	
:INFO_FOLDER
	set Share=0
	if %B64% == 1 (
		if exist "%SetupFolder%\PotPlayer.dll" (
			set B32=1
			set Share=1
		)
	) else if %B32% == 1 (
		if exist "%SetupFolder%\PotPlayer64.dll" (
			set B64=1
			set Share=1
		)
	)
	set StdInst=0
	call :CHECK_STDINST "%SetupFolder%" 0
	
	echo.
	echo.
	echo  You can delete the following folder^(s^) manually at your own risk.
	echo  Maybe you stored some stuff here such as playlists or captured clips...
	echo.
	echo  "%SetupFolder%"
	if %StdInst% == 1 (
		if %B64% == 1 (
			if exist "%APPDATA%\PotPlayerMini64" echo  "%APPDATA%\PotPlayerMini64"
			if exist "%APPDATA%\PotPlayer64" echo  "%APPDATA%\PotPlayer64"
		)
		if %B32% == 1 (
			if exist "%APPDATA%\PotPlayerMini" echo  "%APPDATA%\PotPlayerMini"
			if exist "%APPDATA%\PotPlayer" echo  "%APPDATA%\PotPlayer"
		)
	)
	if %Share% == 1 (
		echo.
		echo  ^<^< WARNING ^>^>
		echo  This installation folder contains PotPlayer both 32-bit and 64-bit.
		echo  If you delete this folder, both versions will be removed.
		echo.
	)
	echo.
	if %StdInst% == 1 (
		echo  The AppData folder^(s^) above may be used also by another PotPlayer
		echo  if it exists in a different program folder.
	)
	echo.
	exit /b 0
	
	
	
