#!/bin/bash
#
# WhatsApp Server Monitor Script
# This script checks if the WhatsApp server is running and restarts it if needed
#

WHATSAPP_URL="http://127.0.0.1:6101"
LOG_FILE="/home/ashraffarid2010/halavoice.store/whatsapp-server/monitor.log"

# Function to log messages
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}

# Check if WhatsApp server is responding
response=$(curl -s -o /dev/null -w "%{http_code}" "$WHATSAPP_URL" 2>/dev/null)

if [ "$response" != "200" ]; then
    log "WhatsApp server is not responding (HTTP $response). Restarting..."
    pm2 restart halavoice-whatsapp >> "$LOG_FILE" 2>&1
    log "WhatsApp server restarted"
else
    log "WhatsApp server is healthy"
fi
