#!/usr/bin/env bash

GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
BLUE="\e[34m"
RESET="\e[0m"



log() {
    local color=""
    case "$1" in
        GREEN|RED|YELLOW|BLUE) color="$1"; shift ;;
    esac

    local msg="[$(date '+%F %T')] $*"
    [[ -n "$color" ]] && msg="${!color}${msg}${RESET}"
    echo -e "$msg" | tee -a "$LOG_FILE"
}
log_not_output_file() {
    local color=""
    case "$1" in
        GREEN|RED|YELLOW|BLUE) color="$1"; shift ;;
    esac

    local msg="[$(date '+%F %T')] $*"
    [[ -n "$color" ]] && msg="${!color}${msg}${RESET}"
    echo -e "$msg"
}

SET_DIR() {
    TIMESTAMP=$(date '+%F_%H-%M-%S')
    fru_output=$(sudo ipmitool fru print 2>/dev/null)
    SERIAL=$(echo "$fru_output" | awk -F: '/Product Serial/ {gsub(/^[ \t]+/, "", $2); print $2}')
    SERIAL=$(echo "$SERIAL" | tr -cd '[:alnum:]_-')
    if [[ -n "$SERIAL" ]]; then
        LOG_DIR="$(pwd)/Debug_logs_${SERIAL}_${TIMESTAMP}"
    else
        LOG_DIR="$(pwd)/Debug_logs_${TIMESTAMP}"
    fi

    Verbose_data_LOG_DIR="$LOG_DIR/Verbose_data"
    mkdir -p "$LOG_DIR"
    mkdir -p "$Verbose_data_LOG_DIR"
    LOG_FILE="${LOG_DIR}/output.log"

    log $BLUE "Logging directory set to: $LOG_DIR"
}

GET_OS_TYPE() {
    OS_TYPE=""
    if [[ -f /etc/os-release ]]; then
        . /etc/os-release
        case "$ID" in
            ubuntu) OS_TYPE="ubuntu" ;;
            rocky) OS_TYPE="rocky" ;;
            rhel) OS_TYPE="rocky" ;;
            *) OS_TYPE="unknown" ;;
        esac
    fi
    log YELLOW "Detected OS: $OS_TYPE"
}


CHECK_DEPENDENCIES() {
    local MISSING=()
    local OS_TYPE
    OS_TYPE=$(grep -i '^ID=' /etc/os-release | cut -d'=' -f2 | tr -d '"')
    log BLUE "Checking system dependencies..."

    local DEPS=()

    if [[ "$OS_TYPE" == "ubuntu" ]]; then
        DEPS=(ipmitool jq curl)
    elif [[ "$OS_TYPE" == "rocky" ]]; then
        DEPS=(ipmitool curl jq)
    else
        log RED "Unsupported OS: $OS_TYPE"
        exit 1
    fi

    for pkg in "${DEPS[@]}"; do
        if ! command -v "$pkg" &>/dev/null; then
            log RED "[MISSING] $pkg not found."
            MISSING+=("$pkg")
        else
            log GREEN "[OK] $pkg found."
        fi
    done

    if [[ ${#MISSING[@]} -gt 0 ]]; then
        if [[ "$OS_TYPE" == "ubuntu" ]]; then
            local UBUNTU_VER
            UBUNTU_VER=$(grep -oP '(?<=^VERSION_ID=")[0-9]+' /etc/os-release)
            log YELLOW "Ubuntu detected, attempting offline installation..."

            for pkg in "${MISSING[@]}"; do
                PKG_DIR="package/${pkg}/ubuntu${UBUNTU_VER}"
                if [ -d "$PKG_DIR" ]; then
                    log YELLOW "Trying to install ${pkg} offline..."
                    sudo dpkg -i "${PKG_DIR}"/*.deb >/dev/null 2>&1 || true
                    log GREEN "[OK] ${pkg} has been installed."
                else
                    log RED "No offline package directory found for ${pkg} (expected at ${PKG_DIR})"
                fi
            done

        elif [[ "$OS_TYPE" == "rocky" ]]; then
            log YELLOW "Rocky Linux detected, attempting online installation..."
            sudo dnf install -y "${MISSING[@]}"
        fi
    fi

    local STILL_MISSING=()
    for pkg in "${MISSING[@]}"; do
        if ! command -v "$pkg" &>/dev/null; then
            STILL_MISSING+=("$pkg")
        fi
    done

    if [[ ${#STILL_MISSING[@]} -gt 0 ]]; then
        log YELLOW "Some packages are still missing after installation: ${STILL_MISSING[*]}"
        if [[ "$OS_TYPE" == "ubuntu" ]]; then
            log YELLOW "Please install manually with: sudo apt-get install -y ${STILL_MISSING[*]}"
            exit 1
        elif [[ "$OS_TYPE" == "rocky" ]]; then
            log YELLOW "Continuing anyway on Rocky Linux (non-fatal)."
        fi
    else
        log GREEN "[OK] All required dependencies are now installed."
    fi
}

detect_platform() {
    local target_ip="169.254.0.17:8080"
    local systems_json="${Verbose_data_LOG_DIR}/systems.json"
    local system_json="${Verbose_data_LOG_DIR}/system_detail.json"

    # Query Systems
    sudo curl -k -s --fail "http://${target_ip}/redfish/v1/Systems" -o "$systems_json"
    if [ $? -ne 0 ]; then
        echo "detect FAIL"
        return
    fi

    local system_id
    system_id=$(jq -r '.Members[0]."@odata.id"' "$systems_json")

    if [[ -z "$system_id" || "$system_id" == "null" ]]; then
        echo "detect FAIL"
        return
    fi

    # Query System detail
    sudo curl -k -s --fail "http://${target_ip}${system_id}" -o "$system_json"
    if [ $? -ne 0 ]; then
        echo "detect FAIL"
        return
    fi

    if jq -e '.Manufacturer | test("NVIDIA"; "i")' "$system_json" >/dev/null; then
        echo "NVIDIA"
    else
        echo "AMD"
    fi
}



find_usb_lan() {
    USB_IFACE=""
    for iface in /sys/class/net/*; do
        if [[ -e "$iface/device/driver" ]]; then
            driver=$(basename "$(readlink "$iface/device/driver" 2>/dev/null || true)")
            if [[ "$driver" == "cdc_ether" ]]; then
                USB_IFACE=$(basename "$iface")
                break
            fi
        fi
    done
    if [[ -z "$USB_IFACE" ]]; then
        log RED "[ERROR] No USB BMC interface found!"
        return 1
    fi
    log GREEN "Found USB interface: $USB_IFACE"
    sudo ip addr flush dev "$USB_IFACE"
    sudo ip addr add 169.254.0.20/16 dev "$USB_IFACE"
    sudo ip link set "$USB_IFACE" up
    log BLUE "USB BMC interface $USB_IFACE is set to 169.254.0.20"
}

ENABLE_HMC_FORWARDING() {
    local output_file="${Verbose_data_LOG_DIR}/HMC_forwarding.txt"
    log YELLOW "Enabling HMC Event Forwarding via BMC..."
    if ! command -v ipmitool &>/dev/null; then
        log RED "[ERROR] ipmitool is not installed! Cannot enable HMC forwarding."
        echo "ipmitool not installed" > "$output_file"

        return 1
    fi
    sudo ipmitool raw 0x3a 0xe3 0xf1 0x01 > "$output_file" 2>&1
    local ret=$?

    if [[ $ret -eq 0 ]]; then
        log GREEN "HMC forwarding enabled successfully."

    else
        log RED "[ERROR] Failed to enable HMC forwarding. (exit code: $ret)"

    fi
}

DISABLE_HMC_FORWARDING() {
    local output_file="${Verbose_data_LOG_DIR}/HMC_forwarding.txt"
    log YELLOW "Disable HMC Event Forwarding via BMC..."
    if ! command -v ipmitool &>/dev/null; then
        log RED "[ERROR] ipmitool is not installed! Cannot disable HMC forwarding."
        echo "ipmitool not installed" > "$output_file"

        return 1
    fi
    sudo ipmitool raw 0x3a 0xe3 0xf1 0x00 > "$output_file" 2>&1
    local ret=$?
    if [[ $ret -eq 0 ]]; then
        log GREEN "HMC forwarding disable successfully."

    else
        log RED "[ERROR] Failed to disable HMC forwarding. (exit code: $ret)"

    fi
}

GET_BIOS_VERSION() {
    local target_ip="169.254.0.17"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/BIOS.txt"
    local url_self="https://${target_ip}/redfish/v1/Systems/Self"
    local url_sys0="https://${target_ip}/redfish/v1/Systems/System_0"
    local url=""

    log YELLOW "Detecting correct BIOS inventory path..."
    if sudo curl -k -s --head "$url_self" | grep -qi "200 OK"; then
        log GREEN "Detected BMC redfish path (/redfish/v1/Systems/Self)"
        url="$url_self"
    elif sudo curl -k -s --head "$url_sys0" | grep -qi "200 OK"; then
        log GREEN "Detected BMC redfish path (/redfish/v1/Systems/System_0)"
        url="$url_sys0"
    else
        log RED "[ERROR] No valid BIOS inventory URL found!"
        echo "BIOS_Version: N/A" >> "$version_file"
        return
    fi

    log YELLOW "Querying BIOS version from $url ..."
    if sudo curl -k -s -X GET "$url" -o "$output_file"; then
        log GREEN "Response saved to $output_file"
    else
        log RED "[ERROR] curl failed."
        echo "BIOS_Version: N/A" >> "$version_file"
        return
    fi
    if command -v python3 &>/dev/null; then
        python3 -m json.tool "$output_file" > "${output_file}.tmp" && mv "${output_file}.tmp" "$output_file"
    fi
    local version=""
    [[ -x "$(command -v jq)" ]] && version=$(jq -r '.BiosVersion // empty' "$output_file")
    if [[ -z "$version" || "$version" == "null" ]]; then
        version=$(grep -o '"BiosVersion" *: *"[^"]*"' "$output_file" | head -n1 | cut -d '"' -f4)
    fi

    if [[ -n "$version" ]]; then
        echo "BIOS_Version: $version" >> "$version_file"
        log GREEN "BIOS Version: $version"
    else
        echo "BIOS_Version: N/A" >> "$version_file"
        log RED "[ERROR] Could not find 'BiosVersion' field."
    fi
}


GET_BMC_VERSION() {
    local target_ip="169.254.0.17"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/BMC.txt"
    local url_self="https://${target_ip}/redfish/v1/Managers/Self"
    local url_bmc0="https://${target_ip}/redfish/v1/Managers/BMC_0"
    local url=""

    log YELLOW "Detecting correct BMC inventory path..."
    if sudo curl -k -s --head "$url_self" | grep -qi "200 OK"; then
        log GREEN "Detected BMC redfish path (/redfish/v1/Managers/Self)"
        url="$url_self"
    elif sudo curl -k -s --head "$url_bmc0" | grep -qi "200 OK"; then
        log GREEN "Detected BMC redfish path (/redfish/v1/Managers/BMC_0)"
        url="$url_bmc0"
    else
        log RED "[ERROR] No valid BMC inventory URL found!"
        echo "BMC_Version: N/A" >> "$version_file"
        return
    fi

    log YELLOW "Querying BMC version from $url ..."

    if ! sudo curl -k -s "$url" -o "$output_file"; then
        log RED "[ERROR] curl failed."
        echo "BMC_Version: N/A" >> "$version_file"
        return
    fi
    if command -v python3 &>/dev/null; then
        python3 -m json.tool "$output_file" > "${output_file}.tmp" && mv "${output_file}.tmp" "$output_file"
    fi

    local version=""
    [[ -x "$(command -v jq)" ]] && version=$(jq -r '.FirmwareVersion // empty' "$output_file")

    if [[ -z "$version" || "$version" == "null" ]]; then
        version=$(grep -o '"FirmwareVersion" *: *"[^"]*"' "$output_file" | head -n1 | cut -d '"' -f4)
    fi

    if [[ -n "$version" ]]; then
        echo "BMC_Version: $version" >> "$version_file"
        log GREEN "BMC Version: $version"
    else
        echo "BMC_Version: N/A" >> "$version_file"
        log RED "[ERROR] Could not find FirmwareVersion field."
    fi
}



GET_MB_CPLD_VERSION() {
    local output_file="${Verbose_data_LOG_DIR}/MB_CPLD.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Querying MB CPLD firmware version via IPMI..."
    local raw_output
    if ! raw_output=$(sudo ipmitool raw 0x3a 0x87 0x08 2>/dev/null); then
        log RED "[ERROR] ipmitool command failed."
        echo "MB_CPLD_Version: N/A" >> "$version_file"
        return
    fi
    echo "$raw_output" > "$output_file"
    local major_hex minor_hex
    major_hex=$(echo "$raw_output" | awk '{print $1}')
    minor_hex=$(echo "$raw_output" | awk '{print $2}')
    local major_dec=$((16#$major_hex))
    local minor_dec=$((16#$minor_hex))
    printf -v minor_fmt "%02d" "$minor_dec"
    if [[ -n "$major_dec" && -n "$minor_fmt" ]]; then
        local version="${major_dec}.${minor_fmt}"
        echo "MB_CPLD_Version: $version" >> "$version_file"
        log GREEN "Extracted MB CPLD version: $version"
        log GREEN "MB CPLD version summary saved to $version_file"
    else
        echo "MB_CPLD_Version: N/A" >> "$version_file"
        log RED "[ERROR] Could not parse MB CPLD version."
        return
    fi
}


GET_BP_CPLD_VERSION() {
    local output_file="${Verbose_data_LOG_DIR}/BP_CPLD.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Querying BP CPLD firmware version via IPMI..."
    local raw_output
    if ! raw_output=$(sudo ipmitool raw 0x3a 0x87 0x11 2>/dev/null); then
        log RED "[ERROR] ipmitool command failed."
        echo "BP_CPLD_Version: N/A" >> "$version_file"
        return
    fi
    echo "$raw_output" > "$output_file"
    local major_hex minor_hex
    major_hex=$(echo "$raw_output" | awk '{print $1}')
    minor_hex=$(echo "$raw_output" | awk '{print $2}')
    local major_dec=$((16#$major_hex))
    local minor_dec=$((16#$minor_hex))
    printf -v minor_fmt "%02d" "$minor_dec"
    if [[ -n "$major_dec" && -n "$minor_fmt" ]]; then
        local version="${major_dec}.${minor_fmt}"
        echo "BP_CPLD_Version: $version" >> "$version_file"
        log GREEN "Extracted BP CPLD version: $version"
        log GREEN "BP CPLD version summary saved to $version_file"
    else
        echo "BP_CPLD_Version: N/A" >> "$version_file"
        log RED "[ERROR] Could not parse BP CPLD version."
        return
    fi
}


GET_BMC_SEL() {
    local output_file="${Verbose_data_LOG_DIR}/BMC_SEL.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get BMC SEL..."
    local raw_output
    if ! raw_output=$(sudo ipmitool sel elist 2>/dev/null); then
        log RED "[ERROR] ipmitool command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted BMC SEL."
    else
        log RED "[ERROR] Could not get BMC SEL."
        return
    fi
}

GET_LSPCI_VT() {
    local output_file="${Verbose_data_LOG_DIR}/lspci_vt.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get lspci -vt..."
    local raw_output
    if ! raw_output=$(sudo lspci -vt 2>/dev/null); then
        log RED "[ERROR] lspci -vt command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted lspci -vt data."
    else
        log RED "[ERROR] Could not get lspci -vt data."
        return
    fi
}

GET_IBDEV2NETDEV() {
    local output_file="${Verbose_data_LOG_DIR}/ibdev2netdev_v.txt"
    log YELLOW "Get ibdev2netdev -v..."

    if ! command -v ibdev2netdev &>/dev/null; then
        log RED "[ERROR] ibdev2netdev command not found."
        return
    fi

    local raw_output
    if ! raw_output=$(sudo ibdev2netdev -v 2>/dev/null); then
        log RED "[ERROR] ibdev2netdev -v command failed."
        return
    fi

    echo "$raw_output" > "$output_file"

    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted ibdev2netdev -v data."
    else
        log RED "[ERROR] ibdev2netdev -v output is empty."
    fi
}

GET_MST_STATUS() {
    local output_file="${Verbose_data_LOG_DIR}/mst_status_v.txt"
    log YELLOW "Get mst status -v..."

    if ! command -v mst &>/dev/null; then
        log RED "[ERROR] mst command not found."
        return
    fi

    local raw_output
    if ! raw_output=$(sudo mst status -v 2>/dev/null); then
        log RED "[ERROR] mst status -v command failed."
        return
    fi

    echo "$raw_output" > "$output_file"

    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted mst status -v data."
    else
        log RED "[ERROR] mst status -v output is empty."
    fi
}

GET_IBSTAT() {
    local output_file="${Verbose_data_LOG_DIR}/ibstat_v.txt"
    log YELLOW "Get ibstat -v..."

    if ! command -v ibstat &>/dev/null; then
        log RED "[ERROR] ibstat command not found."
        return
    fi

    local raw_output
    if ! raw_output=$(sudo ibstat -v 2>/dev/null); then
        log RED "[ERROR] ibstat -v command failed."
        return
    fi

    echo "$raw_output" > "$output_file"

    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted ibstat -v data."
    else
        log RED "[ERROR] ibstat -v output is empty."
    fi
}


GET_DMESG() {
    local output_file="${Verbose_data_LOG_DIR}/dmesg.txt"
    log YELLOW "Dump dmesg..."

    local raw_output
    if ! raw_output=$(sudo dmesg 2>/dev/null); then
        log RED "[ERROR] dmesg command failed."
        return
    fi

    echo "$raw_output" > "$output_file"

    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted dmesg data."
    else
        log RED "[ERROR] dmesg output is empty."
        return
    fi
}

GET_FRU() {
    local output_file="${Verbose_data_LOG_DIR}/FRU.txt"
    log YELLOW "Dumping IPMI FRU information..."

    local raw_output
    raw_output=$(sudo ipmitool fru print 2>/dev/null)

    if [[ -z "$raw_output" ]]; then
        log RED "[ERROR] ipmitool fru print returned empty output."
        return 1
    fi

    echo "$raw_output" > "$output_file"
    log GREEN "FRU information dumped successfully."
}




GET_LSPCI_VVV() {
    local output_file="${Verbose_data_LOG_DIR}/lspci_vvv.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get lspci -vvv..."
    local raw_output
    if ! raw_output=$(sudo lspci -vvv 2>/dev/null); then
        log RED "[ERROR] lspci -vvv command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted lspci -vvv data."
    else
        log RED "[ERROR] Could not get lspci -vvv data."
        return
    fi
}

GET_LSPCI_n_d_10de_vvv_grep_width() {
    local output_file="${Verbose_data_LOG_DIR}/lspci_n_d_10de_vvv_grep_width.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get lspci -n -d 10de: -vvv | grep -i width..."
    local raw_output
    if ! raw_output=$(sudo lspci -n -d 10de: -vvv | grep -i width 2>/dev/null); then
        log RED "[ERROR] lspci -n -d 10de: -vvv | grep -i width command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted lspci -n -d 10de: -vvv | grep -i width data."
    else
        log RED "[ERROR] Could not get lspci -n -d 10de: -vvv | grep -i width data."
        return
    fi
}

GET_LSPCI_n_d_75a0_vvv_grep_width() {
    local output_file="${Verbose_data_LOG_DIR}/lspci_n_d_75a0_vvv_grep_width.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get lspci -n -d 75a0: -vvv | grep -i width..."
    local raw_output
    if ! raw_output=$(sudo lspci -n -d :75a0 -vvv | grep -i width 2>/dev/null); then
        log RED "[ERROR] lspci -n -d :75a0 -vvv | grep -i width command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted lspci -n -d :75a0 -vvv | grep -i width data."
    else
        log RED "[ERROR] Could not get lspci -n -d :75a0 -vvv | grep -i width data."
        return
    fi
}

GET_LSPCI_n_d_75a3_vvv_grep_width() {
    local output_file="${Verbose_data_LOG_DIR}/lspci_n_d_75a3_vvv_grep_width.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get lspci -n -d :75a3 -vvv | grep -i width..."
    local raw_output
    if ! raw_output=$(sudo lspci -n -d :75a3 -vvv | grep -i width 2>/dev/null); then
        log RED "[ERROR] lspci -n -d :75a3 -vvv | grep -i width command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted lspci -n -d :75a3 -vvv | grep -i width data."
    else
        log RED "[ERROR] Could not get lspci -n -d :75a3 -vvv | grep -i width data."
        return
    fi
}

GET_LSPCI_n_d_15b3_vvv_grep_width() {
    local output_file="${Verbose_data_LOG_DIR}/lspci_n_d_15b3_vvv_grep_width.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get lspci -n -d 15b3: -vvv | grep -i width..."
    local raw_output
    if ! raw_output=$(sudo lspci -n -d 15b3: -vvv | grep -i width 2>/dev/null); then
        log RED "[ERROR] lspci -n -d 15b3: -vvv | grep -i width command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted lspci -n -d 15b3: -vvv | grep -i width data."
    else
        log RED "[ERROR] Could not get lspci -n -d 15b3: -vvv | grep -i width data."
        return
    fi
}

GET_LSPCI_n_d_1500_vvv_grep_width() {
    local output_file="${Verbose_data_LOG_DIR}/lspci_n_d_1500_vvv_grep_width.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get lspci -n -d 1500: -vvv | grep -i width..."
    local raw_output
    if ! raw_output=$(sudo lspci -n -d 1500: -vvv | grep -i width 2>/dev/null); then
        log RED "[ERROR] lspci -n -d 1500: -vvv | grep -i width command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted lspci -n -d 1500: -vvv | grep -i width data."
    else
        log RED "[ERROR] Could not get lspci -n -d 1500: -vvv | grep -i width data."
        return
    fi
}

GET_LSPCI_n_d_1000_vvv_grep_a_4_200_b_23() {
    local output_file="${Verbose_data_LOG_DIR}/lspci_n_d_1000_vvv_grep_a_4_200_b_23.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    log YELLOW "Get lspci -n -d 1000: -vvv | grep -A 4 "#200" -B 23..."
    local raw_output
    if ! raw_output=$(sudo lspci -n -d 1000: -vvv | grep -A 4 "#200" -B 23 2>/dev/null); then
        log RED "[ERROR] lspci -n -d 1000: -vvv | grep -A 4 "#200" -B 23 command failed."
        return
    fi
    echo "$raw_output" > "$output_file"
    if [[ -n "$raw_output" ]]; then
        log GREEN "Extracted lspci -n -d 1000: -vvv | grep -A 4 "#200" -B 23 data."
    else
        log RED "[ERROR] Could not get lspci -n -d 1000: -vvv | grep -A 4 "#200" -B 23 data."
        return
    fi
}

GET_PLX_FW_DETAIL_NEW() {
    local diag_tool="./g4Xdiagnostics.x86_64"
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/PLX_DETAIL_FW_data.txt"

    log YELLOW "Running $diag_tool --list to get PLX firmware details..."

    $diag_tool --list > "$output_file" 2>&1

    if [[ ! -s "$output_file" ]]; then
        log RED "[ERROR] Output file is empty"
        echo "PLX_FW_Version: N/A" >> "$version_file"
        return 1
    fi

    if ! grep -q "PEX" "$output_file"; then
        log RED "[ERROR] No PEX device found"
        echo "PLX_FW_Version: N/A" >> "$version_file"
        return 1
    fi

    log GREEN "Saved PLX list output to $output_file"

    grep -E "PEX[0-9]+" "$output_file" | while read -r line; do

        fw_version=$(echo "$line" | awk '{print $5}')
        pci_addr=$(echo "$line" | awk '{print $6}')
        PLX_INDEX=$(echo "$line" | awk '{print $1}')
        PLX_INDEX_VALUE=${PLX_INDEX%)}

        #echo "PLX_FW_${pci_addr}: ${fw_version}" >> "$version_file"
        log GREEN "Extracted PLX_FW_${pci_addr}: ${fw_version}"

        log YELLOW "Running $diag_tool -i $PLX_INDEX_VALUE cli pciehostview to get PLX details..."
        cli_output="${Verbose_data_LOG_DIR}/PLX_cli_pciehostview_${pci_addr}.txt"
        $diag_tool -i "$PLX_INDEX_VALUE" cli pciehostview > "$cli_output" 2>&1
        log GREEN "Saved pciehostview to $cli_output"

        mem_output="${Verbose_data_LOG_DIR}/PLX_mem_rd_${pci_addr}.txt"
        $diag_tool -i "$PLX_INDEX_VALUE" mem -rd 0x6080020c > "$mem_output" 2>&1
        log GREEN "Saved mem read to $mem_output"
        mem_value=$(awk -F': ' '/6080020c:/ {print $2}' "$mem_output" | awk '{print $1}')
        if [[ -n "$mem_value" ]]; then
            echo "PLX_FW_${pci_addr}: ${mem_value}" >> "$version_file"
        else
            echo "PLX_FW_${pci_addr}: N/A" >> "$version_file"
        fi
        
    done
    #log YELLOW "Running $diag_tool -i 1 cli pciehostview to get PLX details..."
    #cli_output="${Verbose_data_LOG_DIR}/PLX_cli_pciehostview.txt"
    #$diag_tool -i 1 cli pciehostview > "$cli_output" 2>&1
    #log GREEN "Saved pciehostview to $cli_output"

    log YELLOW "Running OTC for index 1..."
    $diag_tool -i 1 otc > /dev/null 2>&1

    src_dir="./scrutiny-logs"
    dst_dir="${LOG_DIR}/Verbose_data/scrutiny-logs"

    if [[ -d "$src_dir" ]]; then

        if [[ -d "$dst_dir" ]]; then
            rm -rf "$dst_dir"
        fi

        mv "$src_dir" "$dst_dir"

        log GREEN "Moved scrutiny-logs to $dst_dir"

    else
        log RED "[WARN] scrutiny-logs not generated for index 1"
    fi
    
}



archive() {
    archive_file="${LOG_DIR}.tar.gz"
    if tar -czf "$archive_file" -C "$(dirname "$LOG_DIR")" "$(basename "$LOG_DIR")"; then
        log_not_output_file GREEN "Log directory compressed to $archive_file"
        rm -rf "$LOG_DIR"
    else
        log_not_output_file RED "[ERROR] Failed to compress log directory $LOG_DIR"
    fi

}

GET_CUDA_VERSION() {
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/CUDA.txt"
    local nvcc_path="/usr/local/cuda/bin/nvcc"
    local cuda_version=""
    log YELLOW "Getting CUDA version from host OS..."
    if [[ -x "$nvcc_path" ]]; then
        "$nvcc_path" --version > "$output_file"
        log GREEN "CUDA version output saved to $output_file"
        cuda_version=$(grep -o 'V[0-9]\+\.[0-9]\+\(\.[0-9]\+\)*' "$output_file" | head -n1)
        log GREEN "Detected CUDA version: $cuda_version"
    else
        log RED "[WARN] nvcc not found at $nvcc_path. CUDA may not be installed."
        echo "nvcc not found" > "$output_file"
        cuda_version="N/A"
    fi
    echo "CUDA_Version: $cuda_version" >> "$version_file"
}


GET_GPU_DRIVER_VERSION() {
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/GPU_driver.txt"
    local driver_version=""
    log YELLOW "Getting GPU driver version from host OS..."
    if command -v nvidia-smi &>/dev/null; then
        nvidia-smi > "$output_file"
        log GREEN "GPU driver output saved to $output_file"
        driver_version=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader | head -n1)
        log GREEN "Detected NVIDIA GPU driver version: $driver_version"
    else
        log RED "[WARN] nvidia-smi not found. NVIDIA driver may not be installed."
        echo "nvidia-smi not found" > "$output_file"
        driver_version="N/A"
    fi
    echo "GPU_Driver_Version: $driver_version" >> "$version_file"
}

GET_MFT_VERSION() {
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/MFT.txt"
    local mft_version=""
    log YELLOW "Getting MFT version from host OS..."
    if command -v mst &>/dev/null; then
        sudo mst version > "$output_file" 2>&1
        log GREEN "MFT version output saved to $output_file"
        mft_version=$(grep -oE 'mft[[:space:]]+[0-9]+\.[0-9]+\.[0-9]+-[0-9]+' "$output_file" | awk '{print $2}' | head -n1)

        if [[ -n "$mft_version" ]]; then
            log GREEN "Detected MFT version: $mft_version"
        else
            log RED "[WARN] Unable to parse MFT version."
            mft_version="N/A"
        fi
    else
        log RED "[WARN] mst command not found. MFT may not be installed."
        echo "mst command not found" > "$output_file"
        mft_version="N/A"
    fi
    echo "MFT_Version: $mft_version" >> "$version_file"
}




GET_FM_VERSION() {
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/FabricManager.txt"
    local fm_version=""
    log YELLOW "Getting NVIDIA Fabric Manager version..."
    if command -v nv-fabricmanager &>/dev/null; then
        sudo nv-fabricmanager --version > "$output_file" 2>&1
        log GREEN "Fabric Manager version output saved to $output_file"
        fm_version=$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' "$output_file" | head -n1)
    else
        log RED "[WARN] nv-fabricmanager command not found. Fabric Manager may not be installed."
        echo "nvidai-fabricmanager not found" > "$output_file"
        fm_version="N/A"
    fi
    if [[ -n "$fm_version" ]]; then
        log GREEN "Detected Fabric Manager version: $fm_version"
    else
        log RED "[WARN] Fabric Manager version not detected."
        fm_version="N/A"
    fi
    echo "nvidai-fabricmanager: $fm_version" >> "$version_file"
}

GET_DPKG_LIST() {
    local dpkg_list_file="${Verbose_data_LOG_DIR}/dpkg_list.txt"

    if [[ ! -f "$dpkg_list_file" ]]; then
        log YELLOW "dpkg_list.txt not found. Generating..."

        if [[ "$OS_TYPE" == "ubuntu" ]]; then
            dpkg -l > "$dpkg_list_file" 2>/dev/null
            log GREEN "Saved dpkg list to $dpkg_list_file"
        elif [[ "$OS_TYPE" == "rocky" ]]; then
            rpm -qa | grep -i 'datacenter-gpu-manager-4' > "$dpkg_list_file" 2>/dev/null
            log GREEN "Saved rpm list to $dpkg_list_file"
        else
            log RED "[WARN] Unsupported OS_TYPE: $OS_TYPE"
        fi
    else
        log YELLOW "dpkg_list.txt already exists. Skip generating."
    fi
}



GET_DCGMI_VERSION() {
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/DCGMI.txt"
    local dpkg_list_file="${Verbose_data_LOG_DIR}/dpkg_list.txt"
    local dcgmi_version=""
    local cuda_target=""
    
    log YELLOW "Getting DCGM (dcgmi) version..."

    if command -v dcgmi &>/dev/null; then
        sudo dcgmi --version > "$output_file" 2>&1
        log GREEN "DCGMI version output saved to $output_file"
        dcgmi_version=$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' "$output_file" | head -n1)

        # 根據 OS 抓 DCGM 套件對應的 CUDA 版本
        if [[ "$OS_TYPE" == "ubuntu" ]]; then
            GET_DPKG_LIST
            if grep -q 'datacenter-gpu-manager-4-cuda13' "$dpkg_list_file"; then
                cuda_target="CUDA13"
            elif grep -q 'datacenter-gpu-manager-4-cuda12' "$dpkg_list_file"; then
                cuda_target="CUDA12"
            else
                cuda_target="Unknown_CUDA"
            fi
        elif [[ "$OS_TYPE" == "rocky" ]]; then
            GET_DPKG_LIST
            if grep -q 'datacenter-gpu-manager-4-cuda13' "$dpkg_list_file"; then
                cuda_target="CUDA13"
            elif grep -q 'datacenter-gpu-manager-4-cuda12' "$dpkg_list_file"; then
                cuda_target="CUDA12"
            else
                cuda_target="Unknown_CUDA"
            fi
        else
            cuda_target="N/A"
        fi

    else
        log RED "[WARN] dcgmi command not found. DCGM may not be installed."
        echo "dcgmi not found" > "$output_file"
        dcgmi_version="N/A"
        cuda_target="N/A"
    fi

    if [[ -n "$dcgmi_version" ]]; then
        log GREEN "Detected DCGM version: $dcgmi_version ($cuda_target)"
    else
        log RED "[WARN] DCGM version not detected."
        dcgmi_version="N/A"
    fi

    echo "DCGM_Version: $dcgmi_version" >> "$version_file"
    echo "DCGM_Target_CUDA: $cuda_target" >> "$version_file"
}


GET_OS_VERSION() {
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/OS_version.txt"
    local os_full=""
    log YELLOW "Getting host OS version..."
    if command -v lsb_release &>/dev/null; then
        lsb_release -a &> "$output_file"
        os_full="$(lsb_release -ds)"
        log GREEN "OS version output saved to $output_file"
    elif [[ -f /etc/os-release ]]; then
        cat /etc/os-release > "$output_file"
        local os_name
        local os_version
        os_name=$(grep '^NAME=' /etc/os-release | cut -d'=' -f2 | tr -d '"')
        os_version=$(grep '^VERSION=' /etc/os-release | cut -d'=' -f2 | tr -d '"')
        os_full="${os_name} ${os_version}"
        log GREEN "OS version output saved to $output_file"
    else
        log RED "[WARN] Cannot determine OS version."
        echo "OS version not found" > "$output_file"
        os_full="N/A"
    fi
    echo "OS_Version: $os_full" >> "$version_file"
}

GET_ALL_FW_VERSIONS_NVIDIA() {
    local target_ip="169.254.0.17:8080"
    local base_url="http://${target_ip}/redfish/v1/UpdateService/FirmwareInventory"
    local inventory_list="${Verbose_data_LOG_DIR}/HGX_FirmwareInventory_list.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"

    log YELLOW "Querying full HGX firmware inventory list from ${base_url} ..."
    if ! sudo curl -k -s -X GET "$base_url" -o "$inventory_list"; then
        log RED "[ERROR] Failed to query ${base_url}"
        return 1
    fi

    python3 -m json.tool "$inventory_list" > "${inventory_list}.tmp" && mv "${inventory_list}.tmp" "$inventory_list"
    local urls=()
    if command -v jq &>/dev/null; then
        mapfile -t urls < <(jq -r '.Members[]."@odata.id"' "$inventory_list")
    else
        mapfile -t urls < <(grep -o '"@odata.id" *: *"[^"]*"' "$inventory_list" | cut -d '"' -f4)
    fi

    if [[ ${#urls[@]} -eq 0 ]]; then
        log RED "[ERROR] No firmware inventory entries found!"
        return 1
    fi

    log GREEN "Found ${#urls[@]} firmware inventory entries."

    for url in "${urls[@]}"; do
        local id=$(basename "$url")
        local output_file="${Verbose_data_LOG_DIR}/${id}.txt"

        log YELLOW "Querying firmware version for ${id} ..."
        if ! sudo curl -k -s -X GET "http://${target_ip}${url}" -o "$output_file"; then
            log RED "[ERROR] curl failed for ${id}."
            echo "${id}_FW_Version: N/A" >> "$version_file"
            continue
        fi

        python3 -m json.tool "$output_file" > "${output_file}.tmp" && mv "${output_file}.tmp" "$output_file"
        local version=""
        if command -v jq &>/dev/null; then
            version=$(jq -r '.Version' "$output_file" 2>/dev/null)
        fi
        if [[ -z "$version" || "$version" == "null" ]]; then
            version=$(grep -o '"Version" *: *"[^"]*"' "$output_file" | head -n1 | cut -d '"' -f4)
        fi

        if [[ -n "$version" ]]; then
            echo "${id}_FW_Version: $version" >> "$version_file"
            log GREEN "Extracted ${id}_FW_Version: $version"
        else
            echo "${id}_FW_Version: N/A" >> "$version_file"
            log RED "[ERROR] Version not found for ${id}."
        fi
    done

    log GREEN "All firmware version summaries saved to $version_file"
}

GET_ALL_FW_VERSIONS_AMD() {
    local target_ip="169.254.0.17:8080"
    local base_url="http://${target_ip}/redfish/v1/UpdateService/FirmwareInventory"
    local inventory_list="${Verbose_data_LOG_DIR}/AMD_FirmwareInventory_list.txt"
    local version_file="${LOG_DIR}/FW_version_summary.txt"

    log YELLOW "Querying full AMD firmware inventory list from ${base_url} ..."
    if ! sudo curl -k -s -X GET "$base_url" -o "$inventory_list"; then
        log RED "[ERROR] Failed to query ${base_url}"
        return 1
    fi

    python3 -m json.tool "$inventory_list" > "${inventory_list}.tmp" && mv "${inventory_list}.tmp" "$inventory_list"

    local urls=()
    if command -v jq &>/dev/null; then
        mapfile -t urls < <(jq -r '.Members[]."@odata.id"' "$inventory_list")
    else
        mapfile -t urls < <(grep -o '"@odata.id" *: *"[^"]*"' "$inventory_list" | cut -d '"' -f4)
    fi

    if [[ ${#urls[@]} -eq 0 ]]; then
        log RED "[ERROR] No firmware inventory entries found!"
        return 1
    fi

    log GREEN "Found ${#urls[@]} firmware inventory entries."

    for url in "${urls[@]}"; do
        local id=$(basename "$url")
        local output_file="${Verbose_data_LOG_DIR}/${id}.txt"

        log YELLOW "Querying firmware version for ${id} ..."
        if ! sudo curl -k -s -X GET "http://${target_ip}${url}" -o "$output_file"; then
            log RED "[ERROR] curl failed for ${id}."
            echo "${id}_FW_Version: N/A" >> "$version_file"
            continue
        fi

        python3 -m json.tool "$output_file" > "${output_file}.tmp" && mv "${output_file}.tmp" "$output_file"

        local version=""

        if [[ "$id" == "vr_bundle_active" ]]; then
            # 取出 Oem.VRVersion 底下所有 key 的 value
            if command -v jq &>/dev/null; then
                version=$(jq -r '.Oem.VRVersion | to_entries[] | "\(.key)=\(.value)"' "$output_file" 2>/dev/null | tr '\n' ',' | sed 's/,$//')
            else
                version=$(grep -o '"Oem":{[^}]*}' "$output_file" | grep -o '"VRVersion":{[^}]*}' | tr -d '"{}' | tr ',' ';')
            fi
        else
            # 其他 AMD 版本
            if command -v jq &>/dev/null; then
                version=$(jq -r '.Oem.VersionID.ComponentDetails' "$output_file" 2>/dev/null)
            else
                version=$(grep -o '"ComponentDetails" *: *"[^"]*"' "$output_file" | head -n1 | cut -d '"' -f4)
            fi
        fi

        if [[ -n "$version" && "$version" != "null" ]]; then
            echo "${id}_FW_Version: $version" >> "$version_file"
            log GREEN "Extracted ${id}_FW_Version: $version"
        else
            echo "${id}_FW_Version: N/A" >> "$version_file"
            log RED "[ERROR] Version not found for ${id}."
        fi
    done

    log GREEN "All firmware version summaries saved to $version_file"
}





GET_DOCA_ALL_VERSION() {
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local DOCA_ALL_output_file="${Verbose_data_LOG_DIR}/DOCA_info.txt"
    local DOCA_SDK_output_file="${Verbose_data_LOG_DIR}/DOCA_SDK.txt"
    local dpkg_list_file="${Verbose_data_LOG_DIR}/dpkg_list.txt"
    local doca_version=""
    local doca_sdk_version=""
    GET_DPKG_LIST
    log YELLOW "Getting DOCA version from host OS..."

    if [[ "$OS_TYPE" == "ubuntu" ]]; then
        if [[ -f "/opt/mellanox/doca/VERSION" ]]; then
            doca_sdk_version=$(cat /opt/mellanox/doca/VERSION) > "$DOCA_SDK_output_file"
            doca_version=$(awk '/doca-all/ {print $3}' "$dpkg_list_file")
            #doca_version=$(grep -i 'DOCA' "$output_file" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n1)
            #doca_sdk_version=$(grep -i 'SDK' "$output_file" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n1)
            log GREEN "DOCA VERSION file found at /opt/mellanox/doca/VERSION"
        fi

        echo "DOCA_Version: $doca_version" >> "$version_file"
        echo "DOCA_SDK_Version: $doca_sdk_version" >> "$version_file"

    elif [[ "$OS_TYPE" == "rocky" ]] || [[ "$OS_TYPE" == "rhel" ]]; then
        # Rocky/RHEL: 只抓完整 DOCA 版本，不列子套件
        /opt/mellanox/doca/tools/doca-info > "$output_file" 2>/dev/null
        doca_version=$(grep -i 'doca-ofed-' "$output_file" | head -n1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+-[0-9]+')
        echo "DOCA_Version: $doca_version" >> "$version_file"

    else
        echo "Unknown OS. Cannot detect DOCA version" > "$output_file"
        echo "DOCA_Version: N/A" >> "$version_file"
        log RED "[WARN] DOCA version not found."
    fi

    log GREEN "DOCA version information saved to $version_file"
}

GET_DOCA_SDK_VERSION() {
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    local output_file="${Verbose_data_LOG_DIR}/DOCA_SDK.txt"
    local doca_sdk_version=""
    
    log YELLOW "Getting DOCA SDK version from host OS..."

    if [[ -f "/opt/mellanox/doca/VERSION" ]]; then
        grep -i 'SDK' /opt/mellanox/doca/VERSION > "$output_file" 2>/dev/null || cat /opt/mellanox/doca/VERSION > "$output_file"
        doca_sdk_version=$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' "$output_file" | head -n1)
        log GREEN "DOCA SDK version file found at /opt/mellanox/doca/VERSION"
    fi

    if [[ -z "$doca_sdk_version" ]]; then
        if [[ "$OS_TYPE" == "ubuntu" ]]; then
            dpkg -l 2>/dev/null | grep -i '^ii.*doca-sdk' > "$output_file"
            doca_sdk_version=$(awk '{print $3}' "$output_file" \
                | grep -oE '[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?' \
                | sort -V | tail -n1)
        elif [[ "$OS_TYPE" == "rocky" ]]; then
            rpm -qa | grep -i 'doca-sdk' > "$output_file"
            doca_sdk_version=$(echo "$output_file" | awk -F'-' '{print $3}' | sort -V | tail -n1)
        else
            echo "Unknown OS. Cannot detect DOCA SDK version" > "$output_file"
            doca_sdk_version="N/A"
        fi
    fi

    if [[ -n "$doca_sdk_version" ]]; then
        log GREEN "Detected DOCA SDK version: $doca_sdk_version"
    else
        log RED "[WARN] DOCA SDK version not found."
        echo "DOCA SDK version not found" > "$output_file"
        doca_sdk_version="N/A"
    fi

    echo "DOCA_SDK_Version: $doca_sdk_version" >> "$version_file"
}


main() {
    SET_DIR
    GET_OS_TYPE
    local version_file="${LOG_DIR}/FW_version_summary.txt"
    chmod -R 777 *
    CHECK_DEPENDENCIES
    log YELLOW "Starting system check..."
    find_usb_lan || { log RED "USB LAN setup failed, exiting."; exit 1; }
    ENABLE_HMC_FORWARDING || { log RED "Enable HMC forwarding failed, exiting."; exit 1; }
    sleep 2
    PLATFORM=$(detect_platform)
    log GREEN "Baseboard platform $PLATFORM"
    if [[ "$PLATFORM" == "NVIDIA" ]]; then
      echo "--------HGX Baseboard related FW version--------" >> "$version_file"
      GET_ALL_FW_VERSIONS_NVIDIA

    elif [[ "$PLATFORM" == "AMD" ]]; then
        echo "--------AMD Baseboard related FW version--------" >> "$version_file"
        GET_ALL_FW_VERSIONS_AMD
    
    else
        echo "--------Baseboard related FW version--------" >> "$version_file"
        echo "Redfish connection failed, cannot detect platform." >> "$version_file"
    fi
    echo "------------------------------------------------" >> "$version_file"
    echo "" >> "$version_file"
    echo "---------System & MB related FW version---------" >> "$version_file"
    GET_PLX_FW_DETAIL_NEW
    GET_BIOS_VERSION || log RED "Get BIOS version failed."
    GET_BMC_VERSION || log RED "Get BMC version failed."
    GET_MB_CPLD_VERSION || log RED "Get MB CPLD version failed."
    GET_BP_CPLD_VERSION || log RED "Get BP CPLD version failed."
    echo "------------------------------------------------" >> "$version_file"
    echo "" >> "$version_file"
    GET_DMESG
    GET_FRU
    GET_BMC_SEL || log RED "Get BMC SEL failed."
    GET_LSPCI_VT || log RED "Get lspci -vt failed."
    GET_LSPCI_VVV || log RED "Get lspci -vvv failed."
    if [[ "$PLATFORM" == "NVIDIA" ]]; then
        GET_IBDEV2NETDEV
        GET_MST_STATUS
        GET_IBSTAT
        GET_LSPCI_n_d_10de_vvv_grep_width
        GET_LSPCI_n_d_15b3_vvv_grep_width
    elif [[ "$PLATFORM" == "AMD" ]]; then
        GET_LSPCI_n_d_75a0_vvv_grep_width
        GET_LSPCI_n_d_75a3_vvv_grep_width
        GET_LSPCI_n_d_1500_vvv_grep_width
    fi
    GET_LSPCI_n_d_1000_vvv_grep_a_4_200_b_23
    echo "------OS driver & software related  version------" >> "$version_file"
    GET_OS_VERSION
    GET_DPKG_LIST
    if [[ "$PLATFORM" == "NVIDIA" ]]; then
        GET_CUDA_VERSION
        GET_GPU_DRIVER_VERSION
        GET_MFT_VERSION
        GET_DOCA_ALL_VERSION
        GET_FM_VERSION
        GET_DCGMI_VERSION
    fi
    echo "-------------------------------------------------" >> "$version_file"
    log GREEN "All information has been collected."
    cat "$version_file"
    if [[ "$1" == "--fty" ]]; then
        echo ""
        log YELLOW "==== Please refer to the following information to select the DIAG version. ===="
        grep -E "GPU_[0-9]+_FW_Version|MFT_Version|DOCA_Version" "$version_file"
        log YELLOW "==============================================================================="
    fi
    
    DISABLE_HMC_FORWARDING
    archive
}

main "$@"
