#!/bin/bash

# This script runs the AWS Logs MCP server in Docker with AWS credentials from environment variables

# Check if AWS credentials are exported
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  echo "⚠️  Warning: AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY environment variables are not set."
  echo "   The server will not be able to connect to AWS services."
  
  # Check for AWS CLI
  if command -v aws &> /dev/null; then
    echo "ℹ️  AWS CLI is installed. You can get credentials using:"
    echo "   eval \$(aws configure export-credentials)"
    echo "   # OR for specific profile:"
    echo "   eval \$(aws configure export-credentials --profile your-profile)"
  fi
  
  echo ""
  echo "Do you want to continue without AWS credentials? (y/n)"
  read answer
  if [ "$answer" != "y" ]; then
    echo "Exiting..."
    exit 1
  fi
fi

# Display current AWS environment
echo ""
echo "Current AWS configuration:"
echo "AWS_REGION=${AWS_REGION:-us-east-1}"
if [ ! -z "$AWS_ACCESS_KEY_ID" ]; then
  echo "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:0:5}... (${#AWS_ACCESS_KEY_ID} characters)"
fi
if [ ! -z "$AWS_SECRET_ACCESS_KEY" ]; then
  echo "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:0:5}... (${#AWS_SECRET_ACCESS_KEY} characters)"
fi
if [ ! -z "$AWS_SESSION_TOKEN" ]; then
  echo "AWS_SESSION_TOKEN is set (${#AWS_SESSION_TOKEN} characters)"
fi
echo ""

# Run the Docker container with AWS credentials properly passed
docker-compose -f docker-compose.test.yml up --build