#!/bin/bash

# Quick Nextcloud Upgrade Script
# Simple version update from 27.1.3 to 31.0.8
# Created: $(date)

set -e

echo "=== Quick Nextcloud Upgrade ==="
echo "Upgrading from 27.1.3 to 31.0.8"
echo ""

# Update the docker-compose file
echo "Updating docker-compose configuration..."
sed -i 's|image: nextcloud:27.1.3|image: nextcloud:31.0.8|g' stacks/apps/nextcloud.yml

echo "✅ Configuration updated"

# Deploy the updated service
echo "Deploying updated service..."
ssh root@omv800 "cd /home/jonathan/Coding/HomeAudit && docker stack deploy -c stacks/apps/nextcloud.yml nextcloud"

echo "Waiting for service to start..."
sleep 60

# Check if service is running
echo "Checking service status..."
if ssh root@omv800 "docker service ls | grep -q nextcloud_nextcloud"; then
    echo "✅ Service is running"
    
    # Wait a bit more for container to be ready
    sleep 30
    
    # Run upgrade
    echo "Running Nextcloud upgrade..."
    ssh root@omv800 "docker exec -u 33 \$(docker ps -q -f name=nextcloud) php /var/www/html/occ upgrade"
    
    echo "✅ Upgrade completed!"
    echo "Access at: https://nextcloud.pressmess.duckdns.org"
else
    echo "❌ Service failed to start"
    echo "Please check logs and rollback if needed"
    exit 1
fi
