@echo off
REM Weave AI Agent CLI — Windows Entry Point
REM Usage: weave [command] [options]
REM        w [command] [options]

setlocal enabledelayedexpansion

REM Resolve script directory (handle symlinks by following node resolution)
set "SCRIPT_DIR=%~dp0"
set "DIST_PATH=%SCRIPT_DIR%dist\index.js"

if not exist "%DIST_PATH%" (
  echo [33m[info][0m weave-cli first run — installing ^& building... >&2
  cd /d "%SCRIPT_DIR%"
  call npm install --silent 2>nul
  call npm run build 2>nul
  if not exist "%DIST_PATH%" (
    echo [31m[error][0m build failed. Run manually: cd /d "%SCRIPT_DIR%" ^&^& npm install ^&^& npm run build >&2
    exit /b 1
  )
  echo [32m[ok][0m weave-cli built successfully. >&2
)

node "%SCRIPT_DIR%bin\cli.js" %*
