@echo off
setlocal
title filebrowser WebDAV setup (v4)

set "SRV=189.24.77.117"
set "PORT=5003"
set "U=admin"
set "P=Store#2026alex"
set "WURL=https://%SRV%:%PORT%/webdav"
set "CERTURL=http://%SRV%:5007/filestore-ca.crt"

if /i "%~1"=="admin" goto :adminPhase
if /i "%~1"=="map"   goto :mapOnly

echo ============================================================
echo   filebrowser WebDAV - setup  (v4)
echo   URL  : %WURL%/
echo   User : %U%
echo   Pass : %P%
echo ============================================================
echo.
echo [1/3] admin step - a UAC prompt will appear, please allow it
powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -ArgumentList 'admin' -Verb RunAs -Wait"
echo       [ok] admin step finished

echo [2/3] writing helper map-drive.bat
> "%~dp0map-drive.bat" echo @echo off
>> "%~dp0map-drive.bat" echo "%~f0" map

:mapOnly
echo [3/3] checking the WebDAV redirector (WebClient service)
sc query webclient | find /i "RUNNING" >nul
if errorlevel 1 (
    echo       [warn] WebClient is NOT running - asking for admin to start it
    powershell -NoProfile -Command "Start-Process -FilePath 'cmd.exe' -ArgumentList '/c sc config webclient start= auto ^& sc start webclient' -Verb RunAs -Wait"
    timeout /t 5 >nul
)
echo       mapping drive Z: in THIS ^(non-elevated^) session
net use Z: /delete /y >nul 2>&1
net use Z: "%WURL%/" /user:%U% "%P%" /persistent:yes
if errorlevel 1 (
    echo       [warn] first try failed, retrying in 6s ...
    timeout /t 6 >nul
    net use Z: "%WURL%/" /user:%U% "%P%" /persistent:yes
)
echo.
echo ---------------- dir Z:\ ----------------
dir Z:\
echo ----------------------------------------
echo.
echo   Files listed above = done.  Open Explorer and type  Z:\
echo   Still error 67  -> run these in an ADMIN cmd and tell me the result:
echo        sc query webclient
echo        net start webclient
echo.
pause
exit /b

:adminPhase
net session >nul 2>&1
if errorlevel 1 exit /b
echo       [admin] trusting the server certificate
curl.exe -sL --fail -o "%TEMP%\filestore-ca.crt" "%CERTURL%" 2>nul
if errorlevel 1 powershell -NoProfile -Command "try{ Invoke-WebRequest -UseBasicParsing '%CERTURL%' -OutFile (Join-Path $env:TEMP 'filestore-ca.crt') } catch {}"
certutil -addstore -f Root "%TEMP%\filestore-ca.crt" >nul 2>&1
echo       [admin] writing WebClient policy
reg add "HKLM\SYSTEM\CurrentControlSet\Services\WebClient\Parameters" /v BasicAuthLevel /t REG_DWORD /d 2 /f >nul
reg add "HKLM\SYSTEM\CurrentControlSet\Services\WebClient\Parameters" /v FileSizeLimitInBytes /t REG_DWORD /d 4294967295 /f >nul
reg add "HKLM\SYSTEM\CurrentControlSet\Services\WebClient\Parameters" /v FileAttributesLimitInBytes /t REG_DWORD /d 10485760 /f >nul
reg add "HKLM\SYSTEM\CurrentControlSet\Services\WebClient\Parameters" /v AuthForwardServerList /t REG_MULTI_SZ /d "https://%SRV%:%PORT%/*" /f >nul
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v EnableLinkedConnections /t REG_DWORD /d 1 /f >nul
sc config webclient start= auto >nul 2>&1

echo       [admin] stopping WebClient (if running) and waiting for STOPPED
sc query webclient | find /i "RUNNING" >nul
if not errorlevel 1 (
    net stop webclient >nul 2>&1
    for /l %%i in (1,1,20) do (
        sc query webclient | find /i "STOPPED" >nul
        if not errorlevel 1 goto :wcStopped
        timeout /t 1 >nul
    )
)
:wcStopped
echo       [admin] starting WebClient and waiting for RUNNING
sc start webclient >nul 2>&1
for /l %%i in (1,1,20) do (
    timeout /t 1 >nul
    sc query webclient | find /i "RUNNING" >nul
    if not errorlevel 1 goto :wcReady
)
echo       [admin] WARNING: WebClient did not reach RUNNING
sc query webclient
exit /b
:wcReady
echo       [admin] WebClient is RUNNING
echo       [admin] connectivity test
net use T: /delete /y >nul 2>&1
net use T: "%WURL%/" /user:%U% "%P%" >nul 2>&1
if errorlevel 1 (
    timeout /t 5 >nul
    net use T: "%WURL%/" /user:%U% "%P%" >nul 2>&1
)
if errorlevel 1 (echo       [admin] test FAILED) else (echo       [admin] test OK)
net use T: /delete /y >nul 2>&1
exit /b
