#!/bin/bash
################################################################################
# 53_config_console_check.sh — Console配置检测
# 功能: 检测 console=true 配置项未关闭问题（只检测不修复）
# 参考: check_server_health.sh (第1582-1708行)
# 日期: 2026-07-22
# 说明: 扫描配置文件中 console[:=]true 的行，发现则标记为异常
#       不执行备份和 sed 替换，由人工确认后修复
################################################################################

# 获取脚本所在目录并加载依赖
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB_DIR="${LIB_DIR:-/tmp/check_modules}"

# 加载配置文件和通用函数库
if [ -f "$LIB_DIR/lib/config.sh" ]; then
    source "$LIB_DIR/lib/config.sh"
else
    echo "ERROR: 配置文件不存在: $LIB_DIR/lib/config.sh"
    exit 1
fi

if [ -f "$LIB_DIR/lib/common.sh" ]; then
    source "$LIB_DIR/lib/common.sh"
else
    echo "ERROR: 通用函数库不存在: $LIB_DIR/lib/common.sh"
    exit 1
fi

# console 正则（不区分大小写，匹配 console=true 或 console: true）
CONSOLE_REGEX='console[[:space:]]*[:=][[:space:]]*true'

# ==================== 配置目录定义 ====================

# 新平台配置目录
NEW_PLATFORM_CONFIG_DIRS=(
    "/data/services/api/auth/auth-sso-auth/config"
    "/data/services/api/auth/auth-sso-gateway/config"
    "/data/services/api/auth/auth-sso-system/config"
    "/data/services/api/java-meeting/java-meeting2.0/config"
    "/data/services/api/java-meeting/java-meeting-extapi/config"
    "/data/services/api/java-meeting/java-message-scheduling/config"
    "/data/services/api/java-meeting/java-mqtt/config"
    "/data/services/api/java-meeting/java-quartz/config"
    "/data/services/api/python-cmdb"
    "/data/services/web/pc/pc-vue2-ai/static"
    "/data/services/web/mobile/mobile-vue2/static"
    "/data/middleware/nginx/config"
)

# 传统平台配置目录
OLD_PLATFORM_CONFIG_DIRS=(
    "/var/www/java/api-java-meeting2.0/config"
    "/var/www/java/external-meeting-api/config"
    "/var/www/java/ubains-web-2.0/static"
    "/var/www/java/ubains-web-admin/static"
    "/var/www/java/ubains-web-h5/static/h5"
    "/var/www/java/nginx-conf.d"
)

# ==================== 平台类型识别 ====================

detect_platform_type() {
    [ -d "/data/services" ] && echo "new" || echo "old"
}

# ==================== Console 配置扫描 ====================

scan_console_config() {
    local platform_type="$1"
    local -a config_dirs

    if [ "$platform_type" = "new" ]; then
        config_dirs=("${NEW_PLATFORM_CONFIG_DIRS[@]}")
    else
        config_dirs=("${OLD_PLATFORM_CONFIG_DIRS[@]}")
    fi

    local total_files=0
    local found_count=0
    local found_files=""

    for dir in "${config_dirs[@]}"; do
        if [ ! -d "$dir" ]; then
            continue
        fi

        while IFS= read -r file; do
            [ -z "$file" ] && continue
            [ ! -r "$file" ] && continue

            total_files=$((total_files + 1))

            if grep -qiE "$CONSOLE_REGEX" "$file" 2>/dev/null; then
                found_count=$((found_count + 1))
                # 转义文件路径中的特殊字符
                local escaped_file
                escaped_file=$(echo "$file" | sed 's/"/\\"/g')
                if [ -n "$found_files" ]; then
                    found_files="${found_files},"
                fi
                found_files="${found_files}\"${escaped_file}\""
            fi
        done < <(find "$dir" -type f \( -name "*.yml" -o -name "*.properties" -o -name "config.js" -o -name "config.json" \) 2>/dev/null)
    done

    # 输出结果
    if [ "$found_count" -eq 0 ]; then
        output_result "CFG_CONSOLE_STATUS" "OK"
        output_result "CFG_CONSOLE_STATUS_LEVEL" "正常"
    else
        output_result "CFG_CONSOLE_STATUS" "WARN"
        output_result "CFG_CONSOLE_STATUS_LEVEL" "警告"
    fi

    output_result "CFG_CONSOLE_TOTAL_FILES" "$total_files"
    output_result "CFG_CONSOLE_FOUND_COUNT" "$found_count"

    # JSON 数组输出
    if [ -n "$found_files" ]; then
        output_result "CFG_CONSOLE_FOUND_FILES" "[${found_files}]"
    else
        output_result "CFG_CONSOLE_FOUND_FILES" "[]"
    fi
}

# ==================== 主检测流程 ====================

main() {
    log_info "开始Console配置检测..."

    set +e

    local platform_type
    platform_type=$(detect_platform_type)
    scan_console_config "$platform_type"

    log_info "Console配置检测完成"
}

main
