#!/bin/bash

# Jellyfin Docker Migrationif ! docker compose version &> /dev/null; then
    echo "❌ Docker Compose not found. Please install Docker Compose first."
    exit 1
fi
echo "✅ Docker and Docker Compose are available"pt
# This script helps migrate your bare metal Jellyfin installation to Docker

echo "=== Jellyfin Docker Migration Assistant ==="
echo

# Check if Jellyfin service is running
if systemctl is-active --quiet jellyfin; then
    echo "⚠️  Jellyfin service is currently running. Please stop it before migration:"
    echo "   sudo systemctl stop jellyfin"
    echo "   sudo systemctl disable jellyfin"
    echo
    exit 1
fi

# Check for NVIDIA drivers and container toolkit
echo "🔍 Checking NVIDIA GPU support..."
if command -v nvidia-smi &> /dev/null; then
    echo "✅ NVIDIA drivers detected"
    if command -v nvidia-container-runtime &> /dev/null; then
        echo "✅ NVIDIA Container Toolkit detected"
        echo "   You can uncomment the NVIDIA GPU sections in docker compose.yml"
    else
        echo "⚠️  NVIDIA Container Toolkit not found. Install it for GPU transcoding:"
        echo "   https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html"
    fi
else
    echo "ℹ️  NVIDIA drivers not detected. GPU transcoding will not be available."
fi
echo

# Check Docker and Docker Compose
echo "🔍 Checking Docker installation..."
if ! command -v docker &> /dev/null; then
    echo "❌ Docker not found. Please install Docker first."
    exit 1
fi

if ! command -v docker compose &> /dev/null && ! docker compose version &> /dev/null; then
    echo "❌ Docker Compose not found. Please install Docker Compose first."
    exit 1
fi
echo "✅ Docker and Docker Compose are available"
echo

# Migration steps
echo "📋 Migration Steps:"
echo "1. Copy your existing Jellyfin data to Docker volumes"
echo "2. Update media paths in docker compose.yml"
echo "3. Configure GPU transcoding (if applicable)"
echo "4. Start the container"
echo

# Offer to copy existing data
read -p "Do you want to copy existing Jellyfin data? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "🔄 Creating Docker volumes and copying data..."
    
    # Create the container to create volumes
    docker compose up --no-start
    
    # Copy configuration data
    if [ -d "/var/lib/jellyfin" ]; then
        echo "📁 Copying Jellyfin data directory..."
        sudo docker cp /var/lib/jellyfin/. jellyfin:/config/
    fi
    
    # Copy cache if it exists
    if [ -d "/var/cache/jellyfin" ]; then
        echo "📁 Copying Jellyfin cache directory..."
        sudo docker cp /var/cache/jellyfin/. jellyfin:/cache/
    fi
    
    echo "✅ Data migration completed"
fi

echo
echo "🎬 Next steps:"
echo "1. Edit docker compose.yml and update the media volume paths:"
echo "   - Replace '/path/to/your/movies' with your actual movie directory"
echo "   - Replace '/path/to/your/tv' with your actual TV shows directory"
echo "   - Replace '/path/to/your/music' with your actual music directory"
echo
echo "2. If you have NVIDIA GPU and drivers installed:"
echo "   - Uncomment the NVIDIA GPU sections in docker compose.yml"
echo
echo "3. Start Jellyfin:"
echo "   docker compose up -d"
echo
echo "4. Access Jellyfin at: http://localhost:8096"
echo
echo "5. Optional: Stop and disable the systemd service:"
echo "   sudo systemctl stop jellyfin"
echo "   sudo systemctl disable jellyfin"
