#!/usr/bin/env bash
# Cursor MCP: mysql-mcp-server via WSL (Node 20+ required for import attributes).
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT="$(cd "${SCRIPT_DIR}/.." && pwd)"
cd "${PROJECT}"

NODE20="${HOME}/.local/node-v20.19.2/bin/node"
WIN_NODE="/mnt/c/nvm4w/nodejs/node.exe"

# 1) WSL Node 20 — 可连 172.31.x 内网 MySQL
if [ -x "${NODE20}" ]; then
  export PATH="$(dirname "${NODE20}"):${PATH}"
  exec npx -y @yclenove/mysql-mcp-server
fi

# 2) Windows Node 22（interop）
if [ -x "${WIN_NODE}" ]; then
  WSL_USER="$(whoami)"
  MCP_JS="/mnt/c/Users/${WSL_USER}/AppData/Roaming/npm/node_modules/@yclenove/mysql-mcp-server/dist/index.js"
  if [ -f "${MCP_JS}" ]; then
    exec "${WIN_NODE}" "${MCP_JS}"
  fi
fi

echo "ERROR: Need Node 20+. Run in WSL: bash scripts/wsl_install_node20.sh" >&2
exit 1
