@echo off
REM Bootstrapper: lets you choose which archives to download and extract
setlocal
set BASE=%~dp0
set NODE_URL=https://cdn.reactivesli.me/programs/forza/node-runtime.zip
set MAP_URL=https://cdn.reactivesli.me/programs/forza/maptiles.zip
set CODE_URL=https://cdn.reactivesli.me/programs/forza/code.zip

set "DOWNLOAD_NODE=0"
set "DOWNLOAD_MAP=0"
set "DOWNLOAD_CODE=0"

echo Using base folder: %BASE%
echo.
echo Download options:
echo   [1] Download all
echo   [2] Download node runtime
echo   [3] Download tilemaps
echo   [4] Download app
choice /c 1234 /n /m "Enter choice: "
if errorlevel 4 (
	set "DOWNLOAD_CODE=1"
	goto StartDownloads
)
if errorlevel 3 (
	set "DOWNLOAD_MAP=1"
	goto StartDownloads
)
if errorlevel 2 (
	set "DOWNLOAD_NODE=1"
	goto StartDownloads
)
if errorlevel 1 (
	set "DOWNLOAD_NODE=1"
	set "DOWNLOAD_MAP=1"
	set "DOWNLOAD_CODE=1"
	goto StartDownloads
)

:StartDownloads
if "%DOWNLOAD_NODE%%DOWNLOAD_MAP%%DOWNLOAD_CODE%"=="000" (
	echo No downloads selected. Exiting.
	goto Cleanup
)

set TMPDIR=%TEMP%\fh6_download
if exist "%TMPDIR%" rd /s /q "%TMPDIR%"
mkdir "%TMPDIR%"

if "%DOWNLOAD_NODE%"=="1" (
	echo Downloading node runtime...
	powershell -NoProfile -Command "Invoke-WebRequest -Uri '%NODE_URL%' -OutFile '%TMPDIR%\node-runtime.zip'"
	echo Extracting node runtime...
	powershell -NoProfile -Command "Expand-Archive -Path '%TMPDIR%\node-runtime.zip' -DestinationPath '%TMPDIR%\node' -Force"

	echo Installing node runtime into runtime\ ...
	powershell -NoProfile -Command "New-Item -ItemType Directory -Force -Path '%BASE%runtime' | Out-Null; $src = Get-ChildItem -Path '%TMPDIR%\node' -Directory -ErrorAction SilentlyContinue | Where-Object { Test-Path (Join-Path $_.FullName 'node.exe') } | Select-Object -First 1; if ($src) { $sourcePath = $src.FullName } elseif (Test-Path '%TMPDIR%\node\node.exe') { $sourcePath = '%TMPDIR%\node' } else { throw 'Unable to find Node files after extraction.' }; if (Test-Path '%BASE%runtime') { Remove-Item -Recurse -Force '%BASE%runtime' }; Move-Item -Path $sourcePath -Destination '%BASE%runtime' -Force"
)

if "%DOWNLOAD_MAP%"=="1" (
	echo Downloading map tiles...
	powershell -NoProfile -Command "Invoke-WebRequest -Uri '%MAP_URL%' -OutFile '%TMPDIR%\maptiles.zip'"
	echo Extracting map tiles to %BASE%src\ ...
	powershell -NoProfile -Command "New-Item -ItemType Directory -Force -Path '%BASE%src' | Out-Null; Expand-Archive -Path '%TMPDIR%\maptiles.zip' -DestinationPath '%BASE%src' -Force"
)

if "%DOWNLOAD_CODE%"=="1" (
	echo Downloading app code...
	powershell -NoProfile -Command "Invoke-WebRequest -Uri '%CODE_URL%' -OutFile '%TMPDIR%\code.zip'"
	echo Extracting app code to %BASE%...
	powershell -NoProfile -Command "Expand-Archive -Path '%TMPDIR%\code.zip' -DestinationPath '%BASE%' -Force"
)

echo.
echo Selected downloads complete.

:Cleanup
echo Cleaning up...
if exist "%TMPDIR%" rd /s /q "%TMPDIR%"

echo Done. You can run the app with run-fh6-telemetry.cmd if present.
pause
endlocal
