#!/bin/bash

set -e  # 发生错误时终止脚本执行

echo "==========================="
echo "🚀 开始自动安装 Docker 和 Docker Compose"
echo "==========================="

# 检查 Docker 是否已安装
if command -v docker &> /dev/null; then
    echo "✅ Docker 已安装，跳过安装步骤"
else
    # 原有的 Docker 安装步骤
    echo "📦 安装依赖..."
    sudo apt update -y
    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release

    echo "🔑 添加 Docker GPG 密钥..."
    curl -fsSL https://mirrors.nwafu.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

    echo "🌍 添加 Docker 源..."
    sudo add-apt-repository -y "deb [arch=amd64] https://mirrors.nwafu.edu.cn/docker-ce/linux/ubuntu/ $(lsb_release -cs) stable"
    sudo apt-get update -y

    echo "🐳 安装 Docker..."
    sudo apt-get install -y docker.io

    echo "✅ 启动并设置 Docker 开机自启..."
    sudo systemctl enable docker
    sudo systemctl start docker
fi

# 检查 Docker Compose 是否已安装
if command -v docker-compose &> /dev/null; then
    echo "✅ Docker Compose 已安装，跳过安装步骤"
else
    echo "⚙️ 安装 Docker Compose..."
    sudo mv ./docker-compose-linux-x86_64 /usr/bin/docker-compose
    #设置权限
    chmod +x /usr/bin/docker-compose
fi

echo "📌 Docker 运行状态:"
sudo systemctl status docker --no-pager || true

echo "📌 Docker Compose 版本:"
docker-compose -v

echo "🎉 Docker 和 Docker Compose 已就绪！"

echo "🚀 启动EasyAI"
sudo docker-compose pull && sudo docker-compose up -d
echo "🎉EasyAI应用启动成功"

