#!/bin/bash
# Starts browser-tools connector server (AgentDesk) on port 3025
# This bridges the Chrome extension ↔ browser-tools-mcp
#
# Architecture:
#   Chrome extension → browser-tools-server:3025 → browser-tools-mcp → Claude

PIDFILE="/tmp/browser-tools-server.pid"

if [ -f "$PIDFILE" ] && kill -0 "$(cat $PIDFILE)" 2>/dev/null; then
    echo "browser-tools-server already running (PID $(cat $PIDFILE))"
    exit 0
fi

echo "Starting browser-tools-server on :3025..."
nohup npx @agentdeskai/browser-tools-server@1.2.0 > /tmp/browser-tools-server.log 2>&1 &
echo $! > "$PIDFILE"
sleep 1

if kill -0 "$(cat $PIDFILE)" 2>/dev/null; then
    echo "Started (PID $(cat $PIDFILE)) → log: /tmp/browser-tools-server.log"
else
    echo "Failed to start. Check: /tmp/browser-tools-server.log"
    exit 1
fi
