#!/usr/bin/env bash
# check-mcp-atlassian.sh
# Detect whether mcp-atlassian is wired into the active Claude / Cursor client.
# Prints PASS / MISSING with the install path that fits the current client.
#
# Usage: bash scripts/check-mcp-atlassian.sh
# Exit: 0 if installed, 1 if missing, 2 if check was inconclusive.

set -u

FOUND=0
DETAILS=""

# 1. The MCP server name shows up in the running client's config.
CANDIDATE_CONFIGS=(
  "$HOME/.claude/settings.json"
  "$HOME/.cursor/mcp.json"
  "$HOME/.codex/config.yaml"
  "$HOME/.claude.json"
  "$(pwd)/.mcp.json"
)

for cfg in "${CANDIDATE_CONFIGS[@]}"; do
  if [[ -f "$cfg" ]]; then
    if grep -qiE "mcp-atlassian|sooperset/mcp-atlassian" "$cfg" 2>/dev/null; then
      FOUND=1
      DETAILS="$cfg"
      break
    fi
  fi
done

# 2. Active client processes. If the MCP is loaded we usually see a node / uv
#    process with the server's name in argv.
if [[ $FOUND -eq 0 ]]; then
  if command -v ps >/dev/null 2>&1; then
    if ps -ef 2>/dev/null | grep -qiE "mcp-atlassian|sooperset.*atlassian"; then
      FOUND=1
      DETAILS="(running process)"
    fi
  fi
fi

# 3. npx cache. If installed globally, it lands here.
if [[ $FOUND -eq 0 ]]; then
  if [[ -d "$HOME/.npm/_npx" ]] && find "$HOME/.npm/_npx" -type d -name "*atlassian*" 2>/dev/null | grep -q .; then
    FOUND=1
    DETAILS="(npx cache)"
  fi
fi

if [[ $FOUND -eq 1 ]]; then
  echo "PASS: mcp-atlassian detected in ${DETAILS:-unknown location}"
  echo
  echo "Verify the active client can see it:"
  echo "  - Claude Code : restart the session, then list /mcp"
  echo "  - Cursor      : Cursor > Settings > MCP, look for 'mcp-atlassian'"
  echo "  - Codex CLI   : /mcp list"
  exit 0
fi

cat <<'EOF'
MISSING: mcp-atlassian is not wired into the active Claude / Cursor client.

The Confluence + Jira tools you need are exposed by this MCP server:
  https://github.com/sooperset/mcp-atlassian

Install path depends on the client in use:

  Claude Code
    claude mcp add atlassian \
      -e CONFLUENCE_URL=https://bass.netcracker.com \
      -e CONFLUENCE_USERNAME=<your-username> \
      -e CONFLUENCE_API_TOKEN=<your-token> \
      -- npx -y mcp-atlassian
    # Add JIRA_* envs for Jira access too.

  Cursor (project-level .mcp.json)
    {
      "mcpServers": {
        "atlassian": {
          "command": "npx",
          "args": ["-y", "mcp-atlassian"],
          "env": {
            "CONFLUENCE_URL": "https://bass.netcracker.com",
            "CONFLUENCE_USERNAME": "<your-username>",
            "CONFLUENCE_API_TOKEN": "<your-token>"
          }
        }
      }
    }

  Codex CLI
    Add to ~/.codex/config.yaml:
      mcp_servers:
        atlassian:
          command: npx
          args: ["-y", "mcp-atlassian"]
          env:
            CONFLUENCE_URL: https://bass.netcracker.com
            CONFLUENCE_USERNAME: <your-username>
            CONFLUENCE_API_TOKEN: <your-token>

Approval note: the BASS "Cursor MCPs approval status" page lists mcp-atlassian
as "Not approved" by default. Check the current row before relying on it for
governed spaces; if governance has not approved it yet, your post will land
but the space admin may revert the page.

After install: restart the client, then re-run this script.
EOF
exit 1
