@echo off
REM ===========================================================================
REM  Vespryx desktop tools, one-click setup for Windows.
REM
REM  This file is plain text on purpose. Read it before you run it. It:
REM    1. checks for Node, and installs it with winget if it is missing
REM    2. downloads https://vespryx.com/download/vespryx-tools.zip
REM    3. unpacks it to your user folder, in a folder called Vespryx
REM    4. sets up the TradingView bridge and installs your token
REM    5. runs pre-flight, which names anything still wrong
REM    6. sets Vespryx to start with Windows, and starts it now
REM
REM  It installs nothing else. The one thing it leaves behind is a login entry
REM  (a shortcut in your Startup folder) so Vespryx starts with Windows, and
REM  "node tools\autostart.mjs --uninstall" from the Vespryx folder removes it.
REM  The only thing it sends anywhere is your Vespryx token, to the Vespryx
REM  API, which is what the tools use it for.
REM
REM  YOU DO NOT NEED ANY OF THIS to see Vespryx levels in the TradingView
REM  desktop app. Drawings live on your TradingView account, so a symbol
REM  painted in Chrome is already painted in the desktop app and on your phone.
REM  This kit is for driving the desktop app directly, and for letting a coding
REM  agent read and draw on your chart.
REM ===========================================================================
setlocal
title Vespryx desktop tools setup

set "DEST=%USERPROFILE%\Vespryx"
set "ZIP=%TEMP%\vespryx-tools.zip"
set "KITURL=https://vespryx.com/download/vespryx-tools.zip"

echo.
echo   Vespryx desktop tools
echo   ---------------------
echo   Installing to: %DEST%
echo.

REM --- 1. Node ---------------------------------------------------------------
node --version >nul 2>&1
if errorlevel 1 (
  echo   [1/6] Node is not installed. Installing it now, this takes a minute...
  winget install -e --id OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements
  if errorlevel 1 (
    echo.
    echo   Could not install Node automatically.
    echo   Install it yourself from https://nodejs.org then run this file again.
    echo.
    pause
    exit /b 1
  )
  REM winget puts Node on the system PATH, not on THIS window's copy of it,
  REM which is why a fresh install looks like a failed one. Re-read it.
  for /f "usebackq delims=" %%p in (`powershell -NoProfile -Command "[Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [Environment]::GetEnvironmentVariable('Path','User')"`) do set "PATH=%%p"
) else (
  echo   [1/6] Node is already installed.
)

node --version >nul 2>&1
if errorlevel 1 (
  echo.
  echo   Node is installed, but this window still cannot see it.
  echo   Close this window, then double-click this file again. That is all.
  echo.
  pause
  exit /b 1
)

REM --- 2. download -----------------------------------------------------------
REM  -f is what makes a 404 an ERROR. Without it curl saves the "page could not
REM  be found" body as vespryx-tools.zip and exits 0, so a moved download would
REM  fail one step later, unpacking a 79-byte HTML file, with a message about
REM  the archive rather than about the download. Measured against the live host.
echo   [2/6] Downloading the toolkit...
curl -fL -s -S -o "%ZIP%" "%KITURL%"
if errorlevel 1 (
  echo.
  echo   The download failed. Check your internet connection and try again.
  echo.
  pause
  exit /b 1
)

REM --- 3. unpack -------------------------------------------------------------
REM  Expand-Archive rather than tar: Windows ships a tar that reads zip, but if
REM  Git is installed its GNU tar comes first on PATH and cannot, so this would
REM  fail only on developer machines and work everywhere it was tested.
echo   [3/6] Unpacking to %DEST%
powershell -NoProfile -Command "Expand-Archive -LiteralPath '%ZIP%' -DestinationPath '%DEST%' -Force"
if errorlevel 1 (
  echo.
  echo   Could not unpack the download. Delete %DEST% and try again.
  echo.
  pause
  exit /b 1
)

cd /d "%DEST%"

REM --- 4. bridge + token -----------------------------------------------------
echo.
echo   [4/6] Setting up the TradingView bridge. TradingView will close and
echo         reopen, which is meant to happen.
echo.
node tools\setup-bridge.mjs

echo.
echo   ---------------------------------------------------------------
echo   Now your token, so the tools can read your Vespryx data.
echo.
echo     1. Open  https://vespryx.com/portal/
echo     2. Scroll down to "Desktop tools"
echo     3. Press "Show my token", then press "Copy"
echo.
echo   Then come back to this window and press any key.
echo   ---------------------------------------------------------------
echo.
pause
powershell -NoProfile -Command "Get-Clipboard" | node tools\td-token.mjs

REM --- 5. verify -------------------------------------------------------------
echo.
echo   [5/6] Checking everything...
echo.
node tools\preflight.mjs

REM --- 6. start with Windows -------------------------------------------------
REM  Nobody is going to run node tools\watch.mjs by hand, and a walkthrough
REM  proved it: the install finished, TradingView was opened from its icon,
REM  and nothing happened. So it runs in the background from here on and
REM  starts at login, and every launch of TradingView gets the debug port.
echo.
echo   [6/6] Setting Vespryx to start with Windows, and starting it now...
echo.
node tools\autostart.mjs --install

echo.
echo   ---------------------------------------------------------------
echo   Finished. Vespryx is running in the background and starts with Windows.
echo.
echo   Open TradingView Desktop as normal. The FIRST time you open it, it will
echo   close and reopen once by itself: that is Vespryx connecting. After that
echo   your levels paint on every symbol you look at.
echo.
echo   Your toolkit is in:  %DEST%
echo.
echo   If the bridge stops working later, double-click this file again.
echo   To take Vespryx out of your startup:  node tools\autostart.mjs --uninstall
echo   ---------------------------------------------------------------
echo.
pause
endlocal
