#!/bin/bash
# Script para verificar status do servidor
# Execute: bash check-server-status.sh

echo "=== VERIFICANDO STATUS DO SERVIDOR ===\n"

echo "1. Verificando Nginx..."
if systemctl is-active --quiet nginx; then
    echo "   ✅ Nginx está rodando"
    nginx -t 2>&1 | head -5
else
    echo "   ❌ Nginx NÃO está rodando"
    echo "   Tente: systemctl start nginx"
fi
echo ""

echo "2. Verificando PHP-FPM..."
if systemctl is-active --quiet php8.1-fpm; then
    echo "   ✅ PHP 8.1-FPM está rodando"
elif systemctl is-active --quiet php8.2-fpm; then
    echo "   ✅ PHP 8.2-FPM está rodando"
elif systemctl is-active --quiet php-fpm; then
    echo "   ✅ PHP-FPM está rodando"
else
    echo "   ❌ PHP-FPM NÃO está rodando"
    echo "   Tente: systemctl start php8.1-fpm"
fi
echo ""

echo "3. Verificando logs do Nginx (últimas 10 linhas)..."
if [ -f /var/log/nginx/error.log ]; then
    echo "   Últimas linhas:"
    tail -10 /var/log/nginx/error.log | sed 's/^/   /'
else
    echo "   ⚠️  Arquivo de log não encontrado"
fi
echo ""

echo "4. Verificando se a porta 80/443 está ouvindo..."
netstat -tlnp | grep -E ':(80|443)' | head -3
echo ""

echo "5. Testando conexão local..."
curl -I http://localhost/api/v1/setup/check 2>&1 | head -5
echo ""

echo "=== FIM ===\n"

