#!/bin/sh
# opencode-blender-mcp.sh — Configures opencode.json to connect to Blender MCP addon.
#
# The Blender MCP addon (https://github.com/ahujasid/blender-mcp) runs a TCP
# server inside Blender on port 9876. This script writes or updates an opencode
# config so that opencode's MCP client can talk to the Blender server at
# localhost:9876.
#
# Prerequisites:
#   - jq, and at least one of nc / perl / python3 (for the port check)
#   - uvx (installed via `uv tool install` or `pip install uvx`)
#   - Blender must be running with the blender-mcp addon enabled
#
# Usage:
#   ./opencode-blender-mcp.sh [path-to-opencode.json]
#
#   If no path is given, ./opencode.json is used.  The file is created if it
#   does not exist, or updated in-place if it does.
#
# Environment variables the Blender server respects:
#   BLENDER_HOST  — default "localhost"
#   BLENDER_PORT  — default "9876"
#   DISABLE_TELEMETRY — set to "true" to opt out of usage data
#
# To start Blender with the addon from the command line:
#   blender --python addon.py
#
# The addon registers a panel in the 3D Viewport sidebar (press N) under
# "BlenderMCP" where you can enable optional integrations (Poly Haven,
# Sketchfab, Hyper3D).

set -eu

CONFIG_PATH="${1:-opencode.json}"

# -- Dependency checks -------------------------------------------------------

# jq is used to read and write JSON config below.
if ! command -v jq >/dev/null 2>&1; then
  echo "Error: 'jq' is required but not installed." >&2
  exit 1
fi

# Verify the Blender addon's TCP server is actually running before modifying
# any config.  The addon listens on 127.0.0.1:9876 by default
# (see BlenderMCPServer.__init__ in addon.py).
if command -v nc >/dev/null 2>&1; then
  if ! nc -z 127.0.0.1 9876 2>/dev/null; then
    echo "Error: blender-mcp is not listening on 127.0.0.1:9876." >&2
    echo "       Make sure Blender is running and the addon is enabled." >&2
    exit 1
  fi
elif command -v perl >/dev/null 2>&1; then
  if ! perl -MIO::Socket::INET -e 'exit(!IO::Socket::INET->new(PeerHost=>"127.0.0.1",PeerPort=>9876))' 2>/dev/null; then
    echo "Error: blender-mcp is not listening on 127.0.0.1:9876." >&2
    echo "       Make sure Blender is running and the addon is enabled." >&2
    exit 1
  fi
elif command -v python3 >/dev/null 2>&1; then
  if ! python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('127.0.0.1',9876))" 2>/dev/null; then
    echo "Error: blender-mcp is not listening on 127.0.0.1:9876." >&2
    echo "       Make sure Blender is running and the addon is enabled." >&2
    exit 1
  fi
else
  echo "Warning: cannot verify port 9876 (install nc, perl, or python3). Skipping check." >&2
fi

# -- Write / update opencode config -----------------------------------------
#
# The resulting config tells opencode to spawn `uvx blender-mcp` as its MCP
# client, which in turn connects to the TCP server inside Blender.

if [ ! -f "$CONFIG_PATH" ]; then
  jq -n '{mcp: {"blender-mcp": {type: "local", command: ["uvx", "blender-mcp"], enabled: true, environment: {BLENDER_HOST: "localhost", BLENDER_PORT: "9876", DISABLE_TELEMETRY: "true"}}}}' > "$CONFIG_PATH"
  echo "Created $(realpath "$CONFIG_PATH") with Blender MCP enabled."
else
  tmp="$CONFIG_PATH.tmp"
  jq '.mcp."blender-mcp" = {type: "local", command: ["uvx", "blender-mcp"], enabled: true, environment: {BLENDER_HOST: "localhost", BLENDER_PORT: "9876", DISABLE_TELEMETRY: "true"}}' "$CONFIG_PATH" > "$tmp" &&
  mv "$tmp" "$CONFIG_PATH"
  echo "Updated $(realpath "$CONFIG_PATH") with Blender MCP enabled."
fi

# == Hints ==================================================================

cat <<'HINTS'

A) Enable Online mode in Blender:
   Edit → Preferences → System → Network → check "Allow Online Access"
   Without this, Python network calls (used for Poly Haven, Sketchfab,
   Hyper3D downloads) will fail silently.

B) Blender Addon: https://github.com/ahujasid/blender-mcp/blob/main/addon.py
   After enabling Online mode, import the addon into Blender:
     Edit → Preferences → Add-ons → Install from Disk... → select addon.py
   Then enable it by searching for "Blender MCP" and checking the box.

C) After running this script, verify the MCP server is recognized:
     opencode mcp list
   You should see "blender-mcp" listed as enabled.  If not, try restarting
   opencode or re-running this script.
HINTS
