@echo off
setlocal
set "SCRIPT_DIR=%~dp0"
set "PUBLISH_DIR=%SCRIPT_DIR%..\publish\cli"
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=postgresql"
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-postgresql-docs.dll" set "BTRQL_TREE_SITTER_PARSER_LIBRARY=%TREE_SITTER_ROOT%\native\tree-sitter-btrql-current-postgresql-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.PgNative.Cli.exe" (
  "%PUBLISH_DIR%\Btrql.PgNative.Cli.exe" format %*
) else (
  dotnet "%PUBLISH_DIR%\Btrql.PgNative.Cli.dll" format %*
)
exit /b %ERRORLEVEL%
