#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PUBLISH_DIR="$(cd "$SCRIPT_DIR/../publish/cli" && pwd)"
ARTIFACT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
TREE_SITTER_ROOT="$ARTIFACT_ROOT/tree-sitter"
export BTRQL_ARTIFACT_ROOT="$ARTIFACT_ROOT"
export BTRQL_ARTIFACT_PROFILE='demo'
export BTRQL_ARTIFACT_DIALECTS='postgresql'
export BTRQL_ARTIFACT_CAPABILITIES='compile,formatter-hooks,lsp-jsonrpc'
export BTRQL_TREE_SITTER_GRAMMAR_ROOT="$TREE_SITTER_ROOT"
export BTRQL_TREE_SITTER_SQL_GRAMMAR_ROOT="$TREE_SITTER_ROOT"
export BTRQL_TREE_SITTER_RUNTIME_LIBRARY="$TREE_SITTER_ROOT/native/libtree-sitter.dylib"
export BTRQL_TREE_SITTER_PARSER_LIBRARY="$TREE_SITTER_ROOT/native/libtree-sitter-btrql-current-postgresql-docs.dylib"
export BTRQL_TREE_SITTER_SQL_PARSER_LIBRARY="$TREE_SITTER_ROOT/native/libtree-sitter-sql.dylib"
BINARY="$PUBLISH_DIR/Btrql.PgNative.Cli"
if [[ ! -x "$BINARY" ]]; then
  echo "Missing NativeAOT format executable: $BINARY" >&2
  exit 127
fi
exec "$BINARY" format "$@"
