@echo off
rem ================================================================
rem  SOUL partner - finish install (one double-click)
rem
rem  WHERE TO PUT THIS FILE:
rem    into the PACKAGE ROOT - the folder that contains the  analysis
rem    folder (the same folder that holds the other .cmd files).
rem
rem  THEN:
rem    right-click this file -> "Run as administrator"
rem
rem  WHAT IT DOES:
rem    [1/2] registers auto-start (one scheduled task per account)
rem    [2/2] starts the link (Edge -> bridge 8898 -> gateway 8897)
rem    then it prints the one thing that is left: scan a QR code once
rem    per account.
rem
rem  ASCII-only on purpose: cmd.exe mis-parses UTF-8 batch files.
rem  All Chinese text comes from node, which writes proper UTF-8.
rem ================================================================
chcp 65001 >nul
setlocal EnableExtensions
title SOUL partner - finish install
cd /d "%~dp0"

if not exist "%~dp0analysis\_install_tasks.js" goto :wrongdir

rem ---- elevation check ----
net session >nul 2>&1
if errorlevel 1 (
  echo.
  echo   Administrator rights are required.
  echo   A UAC prompt will pop up now - please click  Yes .
  echo.
  powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)

rem ---- locate node ----
set "NODE="
for /f "delims=" %%v in ('node -v 2^>nul') do set "NODE=node"
if not defined NODE if exist "%ProgramFiles%\nodejs\node.exe" set "NODE=%ProgramFiles%\nodejs\node.exe"
if not defined NODE if exist "%ProgramFiles(x86)%\nodejs\node.exe" set "NODE=%ProgramFiles(x86)%\nodejs\node.exe"
if not defined NODE if exist "%LOCALAPPDATA%\Programs\nodejs\node.exe" set "NODE=%LOCALAPPDATA%\Programs\nodejs\node.exe"
if not defined NODE if exist "%APPDATA%\nvm\node.exe" set "NODE=%APPDATA%\nvm\node.exe"
if not defined NODE goto :nonode

echo ==============================================================
echo   SOUL partner - finish install
echo ==============================================================
echo.
echo   Package : %~dp0
echo   Node    : %NODE%
echo.
echo   [1/2] Registering auto-start (one scheduled task per account) ...
echo.
"%NODE%" "%~dp0analysis\_install_tasks.js" --register
set "RCREG=%ERRORLEVEL%"
echo.
echo   ---- register exit code: %RCREG% ----
echo.
echo   [2/2] Starting the link ...
echo         (Edge first, then bridge 8898 / gateway 8897)
echo.
"%NODE%" "%~dp0analysis\_boot.js"
set "RCBOOT=%ERRORLEVEL%"
echo.
echo   ---- start exit code: %RCBOOT% ----
echo.
echo ==============================================================
echo   Done. What is left: ONE QR scan per account.
echo --------------------------------------------------------------
echo   1) Wait about 20 seconds, then open the console:
echo        http://127.0.0.1:8897/
echo   2) Pick the account - click "show QR" - scan it with the phone
echo      that account is logged into.
echo   3) Repeat for each account. The logins are remembered after
echo      that, so you normally scan only once per account.
echo --------------------------------------------------------------
echo   Logs and reports stay in this folder if anything looks wrong.
echo ==============================================================
echo.
pause
exit /b 0

:wrongdir
echo.
echo   *** analysis folder not found next to this file. ***
echo.
echo   Move this file into the PACKAGE ROOT - the folder that also
echo   contains the  analysis  folder and the other .cmd files.
echo.
pause
exit /b 1

:nonode
echo.
echo   *** Node.js was NOT found on this computer. ***
echo.
echo   Install the LTS .msi first (default options), then run this again:
echo     https://registry.npmmirror.com/-/binary/node/v22.23.3/node-v22.23.3-x64.msi
echo.
echo   Already installed but still not found? Reboot once and retry.
echo.
pause
exit /b 1
