@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  ＜＜ PotPlayerを抽出します ＞＞
	echo.
	echo  セットアップファイル：
	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  インストールフォルダ（potRipperがある場所の親フォルダ）：
	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  既存のファイルは上書きされます。
	
	echo  使用中のPotPlayerがあれば先に終了させてください。
	echo.
	echo  何かキーを押すと処理を開始します。
	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  処理中です...
	echo.
	echo.
	
	call :EXPAND
	if errorlevel 1 exit /b 8
	
	call :EDIT
	
	echo.
	echo.
	echo  ファイル抽出を完了しました。
	
	set Reg64=0
	set Reg32=0
	call :CHECK_REG
	call :REG_APP
	if not errorlevel 1 (
		echo.
		echo.
		echo  アプリの登録を完了しました。
	)
	
	echo.
	echo.
	echo  インストールまたは更新を確認してください。
	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  エラー：ヘルプファイルがありません。
		echo.
		echo  "%BatName%.txt" を "%~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  エラー：指定のファイルが見つかりません。
		echo.
		echo  処理を中止します。
		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  管理者権限で開き直しています...
	echo.
	echo  ユーザーアカウント制御で変更の許可を与えてください。
	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  エラー：管理者権限で実行できません。
		echo.
		echo.
		echo  処理を中止します。
		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  エラー：引数の取得に失敗しました。
		echo.
		echo  最初からやり直してください。
		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  エラー："7z.exe" が見つかりません。
		echo.
		echo  既定のプログラムフォルダ以外の場所に "7-Zip" をインストールしている場合、
		
		echo  7-Zipのパスを環境変数の "Path" に追加してください。
		echo.
		echo  または "7z.exe" を "%~nx0" と同じフォルダに置いてください。
		echo.
		echo.
		echo  処理を中止します。
		pause > NUL
		exit /b 1
	)
	if not exist "%IncList%" (
		echo.
		echo.
		echo  エラー：抽出リストファイル "potRipper_inc.txt" が見つかりません。
		
		echo  "%~nx0" と同じフォルダに置いてください。
		echo.
		echo  "%IncList%"
		echo.
		echo  処理を中止します。
		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  このWindowsでは64bit版は使用できません。
					echo.
				) else (
					call :CHECK_STDINST 1
					if errorlevel 1 (
						echo.
						echo  このフォルダには64bit版を作成できません。
						
						echo  （potRipperがある場所の親フォルダ）
						echo.
					) else (
						set Avail=1
						set B64=1
					)
				)
			)
			if /i "%%d" == "PotPlayer.dll" (
				call :CHECK_STDINST 0
				if errorlevel 1 (
					echo.
					echo  このフォルダには32bit版を作成できません。
					
					echo  （potRipperがある場所の親フォルダ）
					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  このファイルは使用できません。
	echo.
	echo.
	echo  PotPlayerのセットアップファイルを確認してください。
	
	echo  またはインストールフォルダが不正です。
	echo.
	echo.
	echo  処理を中止します。
	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  エラー：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  エラー：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  現在のDLL Date:    !DllDateOld64! ^(SystemTime^)
			if not "!DllVerOld64!" == "" echo  現在のDLL Version: !DllVerOld64!  ^(64-bit^)
			if not "!DllSizeOld64!" == "" echo  現在の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  現在のDLL Date:    !DllDateOld32! ^(SystemTime^)
			if not "!DllVerOld32!" == "" echo  現在のDLL Version: !DllVerOld32!  ^(32-bit^)
			if not "!DllSizeOld32!" == "" echo  現在の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  このフォルダには既に32bit版PotPlayerが存在します。
		
		echo  64bit版も同じフォルダにインストールしますか？
	) else (
		echo  このフォルダには既に64bit版PotPlayerが存在します。
		
		echo  32bit版も同じフォルダにインストールしますか？
	)
	echo  （ 推奨しません ）
	echo.
	echo  このまま続行するには 'S' を、中止するにはその他の文字を入力してください。
	set Ret=
	set /p Ret=">"
	if /i not "%Ret%" == "S" (
		echo.
		echo.
		echo  処理を中止します。
		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  64bit版PotPlayerの設定をどちらに保存しますか？
	) else (
		echo  32bit版PotPlayerの設定をどちらに保存しますか？
	)
	echo.
	echo  R：Windowsのレジストリ（ PC内で共有 ）
	
	echo  I：新しいINIファイル（ ポータブル用 ）
	echo.
	echo  ここでレジストリを選んでも、PotPlayerの環境設定で
	
	echo  後から設定をINIファイルへ移行できます。
	echo.
	echo  INIファイルを選んで使い続ける場合、
	
	echo  レジストリ内に設定があっても無視され（関連付けを除く）、
	
	echo  potRipperはPotPlayerをレジストリへ登録しません。
	echo.
	:jump_01
	echo  'R' または 'I' のどちらか一文字を入力してください。
	set Ret=
	set /p Ret=">"
	echo.
	echo.
	if /i "%Ret%" == "I" (
		copy NUL "%~f1" /a 1> NUL
		if errorlevel 1 (
			echo  エラー：新しいINIファイルの作成に失敗しました。
			echo  "%~pd1"
			echo.
			echo  処理を中止します。
			pause > NUL
			exit /b 2
		)
		echo  新しいINIファイルを作成しました。
		echo  "%~f1"
		echo.
		echo  何かキーを押して続行してください。
		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  64bit版PotPlayerが使用中です。
			
			echo  先にPotPlayerを終了させてから、何かキーを押してください。
			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  32bit版PotPlayerが使用中です。
			
			echo  先にPotPlayerを終了させてから、何かキーを押してください。
			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  エラー：7-Zipでファイルの作成に失敗しました。
		
		echo  ファイルが使用中でないか確認してください。
		echo.
		echo  他のアクセス可能なファイルは更新されています。
	)
	exit /b 0
	
	
	
:_EXPAND
	7z x "%SetupFile%" -o"%SetupFolder%" "%~1" -r -y %Scc% 1> NUL
	if errorlevel 1 (
		echo.
		echo  エラー：7-Zipで "%~1" を作成できませんでした。
		
		echo  ファイルが使用中でないか確認してください。
		call :CHECK_UAC_HIGH
		if errorlevel 1 (
			echo.
			echo.
			echo  ======================================================================
			echo     もし使用中ではないのにファイルへのアクセスが拒否されるなら
			
			echo     "Windows PowerShell" をインストールしてください。
			echo  ======================================================================
			echo.
		)
		echo.
		echo  処理を中止します。
		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  エラー："PotPlayerMiniXP.exe" のファイル名を "PotPlayerMini.exe" へ変更できません。
			
			echo  PotPlayerMiniの実行には "PotPlayerMiniXP.exe" を使用してください。
		)
	)
	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  エラー："PotPlayerXP.exe" のファイル名を "PotPlayer.exe" へ変更できません。
				
				echo  PotPlayerの実行には "PotPlayerXP.exe" を使用してください。
			)
		)
	)
	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  エラー："PotPlayerMiniXP64.exe" のファイル名を "PotPlayerMini64.exe" へ変更できません。
			
			echo  64bit版PotPlayerMiniの実行には "PotPlayerMiniXP64.exe" を使用してください。
		)
	)
	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  エラー："PotPlayerXP64.exe" のファイル名を "PotPlayer64.exe" へ変更できません。
				
				echo  64bit版PotPlayerの実行には "PotPlayerXP64.exe" を使用してください。
			)
		)
	)
	exit /b 0
	
	
	
:CHECK_REG
	if %Xp% == 1 exit /b 1
	
	call :CHECK_UAC_HIGH
	if errorlevel 1 (
		echo.
		echo.
		echo  PotPlayerをレジストリに登録できません。
		set /a FirstOrRepair = %FirstOrRepair64% | %FirstOrRepair32%
		if !FirstOrRepair! == 1 (
			echo.
			echo  エクスプローラからpotRipperを操作する場合、レジストリを編集するには
			
			echo  "Windows PowerShell" をインストールしてください。
		)
		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  64bit版PotPlayerは別フォルダでレジストリに登録されています。
			echo  "%RegFolder64%"
			echo.
			echo  登録パスを今回のものに変更しますか？
			echo  "%SetupFolder%"
		) else (
			echo  64bit版PotPlayerをレジストリに登録しますか？
		)
	) else (
		if %2 == 1 (
			echo  32bit版PotPlayerは別フォルダでレジストリに登録されています。
			echo  "%RegFolder32%"
			echo.
			echo  登録パスを今回のものに変更しますか？
			echo  "%SetupFolder%"
		) else (
			echo  32bit版PotPlayerをレジストリに登録しますか？
		)
	)
	echo.
	echo  レジストリを変更するには 'R' を、何もしないなら他の文字を入力してください。
	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  レジストリ処理をスキップします。
				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  エラー：レジストリを編集できません。
			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  レジストリ処理をスキップします。
				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  エラー：レジストリを編集できません。
			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  エラー：ショートカットファイルを作成できません。
				
				echo  パスに未対応の文字が含まれている可能性があります。
			)
			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  エラー：ショートカットファイルを作成できません。
				
				echo  パスに未対応の文字が含まれている可能性があります。
			)
			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  ＜＜ PotPlayerの登録解除 ＞＞
		echo.
		echo.
		echo  レジストリを編集できません。
		
		echo  "/Unreg64" または "/Unreg32" のコマンドラインスイッチを付けて、
		
		echo  potRipperを管理者権限で実行してください。
		echo.
		echo  処理を中止します。
		pause > NUL
		pause > NUL
		exit /b 1
	)
	
	if %B64% == 1 (
		echo  ＜＜ PotPlayerの登録解除 64bit版 ＞＞
		echo.
		echo  64bit版PotPlayerの登録をレジストリから削除します。
	) else if %B32% == 1 (
		echo  ＜＜ PotPlayerの登録解除 32bit版 ＞＞
		echo.
		echo  32bit版PotPlayerの登録をレジストリから削除します。
	) 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の設定も削除されますが、
	
	echo  この操作ではファイルやフォルダは削除されません。
	
	echo  （ショートカットの削除を除く）
	echo.
	echo  使用中のPotPlayerがあれば先に終了させてください。
	echo.
	echo  何かキーを押すと処理を開始します。
	pause > NUL
	
	call :UNREG_ASC 0
	call :UNREG_APP
	call :UNREG_SET
	echo.
	echo.
	echo  登録解除が完了しました。
	echo.
	echo.
	call :INFO_FOLDER
	echo.
	echo.
	echo  すべて完了しました。
	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  このインストールフォルダは現在レジストリに登録されていません。
			
			echo  処理を続行すると他のPotPlayerに影響を与える可能性があります。
			echo.
			if %1 == 1 (
				echo  64bit版PotPlayerを登録解除してよろしいですか？
			) else (
				echo  32bit版PotPlayerを登録解除してよろしいですか？
			)
			echo.
			echo  続行するには 'U' を、中止するにはその他の文字を入力してください。
			set Ret=
			set /p Ret=">"
			if /i not "%Ret%" == "U" (
				echo.
				echo.
				echo  登録解除を中止します。
				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  エラー：PotPlayerMini64.exeで関連付けを解除できません。
			echo.
		) else (
			if %1 == 1 (
				echo.
				echo.
				echo  64bit版PotPlayerのファイル関連付けは現在解除されています。
				
				echo  PotPlayerの環境設定で関連付けを設定してください。
				echo.
			)
		)
	)
	if %B32% == 1 (
		"%SetupFolder%\PotPlayerMini.exe" /Unregister
		if errorlevel 1 (
			echo.
			echo.
			echo  エラー：PotPlayerMini.exeで関連付けを解除できません。
			echo.
			exit /b 1
		) else (
			if %1 == 1 (
				echo.
				echo.
				echo  32bit版PotPlayerのファイル関連付けは現在解除されています。
				
				echo  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  エラー：レジストリの編集ができません。
				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  エラー：レジストリの編集ができません。
				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  エラー：レジストリ上の設定を削除できません。
				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  エラー：レジストリ上の設定を削除できません。
				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  次の関連フォルダが不要なら、内容を確認してから手動で削除してください。
	
	echo  多くの場合、プレイリストや保存した画像ファイルなども含まれています。
	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  ＜＜ 注意 ＞＞
		
		echo  このインストールフォルダにはPotPlayerの32bit版と64bit版が両方入っています。
		
		echo  フォルダを削除すると両方のバージョンとも使用できなくなります。
		echo.
	)
	echo.
	if %StdInst% == 1 (
		echo  他のプログラムフォルダにもPotPlayerが存在する場合、
		
		echo  上のAppDataフォルダは共有されている可能性があります。
	)
	echo.
	exit /b 0
	
	
	
