@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
)

:: ---------------------------------------------------------
:: 0. 変数設定
:: ---------------------------------------------------------
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 '========================================================';"

:: ---------------------------------------------------------
:: 0. 実行確認
:: ---------------------------------------------------------
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"
)

:: =========================================================
:: GIMP 2 プラグイン削除
:: =========================================================
if defined PLUGIN_PATH (
	if exist "!PLUGIN_PATH!" (
		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 'GIMP 2 プラグインフォルダを削除中...' -ForegroundColor DarkYellow;" ^
			"Write-Host '';"
		
		rd /s /q "!PLUGIN_PATH!" > nul

		if !errorlevel! equ 0 (
			powershell -NoProfile -Command ^
				"Write-Host '--------------------------------------------------------' -ForegroundColor DarkCyan;" ^
				"Write-Host ' [完了] GIMP 2 プラグインの削除が成功しました！' -ForegroundColor DarkCyan;" ^
				"Write-Host '--------------------------------------------------------' -ForegroundColor DarkCyan;" ^
				"Write-Host '';"
		) else (
			powershell -NoProfile -Command ^
				"Write-Host '[エラー] GIMP 2 プラグインの削除に失敗しました。' -ForegroundColor DarkRed;" ^
				"Write-Host '';"
		)
	)
)

powershell -NoProfile -ExecutionPolicy Bypass -Command ^
	"Write-Host '========================================================' -ForegroundColor DarkCyan;" ^
	"Write-Host '  アンインストール処理完了' -ForegroundColor DarkGreen;" ^
	"Write-Host '========================================================' -ForegroundColor DarkCyan;"
	
pause