#!/bin/bash

echo "========================================================="
echo "          MQTT SDK 全平台接口检查工具"
echo "   自动检查：Windows / Linux / ARM64  —  311 / 50 版本"
echo "========================================================="

VERSIONS=("v311" "v50")
IS_WINDOWS=0

if [[ "$(uname -s)" == MINGW* || "$(uname -s)" == MSYS* ]]; then
    IS_WINDOWS=1
fi

for ver in "${VERSIONS[@]}"; do
    echo -e "\n\033[1;34m==================== 版本：$ver ====================\033[0m"

    # ==================== Windows DLL
    if [ $IS_WINDOWS -eq 1 ]; then
        echo -e "\n📌 Windows 导出函数 ($ver):"
        dll="./sdk_shared/windows_${ver}/libmqtt_client_sdk_windows_${ver}.dll"
        if [ -f "$dll" ]; then
            nm -C "$dll" 2>/dev/null | grep -i 'mqtt_sdk' | grep -E '^[0-9a-fA-F]+.*T' | sort
        else
            echo "  - 未编译"
        fi

        echo -e "\n📌 VS2022 导出函数 ($ver):"
        vs_dll="./sdk_shared/vs2022_${ver}/libmqtt_client_sdk_vs2022_${ver}.dll"
        if [ -f "$vs_dll" ]; then
            nm -C "$vs_dll" 2>/dev/null | grep -i 'mqtt_sdk' | grep -E '^[0-9a-fA-F]+.*T' | sort
        else
            echo "  - 未编译"
        fi
    fi

    # ==================== Linux
    echo -e "\n📌 Linux 导出函数 ($ver):"
    lib="./sdk_shared/linux_${ver}/libmqtt_client_sdk_linux_${ver}.so"
    if [ -f "$lib" ]; then
        nm -D "$lib" 2>/dev/null | grep mqtt_sdk | sort
    else
        echo "  - 未编译"
    fi

    # ==================== ARM64
    echo -e "\n📌 ARM64 导出函数 ($ver):"
    lib="./sdk_shared/arm64_${ver}/libmqtt_client_sdk_arm64_${ver}.so"
    if [ -f "$lib" ]; then
        nm -D "$lib" 2>/dev/null | grep mqtt_sdk | sort
    else
        echo "  - 未编译"
    fi

    # ==================== STM32
    if [ $IS_WINDOWS -eq 0 ]; then
        echo -e "\n📌 STM32 导出函数 ($ver):"
        lib="./sdk_shared/stm32_${ver}/libmqtt_client_sdk_stm32_${ver}.a"
        if [ -f "$lib" ]; then
            arm-none-eabi-nm "$lib" 2>/dev/null | grep -E "T" | grep mqtt_sdk | sort | uniq
        else
            echo "  - 未编译"
        fi
    fi
done

echo -e "\n\033[1;32m✅ 检查完成！\033[0m"
