@echo off
chcp 65001 >nul 2>&1
title JoyDock Chrome USB Debug Launcher

set "USER_DATA_DIR=C:\chrome-usb-test"
set "TARGET_URL=https://page.rayneo.com/joydock_web_ota/index_en.html"
set "CHROME_PATH="

REM === 1. Find Chrome via Windows Registry (most reliable) ===
for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" /ve 2^>nul') do set "CHROME_PATH=%%b"
if defined CHROME_PATH goto :found

for /f "tokens=2*" %%a in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" /ve 2^>nul') do set "CHROME_PATH=%%b"
if defined CHROME_PATH goto :found

REM === 2. Check common installation paths ===
if exist "%ProgramFiles%\Google\Chrome\Application\chrome.exe" (
    set "CHROME_PATH=%ProgramFiles%\Google\Chrome\Application\chrome.exe"
    goto :found
)
if exist "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" (
    set "CHROME_PATH=%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"
    goto :found
)
if exist "%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe" (
    set "CHROME_PATH=%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe"
    goto :found
)

REM === 3. Search PATH environment variable ===
for /f "delims=" %%i in ('where chrome.exe 2^>nul') do (
    set "CHROME_PATH=%%i"
    goto :found
)

REM === Chrome not found ===
echo [ERROR] Google Chrome not found!
echo.
echo Please make sure Chrome is installed.
echo If Chrome is installed but still not detected, edit this script:
echo   Right-click this file -^> Edit, and set CHROME_PATH to the full path of chrome.exe
echo.
pause
exit /b 1

:found
echo [OK] Chrome found: %CHROME_PATH%
echo Starting Chrome in USB debug mode...
echo   User data: %USER_DATA_DIR%
echo   URL: %TARGET_URL%
echo.

start "" "%CHROME_PATH%" --disable-features=WebUsbEnforceStandardRequestAllowlist --user-data-dir="%USER_DATA_DIR%" "%TARGET_URL%"
