@echo off
setlocal EnableExtensions
title RC Link - Install from local parts
set "EXPECTED=E6F33B1359B563FC764FCC4D6941D9A6C0DE7B4B277E832D2C4C589F579054D1"
set "WORK=%TEMP%\RcLinkSetup"
set "SRC=%~dp0"

net session >nul 2>&1
if errorlevel 1 (
  echo Requesting administrator rights...
  powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)

echo.
echo  RC Link - Install from local parts
echo  ============================================
echo  Folder: %SRC%
echo.
if not exist "%SRC%RcAgent-Setup.zip.001" goto :missing
if not exist "%SRC%RcAgent-Setup.zip.002" goto :missing
if not exist "%SRC%RcAgent-Setup.zip.003" goto :missing

if exist "%WORK%" rd /s /q "%WORK%"
mkdir "%WORK%" >nul 2>&1
if not exist "%WORK%" goto :fail

echo Joining parts...
copy /b "%SRC%RcAgent-Setup.zip.001"+"%SRC%RcAgent-Setup.zip.002"+"%SRC%RcAgent-Setup.zip.003" "%WORK%\RcAgent-Setup.zip" >nul
if errorlevel 1 goto :fail

echo Verifying SHA-256...
set "ACTUAL="
for /f "usebackq delims=" %%H in (`powershell -NoProfile -Command "(Get-FileHash -LiteralPath '%WORK%\RcAgent-Setup.zip' -Algorithm SHA256).Hash"`) do set "ACTUAL=%%H"
if not defined ACTUAL goto :fail
echo   %ACTUAL%
if /i not "%ACTUAL%"=="%EXPECTED%" goto :hashfail

echo Extracting...
powershell -NoProfile -Command "Expand-Archive -LiteralPath '%WORK%\RcAgent-Setup.zip' -DestinationPath '%WORK%\pkg' -Force"
if errorlevel 1 goto :fail

echo.
echo Starting installer...
echo.
call "%WORK%\pkg\install.cmd"
set "RC=%errorlevel%"
if "%RC%"=="0" rd /s /q "%WORK%" 2>nul
exit /b %RC%

:missing
echo.
echo ERROR: Put all three parts in the same folder as this script:
echo   RcAgent-Setup.zip.001
echo   RcAgent-Setup.zip.002
echo   RcAgent-Setup.zip.003
pause
exit /b 1

:hashfail
echo.
echo ERROR: SHA-256 does not match. Download the parts again.
pause
exit /b 1

:fail
echo.
echo ERROR: Setup failed. Check the messages above.
if exist "%WORK%" echo Temporary files kept in: %WORK%
pause
exit /b 1
