#!/bin/bash

# Nextcloud Log Cleanup Script
# Cleans up old log files to free up disk space
# Created: $(date)

set -e

echo "=== Nextcloud Log Cleanup Script ==="
echo ""

# Get container ID
echo "Getting Nextcloud container ID..."
CONTAINER_ID=$(ssh root@omv800 "docker ps -q -f name=nextcloud" | head -1)
if [ -z "$CONTAINER_ID" ]; then
    echo "❌ Nextcloud container not found"
    exit 1
fi
echo "✅ Container found: $CONTAINER_ID"

# Check current log files and sizes
echo ""
echo "Current log files and sizes:"
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID find /var/www/html/data/ -name '*.log*' -type f -exec ls -lah {} \;"

# Calculate total size before cleanup
echo ""
echo "Calculating total log size before cleanup..."
TOTAL_SIZE_BEFORE=$(ssh root@omv800 "docker exec -u 33 $CONTAINER_ID find /var/www/html/data/ -name '*.log*' -type f -exec du -ch {} + | tail -1 | cut -f1")
echo "Total log size before cleanup: $TOTAL_SIZE_BEFORE"

# Backup current log before cleanup (optional)
echo ""
echo "Creating backup of current log..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID cp /var/www/html/data/nextcloud.log /var/www/html/data/nextcloud.log.backup.$(date +%Y%m%d_%H%M%S)"

# Clean up old log files
echo ""
echo "Cleaning up old log files..."

# Remove old rotated logs (keep only current log)
echo "Removing old rotated log files..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID find /var/www/html/data/ -name '*.log.[0-9]*' -type f -delete"

# Clear current log file content (keep file but empty it)
echo "Clearing current log file content..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID truncate -s 0 /var/www/html/data/nextcloud.log"

# Check if there are any other large log files
echo "Checking for other large log files..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID find /var/www/html/data/ -name '*.log*' -type f -size +10M -exec ls -lah {} \;"

# Verify cleanup
echo ""
echo "Verifying cleanup..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID find /var/www/html/data/ -name '*.log*' -type f -exec ls -lah {} \;"

# Calculate total size after cleanup
echo ""
echo "Calculating total log size after cleanup..."
TOTAL_SIZE_AFTER=$(ssh root@omv800 "docker exec -u 33 $CONTAINER_ID find /var/www/html/data/ -name '*.log*' -type f -exec du -ch {} + | tail -1 | cut -f1")
echo "Total log size after cleanup: $TOTAL_SIZE_AFTER"

# Check available disk space
echo ""
echo "Checking available disk space on data directory..."
ssh root@omv800 "df -h /srv/dev-disk-by-uuid-0f772f0b-917d-4337-a3c5-5cc5d3badac9/nextcloud-db"

# Set up log rotation for future
echo ""
echo "Setting up log rotation configuration..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID cat > /var/www/html/data/.logrotate.conf << 'EOF'
/var/www/html/data/nextcloud.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 644 www-data www-data
    postrotate
        /usr/bin/docker exec -u 33 $CONTAINER_ID php /var/www/html/occ log:rotate
    endscript
}
EOF"

echo ""
echo "=== Log Cleanup Complete ==="
echo "✅ Old log files removed"
echo "✅ Current log cleared"
echo "✅ Log rotation configured"
echo ""
echo "To monitor log growth in the future:"
echo "  ssh root@omv800 'docker exec -u 33 $CONTAINER_ID du -sh /var/www/html/data/*.log*'"
echo ""
echo "To manually rotate logs:"
echo "  ssh root@omv800 'docker exec -u 33 $CONTAINER_ID php /var/www/html/occ log:rotate'"
