#!/bin/bash

# QAUP Docker 镜像版本检查脚本
# 检查各个镜像的最新稳定版本

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

# 检查 Docker Hub API 获取最新版本
check_dockerhub_tags() {
    local repo=$1
    local filter=${2:-""}
    
    print_message $BLUE "检查 $repo 的版本..."
    
    # 使用 Docker Hub API 获取标签
    local api_url="https://registry.hub.docker.com/v2/repositories/$repo/tags?page_size=100"
    
    if command -v curl &> /dev/null; then
        local tags=$(curl -s "$api_url" | grep -o '"name":"[^"]*"' | cut -d'"' -f4 | head -20)
        echo "最新的一些版本标签:"
        echo "$tags" | head -10
    else
        print_message $YELLOW "curl 不可用，无法检查版本"
    fi
    echo ""
}

# 检查当前使用的版本
check_current_versions() {
    print_message $BLUE "当前 docker-compose.yml 中使用的版本:"
    echo ""
    
    if [ -f "../docker-compose.yml" ]; then
        echo "PostgreSQL/PostGIS:"
        grep -A 1 "qaup-postgres:" ../docker-compose.yml | grep "image:" | awk '{print "  " $2}'
        
        echo "Redis:"
        grep -A 1 "qaup-redis:" ../docker-compose.yml | grep "image:" | awk '{print "  " $2}'
        
        echo ""
    fi
    
    if [ -f "../Dockerfile" ]; then
        echo "Dockerfile 中的镜像:"
        grep "^FROM" ../Dockerfile | awk '{print "  " $2}'
        echo ""
    fi
}

print_message $GREEN "========================================="
print_message $GREEN "QAUP Docker 镜像版本检查"
print_message $GREEN "========================================="
echo ""

# 检查当前版本
check_current_versions

# 检查各个镜像的最新版本
print_message $BLUE "检查 Docker Hub 上的最新版本..."
echo ""

# PostgreSQL/PostGIS
check_dockerhub_tags "postgis/postgis"

# Redis
check_dockerhub_tags "library/redis"

# OpenJDK
check_dockerhub_tags "library/openjdk"

# Maven
check_dockerhub_tags "library/maven"

# Nginx
check_dockerhub_tags "library/nginx"

# Node.js
check_dockerhub_tags "library/node"

print_message $YELLOW "注意事项:"
echo "1. 选择版本时应优先考虑稳定性而非最新性"
echo "2. LTS (Long Term Support) 版本通常是生产环境的最佳选择"
echo "3. 对于 PostGIS，需要确保 PostgreSQL 版本兼容性"
echo "4. 建议在测试环境验证新版本后再用于生产"
echo ""

# 检查当前配置文件中的版本
if [ -f "image-versions.env" ]; then
    print_message $BLUE "当前配置文件中的版本:"
    source image-versions.env
    echo "PostgreSQL/PostGIS: $POSTGRES_IMAGE"
    echo "Redis: $REDIS_IMAGE"
    echo "OpenJDK: $OPENJDK_IMAGE"
    echo "Maven: $MAVEN_IMAGE"
    echo "Nginx: $NGINX_IMAGE"
    echo "Node.js: $NODE_IMAGE"
    echo ""
fi

print_message $GREEN "基于实际检查的推荐版本组合:"
echo "PostgreSQL/PostGIS: postgis/postgis:15-3.5-alpine (稳定) 或 postgis/postgis:16-3.5-alpine (更新)"
echo "Redis: redis:7.4.5-alpine (7.x系列最新稳定版) 或 redis:8.0.3-alpine (最新版本)"
echo "OpenJDK: openjdk:17-jre-slim (LTS推荐)"
echo "Maven: maven:3.9.11-openjdk-17-slim (最新稳定版)"
echo "Nginx: nginx:stable-alpine (稳定版本)"
echo "Node.js: node:lts-alpine (当前LTS版本)"
echo ""

print_message $YELLOW "更新建议:"
echo "1. 运行 './update-image-versions.sh --dry-run' 预览更改"
echo "2. 运行 './update-image-versions.sh --backup' 执行更新"
echo "3. 在测试环境验证新版本兼容性"
echo "4. 更新离线部署包"