#!/bin/bash

# Fix Jellyfin Duplication Script
# Removes the failing Docker Swarm service and keeps the working standalone container
# Created: $(date)

set -e

echo "=== Fixing Jellyfin Duplication Issue ==="
echo ""

echo "Current Jellyfin instances:"
echo "1. Docker Swarm service (failing): jellyfin_jellyfin"
echo "2. Standalone container (working): jellyfin"
echo ""

# Check Docker Swarm service status
echo "Checking Docker Swarm service status..."
SWARM_STATUS=$(ssh root@omv800 "docker service ls | grep jellyfin")
echo "Swarm service: $SWARM_STATUS"

# Check standalone container status
echo ""
echo "Checking standalone container status..."
STANDALONE_STATUS=$(ssh root@omv800 "docker ps | grep jellyfin | grep -v jellyfin_jellyfin")
echo "Standalone container: $STANDALONE_STATUS"

# Check for port conflicts
echo ""
echo "Checking port usage..."
PORT_USAGE=$(ssh root@omv800 "netstat -tlnp | grep :8096")
echo "Port 8096 usage: $PORT_USAGE"

# Check storage paths
echo ""
echo "Checking storage paths..."
echo "Docker Swarm service paths (from config):"
echo "  - /export/jellyfin:/config"
echo "  - /export/media:/media/movies:ro"
echo "  - /export/tv_shows:/media/tv:ro"
echo ""
echo "Standalone container paths:"
ssh root@omv800 "docker inspect jellyfin --format='{{range .Mounts}}{{.Type}} {{.Source}} -> {{.Destination}}{{.RW}}{{end}}' | tr 'true' '\n'"

# Decision and action
echo ""
echo "=== RECOMMENDED ACTION ==="
echo "The standalone Jellyfin container is working properly and has been running for 24+ hours."
echo "The Docker Swarm service is failing due to storage path conflicts and SQLite I/O errors."
echo ""
echo "Recommended solution: Remove the failing Docker Swarm service and keep the working standalone container."
echo ""

read -p "Do you want to remove the failing Docker Swarm service? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Operation cancelled. You can manually remove the service later with:"
    echo "  ssh root@omv800 'docker service rm jellyfin_jellyfin'"
    exit 0
fi

# Remove the failing Docker Swarm service
echo ""
echo "Removing failing Docker Swarm service..."
ssh root@omv800 "docker service rm jellyfin_jellyfin"

# Wait for service removal
echo "Waiting for service removal..."
sleep 10

# Verify service removal
echo ""
echo "Verifying service removal..."
if ssh root@omv800 "docker service ls | grep -q jellyfin"; then
    echo "❌ Service still exists"
else
    echo "✅ Docker Swarm service removed successfully"
fi

# Check that standalone container is still working
echo ""
echo "Verifying standalone container is still working..."
if ssh root@omv800 "docker ps | grep -q jellyfin"; then
    echo "✅ Standalone Jellyfin container is still running"
    
    # Check health status
    HEALTH=$(ssh root@omv800 "docker inspect jellyfin --format='{{.State.Health.Status}}'")
    echo "Container health: $HEALTH"
    
    # Check if it's accessible
    echo "Testing accessibility..."
    if ssh root@omv800 "curl -s -o /dev/null -w '%{http_code}' http://localhost:8096" | grep -q "200\|302"; then
        echo "✅ Jellyfin is accessible on port 8096"
    else
        echo "⚠️  Jellyfin may not be accessible on port 8096"
    fi
else
    echo "❌ Standalone container not found"
fi

# Update Caddy configuration if needed
echo ""
echo "=== CADDY CONFIGURATION ==="
echo "Note: The standalone Jellyfin container is not managed by Docker Swarm, so Caddy labels won't work."
echo "You may need to update your Caddy configuration to point directly to OMV800:8096"
echo ""
echo "Current Caddy routing: jellyfin.pressmess.duckdns.org → 192.168.50.229:8096 (OMV800)"
echo "This should continue to work as long as the standalone container is running."

echo ""
echo "=== CLEANUP COMPLETE ==="
echo "✅ Failing Docker Swarm service removed"
echo "✅ Working standalone container preserved"
echo "✅ Port conflict resolved"
echo ""
echo "To monitor Jellyfin status:"
echo "  ssh root@omv800 'docker ps | grep jellyfin'"
echo ""
echo "To view Jellyfin logs:"
echo "  ssh root@omv800 'docker logs jellyfin --tail 50'"
echo ""
echo "To restart Jellyfin if needed:"
echo "  ssh root@omv800 'docker restart jellyfin'"
