@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

openfiles >nul 2>&1
if !errorlevel! neq 0 (
	powershell -NoProfile -Command ^
	"Write-Host '============================================================' -ForegroundColor DarkRed;" ^
	"Write-Host '[エラー] このバッチファイルは 管理者として実行 が必要です。' -ForegroundColor DarkRed;" ^
	"Write-Host '============================================================' -ForegroundColor DarkRed;"
	pause
	exit /b
)

:: ---------------------------------------------------------
:: 変数設定
:: ---------------------------------------------------------
set "SOFT_NAME=GIMP2"
set "PLUGIN_NAME=jpegli"
set "PLUGIN_PATH="
set "SEARCH_EXE_PATH=gimp-2*"
set "ADD_PATH=lib\gimp\2.0\plug-ins"
set "COPY_PLUGIN_FOLDER=export-jpegli"

powershell -NoProfile -Command ^
	"Write-Host '========================================================';" ^
	"Write-Host '%PLUGIN_NAME% for %SOFT_NAME% インストール';" ^
	"Write-Host '========================================================';"

powershell -NoProfile -Command ^
	"Write-Host '';" ^
	"Write-Host 'インストールを開始しますか？ (Y/N): ' -ForegroundColor DarkYellow -NoNewline;" ^
	"$a = Read-Host;" ^
	"if ($a -eq 'y' -or $a -eq 'Y') { exit 1 } else { exit 0 }"

if errorlevel 1 (
	goto :EXECUTE
) else (
	goto :CANCEL
)

:CANCEL

powershell -NoProfile -Command ^
	"Write-Host '';" ^
	"Write-Host '処理を中止しました。' -ForegroundColor DarkRed;"
pause
exit /b

:EXECUTE

powershell -NoProfile -Command ^
	"Write-Host '';" ^
	"Write-Host '%SOFT_NAME%プラグインフォルダを検索中...' -ForegroundColor DarkYellow;" ^
	"Write-Host '';"

:: PowerShellでインストールパスを取得
for /f "delims=" %%I in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "$exe='%SEARCH_EXE_PATH%'; $add='%ADD_PATH%'; $plugin='%COPY_PLUGIN_FOLDER%'; $t=@('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*','HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*','HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',\"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\$exe\"); $i=Get-ItemProperty $t -ErrorAction SilentlyContinue | Where-Object { $d = if ($_.Path) { $_.Path } else { $_.InstallLocation }; if ($d -and (Test-Path (Join-Path $d \"bin\$exe\"))) { $true } else { $false } } | Sort-Object -Property @{ Expression = { (Get-Item $_.PSPath).LastWriteTime } } -Descending | Select-Object -First 1; if ($i) { $r = if ($i.Path) { $i.Path } else { $i.InstallLocation }; Write-Output (Join-Path $r.TrimEnd('\') \"$add\$plugin\") }" 2^>^&1') do (
	set "PLUGIN_PATH=%%I"
)

:: 結果判定とコピー処理
if defined PLUGIN_PATH (
	if exist "!PLUGIN_PATH!\.." (
		mkdir "!PLUGIN_PATH!" 2>nul

		powershell -NoProfile -Command ^
			"Write-Host '========================================================' -ForegroundColor DarkGreen;" ^
			"Write-Host ' [成功] %SOFT_NAME%プラグインパスを取得しました:' -ForegroundColor DarkGreen;" ^
			"Write-Host ' !PLUGIN_PATH!' -ForegroundColor DarkYellow;" ^
			"Write-Host '========================================================' -ForegroundColor DarkGreen;" ^
			"Write-Host '';"

		powershell -NoProfile -Command ^
			"Write-Host '%SOFT_NAME%プラグインフォルダにコピー中...' -ForegroundColor DarkYellow;" ^
			"Write-Host '';"

		set "SRC_PATH=%~dp0%COPY_PLUGIN_FOLDER%"
		
		if exist "!SRC_PATH!" (
			powershell -NoProfile -Command ^
				"Write-Host '[%COPY_PLUGIN_FOLDER%] をプラグインフォルダにコピー中...' -ForegroundColor DarkYellow;" ^
				"Write-Host '';"
			
			if exist "!SRC_PATH!\*" (
				xcopy "!SRC_PATH!" "!PLUGIN_PATH!" /E /I /Y > nul
			) else (
				copy /Y "!SRC_PATH!" "!PLUGIN_PATH!\" > nul
			)

			if !errorlevel! equ 0 (
				powershell -NoProfile -Command ^
					"Write-Host '========================================================' -ForegroundColor DarkGreen;" ^
					"Write-Host ' [完了] プラグインのコピーが成功しました！' -ForegroundColor DarkGreen;" ^
					"Write-Host '========================================================' -ForegroundColor DarkGreen;" ^
					"Write-Host '';"
			) else (
				powershell -NoProfile -Command ^
					"Write-Host '[エラー] コピー処理に失敗しました。アクセス権限を確認してください。' -ForegroundColor DarkRed;" ^
					"Write-Host '';"
			)
		) else (
			powershell -NoProfile -Command ^
				"Write-Host '[エラー] コピー元が見つかりません: !SRC_PATH!' -ForegroundColor DarkRed;" ^
				"Write-Host '';"
		)

	) else (
		powershell -NoProfile -Command ^
			"Write-Host '[エラー] %SOFT_NAME%のインストールフォルダが見つかりませんでした。' -ForegroundColor DarkRed;" ^
			"Write-Host '';"
	)
) else (
	powershell -NoProfile -Command ^
		"Write-Host '[エラー] 有効な%SOFT_NAME%が検出できませんでした。' -ForegroundColor DarkRed;" ^
		"Write-Host '';"
)

powershell -NoProfile -Command ^
	"Write-Host '処理が完了しました。キーを押すと閉じます。' -ForegroundColor DarkYellow;"

pause
exit /b