#!/bin/sh
# Mineralis MCP bridge installer. Fetches the bridge from the service that serves this
# script, installs its one dependency, and registers it with your MCP client.
#
# Usage:  curl -fsSL https://mineralis.digiearth.com.au/mineralis-mcp/install.sh | sh
#         MINERALIS_API_TOKEN=mrl_... curl -fsSL https://mineralis.digiearth.com.au/mineralis-mcp/install.sh | sh
#         curl -fsSL https://mineralis.digiearth.com.au/mineralis-mcp/install.sh | sh -s -- /path/to/install/in
#
# It creates one directory, runs npm install inside it, and - if the claude CLI is on
# your PATH - registers the server with it. Nothing else on your machine is touched,
# and your API token is never written to a file by this script.
set -eu
BASE="${MINERALIS_MCP_BASE:-https://mineralis.digiearth.com.au/mineralis-mcp/}"
DIR="${1:-$HOME/.mineralis-mcp}"
echo "Installing the Mineralis MCP bridge into $DIR"
mkdir -p "$DIR/src"
for FILE in package.json package-lock.json README.md AGENT_GUIDE.md example_input.csv src/server.mjs src/tools.mjs src/mineralis.mjs src/packet.mjs src/catalog.mjs src/catalog.json src/constants.mjs src/summary.mjs; do
  echo "  $FILE"
  curl -fsSL "$BASE$FILE" -o "$DIR/$FILE"
done
if ! command -v node >/dev/null 2>&1; then
  echo "Node 20 or newer is required and 'node' was not found on your PATH." >&2
  exit 1
fi
echo "Installing dependencies with npm"
(cd "$DIR" && npm install --omit=dev --no-audit --no-fund)
SERVER="$DIR/src/server.mjs"
echo
if [ -n "${MINERALIS_API_TOKEN:-}" ] && command -v claude >/dev/null 2>&1; then
  echo "Registering with Claude Code"
  claude mcp add mineralis --env "MINERALIS_API_TOKEN=$MINERALIS_API_TOKEN" -- node "$SERVER"
  echo "Done. Check it with /mcp inside Claude Code; the tools appear as mineralis_*."
  exit 0
fi
echo "Installed. To register it, run this with your own API credential:"
echo
echo "  claude mcp add mineralis --env MINERALIS_API_TOKEN=YOUR_TOKEN -- node $SERVER"
echo
echo "For Claude Desktop, Cursor or VS Code, add this to the client's MCP config:"
cat <<JSON
  {"mcpServers": {"mineralis": {"command": "node", "args": ["$SERVER"],
    "env": {"MINERALIS_API_TOKEN": "YOUR_TOKEN"}}}}
JSON
echo
echo "Create the credential on the Claude / Codex / Grok Integration tab of the dashboard. Give it the billing"
echo "scope if the agent should read your balance or buy tokens."
