@echo off
:: ============================================================
:: setup-once.bat - One-time setup for transparent-encrypt-file-mcp
:: Run this ONCE after copying the transparent-encrypt-file-mcp
:: folder to your machine. It installs the npm dependencies.
::
:: Why we do NOT call "npm" directly:
::   * npm.cmd segfaults on some Windows builds when launched from Git Bash;
::   * "node install" (a previous bug in this file) is not a package manager
::     command at all - it makes Node try to run a script named "install".
::   We therefore invoke npm's own CLI entry point (npm-cli.js) through the
::   Node binary we already detected. That path is version- and shell-agnostic.
:: ============================================================

setlocal EnableExtensions

echo.
echo ============================================
echo  transparent-encrypt-file-mcp - One-time Setup
echo ============================================
echo.

cd /d "%~dp0"

:: ---- Detect Node.js ----
set "NODE_EXE="
for /f "delims=" %%d in ('dir /b /ad /o-n "%USERPROFILE%\.workbuddy\binaries\node\versions" 2^>nul') do (
    if not defined NODE_EXE if exist "%USERPROFILE%\.workbuddy\binaries\node\versions\%%d\node.exe" set "NODE_EXE=%USERPROFILE%\.workbuddy\binaries\node\versions\%%d\node.exe"
)
if not defined NODE_EXE if exist "D:\Node.js\node.exe" set "NODE_EXE=D:\Node.js\node.exe"
if not defined NODE_EXE if exist "%ProgramFiles%\nodejs\node.exe" set "NODE_EXE=%ProgramFiles%\nodejs\node.exe"
if not defined NODE_EXE if exist "%LOCALAPPDATA%\Programs\node\node.exe" set "NODE_EXE=%LOCALAPPDATA%\Programs\node\node.exe"
if not defined NODE_EXE for /f "delims=" %%i in ('where node.exe 2^>nul') do (
    if not defined NODE_EXE set "NODE_EXE=%%i"
)

if not defined NODE_EXE (
    echo [ERROR] Node.js not found. Please install Node.js 18+ first.
    exit /b 1
)

echo [*] Using Node: %NODE_EXE%
"%NODE_EXE%" -v

:: ---- Locate npm's CLI entry point (npm-cli.js) ----
set "NPM_CLI="
for %%c in (
    "%NODE_EXE%\..\node_modules\npm\bin\npm-cli.js"
    "%ProgramFiles%\nodejs\node_modules\npm\bin\npm-cli.js"
    "%APPDATA%\npm\node_modules\npm\bin\npm-cli.js"
) do if not defined NPM_CLI if exist "%%~fc" set "NPM_CLI=%%~fc"

echo.
if defined NPM_CLI (
    echo [*] Installing dependencies via npm-cli.js ^(this may take 30-90 seconds^)...
    "%NODE_EXE%" "%NPM_CLI%" install --no-audit --no-fund
) else (
    echo [WARN] npm-cli.js not found; falling back to "call npm install".
    call npm install --no-audit --no-fund
)

if errorlevel 1 (
    echo.
    echo [ERROR] dependency install failed. Check network / proxy, or Node.js version ^(need 18+^).
    echo [HINT]  Manual fallback:
    echo         cd /d "%~dp0"
    echo         "%NODE_EXE%" "%%APPDATA%%\npm\node_modules\npm\bin\npm-cli.js" install
    exit /b 1
)

:: ---- Verify the server file parses ----
echo.
echo [*] Verifying index.js ...
if not exist "%~dp0index.js" (
    echo [ERROR] index.js not found. Copy references\server_template.js to index.js first.
    exit /b 1
)
"%NODE_EXE%" --check "%~dp0index.js"
if errorlevel 1 (
    echo [ERROR] Syntax check failed on index.js.
    exit /b 1
)

echo.
echo ============================================
echo  SETUP COMPLETE
echo ============================================
echo.
echo Next step: register the server in WorkBuddy's mcp.json.
echo   Preferred ^(truly avoids cmd.exe^):
echo     "command": "%NODE_EXE%",
echo     "args":    ["^<THIS_FOLDER^>\\index.js"]
echo   Alternative ^(launcher, auto-detects Node^):
echo     "command": "cmd",
echo     "args":    ["/c", "^<THIS_FOLDER^>\\start-portable.bat"]
echo.
echo Optional env vars ^(all defaulted; change only if you need to^):
echo     COM_TIMEOUT_MS=120000        Office/WPS/Outlook/AutoCAD COM
echo     WORD_COM_TIMEOUT_MS=30000    Word and PDF ^(PDF defaults to 60000^)
echo     CLI_TIMEOUT_MS=120000        7-Zip / LibreOffice / Open XML child process
echo   Run the check_status tool to see the values actually in effect.
echo.
echo Optional: the Python structured channel ^(scripts\*.py^) additionally needs
echo   pywin32 installed in an isolated venv:
echo     python -m venv ^<venv^>
echo     ^<venv^>\Scripts\pip.exe install pywin32
echo.
echo Then open the connector page for this machine and click Trust / Reconnect.
echo.

endlocal
