#!/bin/bash

# 设置颜色
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
RED='\033[0;31m'
NC='\033[0m' # 没有颜色

# 捕获Ctrl+C信号
trap 'echo -e "${RED}\n用户退出脚本。${NC}"; exit 1' SIGINT

# 检查命令存在函数
check_command() {
    if command -v docker-compose &> /dev/null; then
        COMPOSE_CMD="docker-compose"
        echo -e "${GREEN}using docker-compose${NC}"
    elif command -v docker &> /dev/null && docker compose version &> /dev/null; then
        COMPOSE_CMD="docker compose"
        echo -e "${GREEN}using docker compose${NC}"
    elif command -v podman-compose &> /dev/null && podman-compose version &> /dev/null; then
        COMPOSE_CMD="podman-compose"
        echo -e "${GREEN}using podman-compose${NC}"
    else
        echo -e "${RED}No compatible compose command found.${NC}"
        exit 1
    fi
}

# 遍历函数
find_compose_files() {
    compose_files=()
    while IFS= read -r -d '' file; do
        compose_files+=("$file")
    done < <(
        find . -maxdepth 3 -name '*ignore*' -prune -o \
            \( -name 'compose.yaml' -o -name 'compose.yml' -o \
            -name 'docker-compose.yaml' -o -name 'docker-compose.yml' \) -print0
    )

    echo -e "${YELLOW}发现 ${#compose_files[@]} compose 文件:${NC}"
    for file in "${compose_files[@]}"; do
        echo "$file"
    done
}

# 加载动画
loading_animation() {
  local pid=$1
  local spin='\|/-'
  local i=0

  while kill -0 $pid 2>/dev/null; do
    i=$(( (i+1) % 4 ))
    printf "\r${spin:$i:1} 拉取中..."
    sleep .1
  done

  printf "\r"
}

# 获取镜像ID的函数
get_image_id() {
    local file="$1"
    $COMPOSE_CMD -f "$file" images -q | sort | md5sum | awk '{print $1}'
}

update_compose_files() {
    for compose_file in "${compose_files[@]}"; do
        echo -e "${CYAN}正在更新 $compose_file...${NC}"
        
        before_pull=$(get_image_id "$compose_file")
        $COMPOSE_CMD -f "$compose_file" pull &> /dev/null &
        pid=$!
        loading_animation $pid
        wait $pid

        after_pull=$(get_image_id "$compose_file")

        if [ "$before_pull" == "$after_pull" ]; then
            echo -e "${YELLOW}没有新镜像，跳过重启。${NC}"
        else
            echo -e "${GREEN}新镜像已下载，重启服务...${NC}"
            $COMPOSE_CMD -f "$compose_file" down &> /dev/null
            $COMPOSE_CMD -f "$compose_file" up -d &> /dev/null
            echo -e "${GREEN}服务重启完成。${NC}"
        fi
    done
}



# 删除全部服务
pause_all_compose() {
    for compose_file in "${compose_files[@]}"; do
        echo -e "${CYAN}删除所有服务 $compose_file...${NC}"
        (
          cd "$(dirname "$compose_file")"
          $COMPOSE_CMD down &> /dev/null
          echo -e "${BLUE}服务已全部删除。${NC}"
        )
    done
}

# 启动全部服务
start_all_compose() {
    for compose_file in "${compose_files[@]}"; do
        echo -e "${CYAN}启动服务 $compose_file...${NC}"
        (
          cd "$(dirname "$compose_file")"
          $COMPOSE_CMD up -d &> /dev/null
          echo -e "${GREEN}服务已启动。${NC}"
        )
    done
}

# 主函数
main() {
    check_command
    find_compose_files

    echo -e "${YELLOW}请选择操作:${NC}"
    echo -e "${GREEN}1) down全部compose${NC}"
    echo -e "${GREEN}2) up全部compose${NC}"
    echo -e "${GREEN}3) update全部compose (默认)${NC}"
    read -r choice

    case $choice in
        1) pause_all_compose ;;
        2) start_all_compose ;;
        *) update_compose_files ;;
    esac
}

# 执行主函数
main

