#!/bin/bash
# Setup log rotation for Docker containers
# Run this script on the host system with sudo

set -e

LOGROTATE_CONF="/etc/logrotate.d/overseas-appointment"

echo "Setting up log rotation for Overseas Appointment System..."

# Create logrotate configuration
cat > "${LOGROTATE_CONF}" << 'EOF'
# Docker container logs
/var/lib/docker/containers/*/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    copytruncate
    maxsize 100M
}

# Application logs (if mounted to host)
/var/log/overseas-appointment/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 root root
    sharedscripts
}
EOF

echo "Log rotation configuration created at: ${LOGROTATE_CONF}"

# Test the configuration
logrotate -d "${LOGROTATE_CONF}" 2>/dev/null && echo "Configuration is valid."

echo "Log rotation setup complete!"
echo ""
echo "To manually run log rotation:"
echo "  sudo logrotate -f ${LOGROTATE_CONF}"
