#!/usr/bin/env bash
# Starts the Teletraan Framework installer agent.
# Launches an interactive Claude Code session guided by CLAUDE.md,
# with MCP tools from install-tools.js available.
set -euo pipefail

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

# Install npm dependencies if node_modules is absent.
if [ ! -d "${SCRIPT_DIR}/node_modules" ]; then
  echo "→ Installing installer agent dependencies..."
  npm install --prefix "${SCRIPT_DIR}" --silent
fi

# Generate a runtime MCP config with the absolute path to install-tools.js.
TMP_MCP=$(mktemp /tmp/teletraan-installer-mcp-XXXXXX)
trap 'rm -f "${TMP_MCP}"' EXIT

cat > "${TMP_MCP}" <<EOF
{
  "mcpServers": {
    "teletraan-installer": {
      "command": "node",
      "args": ["${SCRIPT_DIR}/install-tools.js"]
    }
  }
}
EOF

# Change into agent-installer so Claude picks up CLAUDE.md automatically.
cd "${SCRIPT_DIR}"

exec claude \
  --dangerously-skip-permissions \
  --mcp-config "${TMP_MCP}" \
  -- "Start the Teletraan Framework installation. Follow the instructions in CLAUDE.md to guide the user through setup."
