#!/bin/bash
# =========================================================
# CHECK FRONTEND - Verifica se backend Node.js e' attivo
# =========================================================

LOG_FILE="/var/log/ids/backend.log"

mkdir -p /var/log/ids

if systemctl is-active --quiet ids-backend.service 2>/dev/null; then
    exit 0
else
    echo "[$(date)] Backend Node.js NON attivo" >> "$LOG_FILE"
    systemctl start ids-backend.service 2>> "$LOG_FILE" || true

    sleep 3

    if systemctl is-active --quiet ids-backend.service 2>/dev/null; then
        echo "[$(date)] Backend riavviato con successo via systemd" >> "$LOG_FILE"
    else
        echo "[$(date)] ERRORE: Backend non si e' avviato - verificare con: journalctl -u ids-backend -n 20" >> "$LOG_FILE"
    fi
fi
