#!/bin/bash
set -e

echo "Installing Granola MCP for Claude Desktop..."

# 1. Install uv if not present
if ! command -v uvx &> /dev/null; then
    echo "Installing uv..."
    curl -LsSf https://astral.sh/uv/install.sh | sh
    export PATH="$HOME/.local/bin:$PATH"
fi

# 2. Create/update Claude Desktop config
CONFIG_DIR="$HOME/Library/Application Support/Claude"
CONFIG_FILE="$CONFIG_DIR/claude_desktop_config.json"

mkdir -p "$CONFIG_DIR"

if [ -f "$CONFIG_FILE" ]; then
    # Merge with existing config
    echo "Updating existing Claude Desktop config..."
    python3 << 'PYEOF'
import json
from pathlib import Path

config_path = Path.home() / "Library/Application Support/Claude/claude_desktop_config.json"
config = json.loads(config_path.read_text()) if config_path.exists() else {}

if "mcpServers" not in config:
    config["mcpServers"] = {}

config["mcpServers"]["granola"] = {
    "command": "uvx",
    "args": ["granola-mcp"]
}

config_path.write_text(json.dumps(config, indent=2))
PYEOF
else
    echo "Creating Claude Desktop config..."
    cat > "$CONFIG_FILE" << 'EOF'
{
  "mcpServers": {
    "granola": {
      "command": "uvx",
      "args": ["granola-mcp"]
    }
  }
}
EOF
fi

# 3. Pre-cache the package
echo "Downloading package..."
uvx --quiet granola-mcp --version 2>/dev/null || true

echo ""
echo "Granola MCP installed!"
echo ""
echo "Next: Restart Claude Desktop to activate."
echo "Then ask Claude: 'What meetings did I have this week?'"
