#!/bin/bash
# =========================================================
# RESTART ALL - Riavvio completo sistema IDS
# =========================================================

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

echo "$(date): === RESTART SETTIMANALE SISTEMA IDS ===" >> "$LOG_FILE"

# Stop all services
echo "$(date): Arresto servizi..." >> "$LOG_FILE"
pkill -f "python_ml/main.py"
pkill -f "vite"
pkill -f "npm run dev"

sleep 5

# Clean temp files
echo "$(date): Pulizia file temporanei..." >> "$LOG_FILE"
rm -f /var/log/ids/*.pid
find /tmp -name "ids_*" -mtime +7 -delete 2>/dev/null

# Restart services
echo "$(date): Riavvio servizi..." >> "$LOG_FILE"
/opt/ids/deployment/check_backend.sh >> "$LOG_FILE" 2>&1
sleep 3
/opt/ids/deployment/check_frontend.sh >> "$LOG_FILE" 2>&1

echo "$(date): Restart completato!" >> "$LOG_FILE"
