#!/bin/bash
# 🚀 LAUNCH LOCAL OVERPASS ON MAC M1/M2/M3/M4
# Strategy: CLONE MODE (Most reliable on Mac/Docker)

# 1. Check Docker
if ! command -v docker &> /dev/null; then
    echo "❌ Docker is not installed."
    exit 1
fi

# 2. Configuration
DATA_DIR="$HOME/overpass_data"
CONTAINER_NAME="overpass_local"

echo "🍏 Setup for Apple Silicon (M-Series)"
echo "📂 Data Directory: $DATA_DIR"

# 3. Create data dir
mkdir -p "$DATA_DIR"

# 4. Cleanup
docker stop $CONTAINER_NAME 2>/dev/null || true
docker rm $CONTAINER_NAME 2>/dev/null || true

# 5. Launch Container (Clone Mode)
echo "🚀 Starting Overpass Container (CLONE MODE)..."
echo "ℹ️  This downloads a pre-indexed DB from Overpass servers."
echo "    It avoids compilation errors and is generally faster to start."

# Added --platform linux/amd64 to force Rosetta
docker run \
  --platform linux/amd64 \
  -e OVERPASS_META=no \
  -e OVERPASS_MODE=clone \
  -e OVERPASS_CLONE_SOURCE=http://dev.overpass-api.de/api_drolbr/ \
  -e OVERPASS_RULES_LOAD=10 \
  -v "$DATA_DIR":/db \
  -p 8080:80 \
  --name $CONTAINER_NAME \
  -d \
  wiktorn/overpass-api

echo "✅ Container started!"
echo "📜 Monitor logs: docker logs -f $CONTAINER_NAME"
echo "⏳ Wait for 'Ready to serve requests'."

