
#!/bin/bash

# ====================================================
# AstrBot + NapCatQQ 一键安装脚本 (国内优化版)
# 支持断点续传、自动镜像选择
# ====================================================

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

PROGRESS_FILE="/root/.bot_install_progress"
WORK_DIR="/root/mybot"

# 检查是否为 root
if [ "$EUID" -ne 0 ]; then 
  echo -e "${RED}请以 root 用户运行此脚本${NC}"
  exit 1
fi

# 获取当前进度
get_stage() {
    if [ -f "$PROGRESS_FILE" ]; then
        cat "$PROGRESS_FILE"
    else
        echo 0
    fi
}

# 设置进度
set_stage() {
    echo "$1" > "$PROGRESS_FILE"
}

echo -e "${GREEN}>>> 欢迎使用 AstrBot + NapCatQQ 一键部署脚本${NC}"

# --- STAGE 1: 基础工具安装 ---
if [ $(get_stage) -le 0 ]; then
    echo -e "${YELLOW}[1/5] 安装基础工具 (curl, wget, ca-certificates)...${NC}"
    apt-get update || yum update -y
    apt-get install -y curl wget ca-certificates || yum install -y curl wget ca-certificates
    set_stage 1
fi

# --- STAGE 2: Docker 安装 (使用阿里云镜像源) ---
if [ $(get_stage) -le 1 ]; then
    echo -e "${YELLOW}[2/5] 正在安装 Docker (使用国内镜像源)...${NC}"
    if ! command -v docker &> /dev/null; then
        curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    fi
    systemctl enable --now docker
    set_stage 2
fi

# --- STAGE 3: 配置 Docker 国内加速器 ---
if [ $(get_stage) -le 2 ]; then
    echo -e "${YELLOW}[3/5] 配置 Docker 镜像加速器...${NC}"
    # 这里列出目前国内较为稳定的镜像源
    cat <<EOF > /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://mirror.ccs.tencentyun.com",
    "https://hub.rat.dev",
    "https://docker.m.daocloud.io",
    "https://docker.1panel.live"
  ]
}
EOF
    systemctl restart docker
    set_stage 3
fi

# --- STAGE 4: 安装 Docker Compose ---
if [ $(get_stage) -le 3 ]; then
    echo -e "${YELLOW}[4/5] 安装 Docker Compose...${NC}"
    if ! command -v docker-compose &> /dev/null; then
        # 使用 GHProxy 代理加速下载
        curl -L "https://mirror.ghproxy.com/https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
        chmod +x /usr/local/bin/docker-compose
        ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
    fi
    set_stage 4
fi

# --- STAGE 5: 部署容器 ---
if [ $(get_stage) -le 4 ]; then
    echo -e "${YELLOW}[5/5] 正在准备配置文件并拉取镜像...${NC}"
    
    mkdir -p $WORK_DIR/napcat-config
    mkdir -p $WORK_DIR/astrbot-data
    cd $WORK_DIR

    read -p "请输入你的机器人QQ号: " BOT_QQ

    cat <<EOF > docker-compose.yml
version: '3.8'
services:
  napcat:
    image: mlikiowa/napcat-docker:latest
    container_name: napcat
    restart: always
    environment:
      - ACCOUNT=$BOT_QQ
    volumes:
      - ./napcat-config:/app/napcat/config
      - ./napcat-data:/app/.config/QQ
    ports:
      - "6099:6099"
      - "3001:3001"
    network_mode: bridge

  astrbot:
    image: ch360465323/astrbot:latest
    container_name: astrbot
    restart: always
    volumes:
      - ./astrbot-data:/AstrBot/data
    ports:
      - "6185:6185"
    depends_on:
      - napcat
    network_mode: bridge
EOF

    echo -e "${YELLOW}正在拉取镜像 (如果卡住请按 Ctrl+C 重试，脚本支持断点续传)...${NC}"
    docker-compose pull
    docker-compose up -d

    set_stage 5
fi

# --- 部署完成 ---
echo -e "${GREEN}==============================================${NC}"
echo -e "${GREEN}部署完成！${NC}"
echo -e "${YELLOW}1. NapCat WebUI (扫码登录): http://$(curl -s ifconfig.me):6099${NC}"
echo -e "${YELLOW}2. AstrBot 管理面板: http://$(curl -s ifconfig.me):6185${NC}"
echo -e "${YELLOW}   - 初始账号: admin  初始密码: astrbot${NC}"
echo -e "${GREEN}==============================================${NC}"
echo -e "提示: 如果无法访问，请检查云服务器安全组是否放行了 6099, 3001, 6185 端口。"

# 任务完成，删除进度文件
rm -f "$PROGRESS_FILE"