@echo off
setlocal
set "SCRIPT_DIR=%~dp0"
set "PUBLISH_DIR=%SCRIPT_DIR%..\publish\lsp"
for %%I in ("%PUBLISH_DIR%") do set "PUBLISH_DIR=%%~fI"
set "ARTIFACT_ROOT=%SCRIPT_DIR%.."
for %%I in ("%ARTIFACT_ROOT%") do set "BTRQL_ARTIFACT_ROOT=%%~fI"
set "BTRQL_ARTIFACT_PROFILE=demo"
set "BTRQL_ARTIFACT_DIALECTS=clickhouse"
set "BTRQL_ARTIFACT_CAPABILITIES=compile,formatter-hooks,lsp-jsonrpc"
set "TREE_SITTER_ROOT=%SCRIPT_DIR%..\tree-sitter"
for %%I in ("%TREE_SITTER_ROOT%") do set "TREE_SITTER_ROOT=%%~fI"
set "BTRQL_TREE_SITTER_GRAMMAR_ROOT=%TREE_SITTER_ROOT%"
set "BTRQL_TREE_SITTER_SQL_GRAMMAR_ROOT=%TREE_SITTER_ROOT%\vendor\tree-sitter-sql"
if exist "%TREE_SITTER_ROOT%\native\tree-sitter-btrql-current-clickhouse-docs.dll" set "BTRQL_TREE_SITTER_PARSER_LIBRARY=%TREE_SITTER_ROOT%\native\tree-sitter-btrql-current-clickhouse-docs.dll"
if exist "%TREE_SITTER_ROOT%\native\tree-sitter-sql.dll" set "BTRQL_TREE_SITTER_SQL_PARSER_LIBRARY=%TREE_SITTER_ROOT%\native\tree-sitter-sql.dll"
if exist "%TREE_SITTER_ROOT%\native\tree-sitter.dll" set "BTRQL_TREE_SITTER_RUNTIME_LIBRARY=%TREE_SITTER_ROOT%\native\tree-sitter.dll"

if exist "%PUBLISH_DIR%\Btrql.ClickHouseNative.Lsp.exe" (
  "%PUBLISH_DIR%\Btrql.ClickHouseNative.Lsp.exe" --backend-cli "%SCRIPT_DIR%btrql.bat" --artifact-profile demo %*
) else (
  dotnet "%PUBLISH_DIR%\Btrql.ClickHouseNative.Lsp.dll" --backend-cli "%SCRIPT_DIR%btrql.bat" --artifact-profile demo %*
)
exit /b %ERRORLEVEL%
