#!/bin/bash
# Log Rotation Script
# Rotates logs when they exceed size limit

LOG_DIR="/var/www/html/homoeopathy/logs"
MAX_SIZE=$((10 * 1024 * 1024)) # 10MB

for log in errors.log access.log ai.log cron.log; do
    LOG_FILE="$LOG_DIR/$log"
    
    if [ -f "$LOG_FILE" ]; then
        SIZE=$(stat -c%s "$LOG_FILE")
        
        if [ $SIZE -gt $MAX_SIZE ]; then
            TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
            mv "$LOG_FILE" "$LOG_FILE.$TIMESTAMP"
            gzip "$LOG_FILE.$TIMESTAMP"
            touch "$LOG_FILE"
            echo "[$(date)] Rotated $log (was $SIZE bytes)" >> "$LOG_DIR/rotate.log"
        fi
    fi
done

# Delete logs older than 30 days
find "$LOG_DIR" -name "*.gz" -mtime +30 -delete