#!/bin/bash
# Kills conflicting host processes (chroot lets us reach host namespace)
if [ -d "/host_root" ]; then
    echo "Limpando processos conflitantes no HOST..."
    chroot /host_root /bin/bash -c "pkill -9 -f bridge_telegram.py" 2>/dev/null || true
    chroot /host_root /bin/bash -c "pkill -9 -f BotVPS.*main.py" 2>/dev/null || true
    chroot /host_root /bin/bash -c "pkill -9 -f watchdog.py" 2>/dev/null || true
    sleep 3
fi

# Inicia a ponte do Telegram em background
echo "Iniciando Telegram Bridge dentro do container..."
python3 bridge_telegram.py &

# Inicia o serviço web com log unbuffered
echo "Iniciando Uvicorn na porta ${PORT:-8001}..."
exec uvicorn main:app --host 0.0.0.0 --port ${PORT:-8001}
