@echo off
setlocal EnableExtensions DisableDelayedExpansion
title LeadsIce Local Runner Direct Setup

set "VERSION=0.3.30"
set "OPERATION=install"
set "EXPECTED_SHA256=553350b1d777dd7adf7381f5bf58d4b1b6a5af0a1647acb0b617403284344f82"
set "DOWNLOAD_BASE=https://leadsice-workspace.pages.dev/downloads"
set "DOWNLOAD_FALLBACK=https://app.leadsice.com/downloads"
set "WORK_DIR=%TEMP%\LeadsIce-Runner-Portable-%VERSION%-%RANDOM%-%RANDOM%"
set "PACKAGE=%WORK_DIR%\LeadsIce-Local-Runner-Portable-v%VERSION%.zip"
set "INSTALL_ROOT=%LOCALAPPDATA%\Programs\LeadsIce Local Runner"
set "STATE_ROOT=%LOCALAPPDATA%\LeadsIce\Local Runner"
set "APP_DIR=%INSTALL_ROOT%\LeadsIce-Local-Runner-Portable-v%VERSION%"
set "START_MENU=%APPDATA%\Microsoft\Windows\Start Menu\Programs\LeadsIce Local Runner Temporary.cmd"
set "STARTUP=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\LeadsIce Local Runner Temporary.cmd"
if "%~1"=="--apply" goto :apply_verified

echo.
echo LeadsIce Local Runner direct installer v%VERSION%
echo Downloading securely from the LeadsIce website. Keep this window open.
echo.

if exist "%WORK_DIR%" goto :failed
mkdir "%WORK_DIR%" || goto :failed

curl.exe --fail --location --retry 3 --retry-all-errors --output "%PACKAGE%.part1" "%DOWNLOAD_BASE%/LeadsIce-Local-Runner-Portable-v%VERSION%.zip.part1" || curl.exe --fail --location --retry 3 --retry-all-errors --output "%PACKAGE%.part1" "%DOWNLOAD_FALLBACK%/LeadsIce-Local-Runner-Portable-v%VERSION%.zip.part1" || goto :failed
curl.exe --fail --location --retry 3 --retry-all-errors --output "%PACKAGE%.part2" "%DOWNLOAD_BASE%/LeadsIce-Local-Runner-Portable-v%VERSION%.zip.part2" || curl.exe --fail --location --retry 3 --retry-all-errors --output "%PACKAGE%.part2" "%DOWNLOAD_FALLBACK%/LeadsIce-Local-Runner-Portable-v%VERSION%.zip.part2" || goto :failed
curl.exe --fail --location --retry 3 --retry-all-errors --output "%PACKAGE%.part3" "%DOWNLOAD_BASE%/LeadsIce-Local-Runner-Portable-v%VERSION%.zip.part3" || curl.exe --fail --location --retry 3 --retry-all-errors --output "%PACKAGE%.part3" "%DOWNLOAD_FALLBACK%/LeadsIce-Local-Runner-Portable-v%VERSION%.zip.part3" || goto :failed

copy /b "%PACKAGE%.part1"+"%PACKAGE%.part2"+"%PACKAGE%.part3" "%PACKAGE%" >nul || goto :failed

for /f "skip=1 tokens=* delims=" %%H in ('certutil.exe -hashfile "%PACKAGE%" SHA256') do if not defined ACTUAL_SHA256 set "ACTUAL_SHA256=%%H"
set "ACTUAL_SHA256=%ACTUAL_SHA256: =%"
if /i not "%ACTUAL_SHA256%"=="%EXPECTED_SHA256%" (
  echo Download verification failed. Setup has stopped to protect this computer.
  goto :failed
)

tar.exe -xf "%PACKAGE%" -C "%WORK_DIR%" || goto :failed
set "STAGED_APP=%WORK_DIR%\LeadsIce-Local-Runner-Portable-v%VERSION%"
if "%OPERATION%"=="uninstall" (
  "%STAGED_APP%\runtime\node-v24.21.0-win-x64\node.exe" "%STAGED_APP%\install-guard.js" --uninstall || goto :failed
  echo Uninstall completed. Local authorization and task data are preserved.
  timeout /t 5 >nul
  exit /b 0
)
"%STAGED_APP%\runtime\node-v24.21.0-win-x64\node.exe" "%STAGED_APP%\install-guard.js" "%~f0" || goto :failed
echo Setup completed and the running version was verified.
timeout /t 5 >nul
exit /b 0

:apply_verified
if not defined LEADSICE_VERIFIED_STAGE exit /b 1
if not exist "%LEADSICE_VERIFIED_STAGE%\install-guard.js" exit /b 1
set "WORK_DIR=%LEADSICE_VERIFIED_STAGE%"
cd /d "%WORK_DIR%" || goto :failed
del /q "%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\LeadsIce Local Runner.lnk" >nul 2>&1
del /q "%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\LeadsIce Local Runner Temporary.cmd" >nul 2>&1
if exist "%APPDATA%\Microsoft\Windows\Start Menu\Programs\LeadsIce Local Runner" rmdir /s /q "%APPDATA%\Microsoft\Windows\Start Menu\Programs\LeadsIce Local Runner"
call :remove_install_root
if exist "%INSTALL_ROOT%" goto :files_in_use
mkdir "%INSTALL_ROOT%" || goto :failed
xcopy "%LEADSICE_VERIFIED_STAGE%\*" "%APP_DIR%\" /E /I /H /Y >nul || goto :failed

if not exist "%APP_DIR%\Start-LeadsIce-Runner.cmd" goto :failed
if not exist "%APP_DIR%\Uninstall-LeadsIce-Runner.cmd" goto :failed
>"%START_MENU%" echo @echo off
>>"%START_MENU%" echo call "%APP_DIR%\Start-LeadsIce-Runner.cmd"
>"%STARTUP%" echo @echo off
>>"%STARTUP%" echo call "%APP_DIR%\Start-LeadsIce-Runner.cmd"
"%APP_DIR%\runtime\node-v24.21.0-win-x64\node.exe" "%APP_DIR%\portable-runner.js" register-protocol || goto :failed
"%APP_DIR%\runtime\node-v24.21.0-win-x64\node.exe" "%APP_DIR%\portable-runner.js" register-uninstall || goto :failed
"%APP_DIR%\runtime\node-v24.21.0-win-x64\node.exe" "%APP_DIR%\portable-runner.js" start || goto :failed

echo.
echo Files installed. Checking the actual running version...
exit /b 0

:remove_install_root
for /l %%R in (1,1,8) do (
  if not exist "%INSTALL_ROOT%" exit /b 0
  rmdir /s /q "%INSTALL_ROOT%" >nul 2>&1
  if exist "%INSTALL_ROOT%" "%LEADSICE_VERIFIED_STAGE%\runtime\node-v24.21.0-win-x64\node.exe" -e "setTimeout(() => {}, 1000)"
)
exit /b 1

:files_in_use
echo.
echo The previous Runner is still using files in the installation folder.
echo Restart Windows and run this installer again. Do not launch the old Runner first.
pause
exit /b 1

:failed
echo.
echo Temporary Runner setup failed. Do not disable Defender or add an exclusion.
if exist "%STATE_ROOT%\install-status.json" echo Installation report: "%STATE_ROOT%\install-status.json"
echo Take a screenshot of this window and contact AlanLau@LeadsIce.com.
pause
exit /b 1

