@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
chcp 65001 >nul
title Soar Revit MCP — Installer (V2)

set "HERE=%~dp0"
set "EXE=%HERE%launcher.dist\launcher.exe"
pushd "%HERE%"

setlocal DISABLEDELAYEDEXPANSION
echo "%HERE%" | findstr /R "[()!^&]" >nul 2>&1
if not errorlevel 1 (
    echo.
    echo ============================================================
    echo   [X] ERROR: Install path contains special characters
    echo       such as parentheses, !, ^& or ^
    echo.
    echo   Current path: %HERE%
    echo.
    echo   Please move the folder to a simple path like:
    echo       D:\Claude\Soar_Revit_MCP_V1.0\
    echo ============================================================
    endlocal
    pause
    popd
    exit /b 1
)
endlocal

echo.
echo ============================================================
echo   Soar Revit MCP — V2 Installer
echo   For Autodesk Revit 2025
echo ============================================================
echo.

echo [1/5] Checking launcher ...
if not exist "%EXE%" (
    echo   [X] launcher.dist\launcher.exe not found.
    echo       Please ensure the installation package is complete.
    goto :fail
)
echo   OK.

echo [2/5] License key
if exist "%HERE%license.txt" (
    for /f "usebackq delims=" %%L in ("%HERE%license.txt") do set "LICENSE=%%L"
    echo   Existing license.txt found: !LICENSE!
    set /p "CHANGE=  Change it? (y/N): "
    if /I "!CHANGE!"=="Y" goto :ask_license
    goto :have_license
)
:ask_license
set /p "LICENSE=  Paste license key (SOAR-XXXX-XXXX-XXXX-XXXX): "
> "%HERE%license.txt" echo !LICENSE!

:have_license

echo [3/5] Installing Revit Addin for Revit 2025 ...
set "REVIT_ADDINS=%APPDATA%\Autodesk\Revit\Addins\2025"
if not exist "%APPDATA%\Autodesk\Revit\Addins" (
    echo   [X] Autodesk Revit not found on this machine.
    echo       Install Revit 2025 first, then re-run this installer.
    goto :fail
)
if not exist "%REVIT_ADDINS%" mkdir "%REVIT_ADDINS%"
copy /Y "%HERE%bridge\RevitMCPBridge.addin"   "%REVIT_ADDINS%\" >nul
copy /Y "%HERE%bridge\RevitMCPBridge.dll"     "%REVIT_ADDINS%\" >nul
copy /Y "%HERE%bridge\Newtonsoft.Json.dll"    "%REVIT_ADDINS%\" >nul
echo   Done (3 files copied to %REVIT_ADDINS%).

echo [4/5] Verifying license with Cloudflare ...
"%EXE%" --install-check
if errorlevel 1 (
    echo   [X] License invalid or server unreachable.
    goto :fail
)
echo   Modules loaded to RAM successfully.

echo [5/5] Registering MCP with Claude Desktop / Cowork ...
"%EXE%" --register-mcp
if errorlevel 1 (
    echo   Warning: Auto-register failed. Configure manually using config\cowork_mcp.json.template
) else (
    echo   MCP registered successfully.
)

echo.
echo ============================================================
echo   Installation complete!
echo ============================================================
echo.
echo   Next steps:
echo   1. Restart Autodesk Revit 2025 to load the addin
echo   2. Open any .rvt project
echo   3. Restart Cowork / Claude Desktop to see the MCP
echo   4. Try: "ping Revit"
echo.
popd
pause
endlocal
exit /b 0

:fail
echo.
echo ============================================================
echo   [X] Installation FAILED. See errors above.
echo ============================================================
popd
pause
endlocal
exit /b 1
