#!/bin/bash
# DSS MCP Server Startup Script

set -e

# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# Change to project root
cd "$PROJECT_ROOT"

# Ensure logs directory exists
mkdir -p "$PROJECT_ROOT/.dss/logs"

# Log startup
echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting DSS MCP Server"
echo "$(date '+%Y-%m-%d %H:%M:%S') - Project root: $PROJECT_ROOT"
echo "$(date '+%Y-%m-%d %H:%M:%S') - Python: $(which python3)"
echo "$(date '+%Y-%m-%d %H:%M:%S') - Python version: $(python3 --version)"

# Check for required dependencies
if ! python3 -c "import mcp" 2>/dev/null; then
    echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: MCP library not found. Install with: pip install mcp"
    exit 1
fi

if ! python3 -c "import httpx" 2>/dev/null; then
    echo "$(date '+%Y-%m-%d %H:%M:%S') - WARNING: httpx not found. Install with: pip install httpx"
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Debug tools will not work without httpx"
fi

# Start MCP server
echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting MCP server on ${DSS_MCP_HOST:-0.0.0.0}:${DSS_MCP_PORT:-3457}"

exec python3 -m tools.dss_mcp.server
