@echo off
setlocal EnableExtensions
title WorkRequest - Explorer Protocol Setup
echo ======================================================================
echo    [WorkRequest] Explorer Open Protocol (workfolder:) Full Setup
echo ======================================================================
echo.

:: --- Step 1: Install WorkFolder.exe ---
set "TARGET_DIR=%LOCALAPPDATA%\WorkRequest"
if not exist "%TARGET_DIR%" mkdir "%TARGET_DIR%"

set "SCRIPT_DIR=%~dp0"
copy /y "%SCRIPT_DIR%WorkFolder.exe" "%TARGET_DIR%\WorkFolder.exe" >nul 2>nul
copy /y "%SCRIPT_DIR%setup_helper.ps1" "%TARGET_DIR%\setup_helper.ps1" >nul 2>nul

echo [1/4] WorkFolder.exe installed: %TARGET_DIR%

:: --- Step 2: Register workfolder: protocol handler ---
reg add "HKCU\Software\Classes\workfolder" /ve /t REG_SZ /d "URL:WorkFolder Protocol" /f >nul
reg add "HKCU\Software\Classes\workfolder" /v "URL Protocol" /t REG_SZ /d "" /f >nul
reg add "HKCU\Software\Classes\workfolder\shell\open\command" /ve /t REG_SZ /d "\"%TARGET_DIR%\WorkFolder.exe\" \"%%1\"" /f >nul

echo [2/4] Protocol handler (workfolder:) registered.

:: --- Step 3: ExternalProtocolDialogShowAlwaysOpenCheckbox ---
reg add "HKCU\Software\Policies\Google\Chrome" /v "ExternalProtocolDialogShowAlwaysOpenCheckbox" /t REG_DWORD /d 1 /f >nul 2>nul
reg add "HKCU\Software\Policies\Microsoft\Edge" /v "ExternalProtocolDialogShowAlwaysOpenCheckbox" /t REG_DWORD /d 1 /f >nul 2>nul

echo [3/4] ExternalProtocolDialog checkbox policy set.

:: --- Step 4: Browser policy + Chrome flags (PowerShell helper) ---
echo [4/4] Applying browser auto-launch policy + Chrome flags...
echo.

:: IMPORTANT: Close Chrome/Edge before this step for flags to take effect
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%SCRIPT_DIR%setup_helper.ps1" 2>nul

echo.
echo ======================================================================
echo  [DONE] Setup completed!
echo.
echo  IMPORTANT: Close ALL browser windows, then reopen the browser.
echo.
echo  If you still see a popup when clicking [Open Folder]:
echo    1. Open browser address bar
echo    2. Type:  chrome://flags/#unsafely-treat-insecure-origin-as-secure
echo       (Edge: edge://flags/#unsafely-treat-insecure-origin-as-secure)
echo    3. Enter: http://192.168.1.250
echo    4. Set to [Enabled], click [Relaunch]
echo    5. Check [Always allow] on the popup, click [Open]
echo ======================================================================
echo.
pause
