@echo off
setlocal EnableExtensions
title STIMPACK Launcher Connection Setup
color 0B

echo.
echo ==========================================
echo   STIMPACK GAME LAB - Launcher Setup
echo ==========================================
echo.

set "STIMPACK_EXE=%~dp0stimpack.exe"

if not exist "%STIMPACK_EXE%" (
    echo [1/4] stimpack.exe was not found next to this setup file.
    echo       Select the WOW folder's stimpack.exe.
    echo.

    for /f "usebackq delims=" %%I in (`powershell.exe -NoProfile -STA -Command "Add-Type -AssemblyName System.Windows.Forms; $d=New-Object System.Windows.Forms.OpenFileDialog; $d.Title='Select STIMPACK stimpack.exe'; $d.Filter='stimpack.exe|stimpack.exe'; $d.CheckFileExists=$true; if($d.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK){$d.FileName}"`) do (
        set "STIMPACK_EXE=%%I"
    )
) else (
    echo [1/4] stimpack.exe found next to setup file.
)

if not exist "%STIMPACK_EXE%" (
    echo.
    echo ERROR: stimpack.exe was not selected or does not exist.
    echo.
    pause
    exit /b 1
)

for %%F in ("%STIMPACK_EXE%") do (
    if /I not "%%~nxF"=="stimpack.exe" (
        echo.
        echo ERROR: The selected file is not stimpack.exe.
        echo.
        pause
        exit /b 1
    )
)

echo [2/4] Launcher:
echo       %STIMPACK_EXE%
echo.

echo [3/4] Registering stimpack:// protocol...

reg.exe add "HKCU\Software\Classes\stimpack" /ve /d "URL:STIMPACK Launcher" /f >nul
if errorlevel 1 goto :REGFAIL

reg.exe add "HKCU\Software\Classes\stimpack" /v "URL Protocol" /d "" /f >nul
if errorlevel 1 goto :REGFAIL

reg.exe add "HKCU\Software\Classes\stimpack\DefaultIcon" /ve /d "\"%STIMPACK_EXE%\",0" /f >nul
if errorlevel 1 goto :REGFAIL

reg.exe add "HKCU\Software\Classes\stimpack\shell\open\command" /ve /d "\"%STIMPACK_EXE%\"" /f >nul
if errorlevel 1 goto :REGFAIL

echo [4/4] Verifying registration...
reg.exe query "HKCU\Software\Classes\stimpack\shell\open\command" /ve > "%TEMP%\stimpack_protocol_check.txt" 2>nul
if errorlevel 1 goto :VERIFYFAIL

findstr /I /C:"%STIMPACK_EXE%" "%TEMP%\stimpack_protocol_check.txt" >nul
if errorlevel 1 goto :VERIFYFAIL

del /q "%TEMP%\stimpack_protocol_check.txt" >nul 2>&1

echo.
echo ==========================================
echo   Connected successfully
echo ==========================================
echo.
echo Registered:
echo   stimpack://launch
echo.
echo Launcher:
echo   %STIMPACK_EXE%
echo.
echo You can now close this window and click
echo "Game Start" on the STIMPACK website.
echo.
echo Saving the connected state in your browser...
start "" "https://stimpack.pe.kr/wow/?launcher=registered"
echo.
pause
exit /b 0

:REGFAIL
echo.
echo ==========================================
echo   Registration failed
echo ==========================================
echo.
echo Windows could not write the protocol registry keys.
echo ErrorLevel: %ERRORLEVEL%
echo.
echo Please keep this window open and send a screenshot.
echo.
pause
exit /b 1

:VERIFYFAIL
echo.
echo ==========================================
echo   Registration verification failed
echo ==========================================
echo.
echo Registry values were written but could not be verified.
echo.
echo Run this command and send the result:
echo reg query "HKCU\Software\Classes\stimpack\shell\open\command" /ve
echo.
pause
exit /b 1
