#!/bin/bash

[ -f /opt/huawei/snas/script/inspect_mml/CheckItems ] && source /opt/huawei/snas/script/inspect_mml/CheckItems
[ -f /opt/huawei/snas/script/inspect_mml/inspect_comm.sh ] && source /opt/huawei/snas/script/inspect_mml/inspect_comm.sh

function is3008SasCard()
{
    lspci |grep SAS3008 >/dev/null 2>&1
    return $?
}

function writeResultFile()
{
    local isPass=$1
    local curInspectFun=$2
    local resultFile=$3
    local msgInfo=$4
    
    echo "$msgInfo" >> ${resultFile} 2>&1
    echo "${curInspectFun}_Pass ${isPass}" >> ${resultFile} 2>&1
    LOG "[$FUNCNAME]${curInspectFun}_Pass ${isPass}"
}

function DeepCheckHardDriveFirmware()
{
    local iRet=""
    local slotList=""
    local i15SEList=""
    local isHSSDD5List=""
    local allList=""
    local FWVersion=""
    local devName=""
    local SESVersion=""
    local isPass=0
    local productName=""
    local curInspectNum="321"
    local curInspectFun="$( GetInspectType $curInspectNum )"
    local resultFile="/tmp/tmpResult${curInspectFun}"
    >${resultFile}
    
    productName=${PRODUCT_NAME}
     
    IsRH2288Node
    iRet=$?
    if [ "${iRet}" -ne 0 ];then
        isPass=3
        writeResultFile ${isPass} $curInspectFun ${resultFile} "Product Name:$productName||SAS3008 Version:-- ||SES Version:--"
        return 
    fi

    is3008SasCard
    iRet=$?
    if [[ ${iRet} -eq 1 ]];then
        isPass=3
        writeResultFile ${isPass} $curInspectFun ${resultFile} "Product Name:$productName||SAS3008 Version:-- ||SES Version:--"
        return 
    fi

    FWVersion=$(/opt/driver/lsisas-mpt3sas-driver/sas3flash -listall | grep SAS3008 | awk '{print $3}' | grep -Po '[0-9+]+\.[0-9]+\.[0-9]+\.[0-9]+')
    if [[ "${FWVersion}" == "" ]];then
        isPass=1
        writeResultFile ${isPass} $curInspectFun ${resultFile} "[ERR]INFO:can not get SAS Firmware Version!"
        return 
    fi
    if [[ "${FWVersion}" < "15.00.07.00" ]];then
        writeResultFile ${isPass} $curInspectFun ${resultFile} "Product Name:$productName||SAS3008 Version:$FWVersion ||SES Version:--"
        return 
    fi
    
    devName=$(lsscsi -g |grep "Expander" |awk '{print $NF}')
    if [ "$devName" == "" ];then
        isPass=1
        writeResultFile ${isPass} $curInspectFun ${resultFile} "[ERR]INFO:can not get Expander device!"
        return
    else
        # еexpanderToolһ, ܻ
        local expanderTool=$(ls /opt/hardware_firmware/rh/ses/**/expanderTool | head -1)
        if [ -f $expanderTool ];then
            chmod +x $expanderTool
            SESVersion=$($expanderTool query $devName | grep "Image Version" |grep -Po '[0-9]+')
            if [ "X${SESVersion}" == "X" ];then
                isPass=1
                writeResultFile ${isPass} $curInspectFun ${resultFile} "[ERR]INFO:SES version is NULL!"
                return 
            fi
            if [ ${SESVersion} -ge 131 ];then
                writeResultFile ${isPass} $curInspectFun ${resultFile} "Product Name:$productName||SAS3008 Version:$FWVersion ||SES Version:${SESVersion}"
                return 
            fi
        else
            isPass=1
            writeResultFile ${isPass} $curInspectFun ${resultFile} "[ERR]INFO:SES tool ExpanderTool is not exist,can not get SES version!"
            return
        fi
    fi

    /usr/local/bin/MmlBatch 4016 "cm localdiskinfo" >> /dev/null 2>&1
    i15SEList=$(cat /tmp/tmpfilelocaldiskinfo | grep Toshiba | grep 15SE)
    isHSSDD5List=$(cat /tmp/tmpfilelocaldiskinfo | grep "Model: HSSD-D5" )
    allList=$(echo -e "$i15SEList\n$isHSSDD5List")
    if [ "X${i15SEList}" != "X" ] || [ "X${isHSSDD5List}" != "X" ]; then
        set_ifs

        for line in $allList; do
            if [ "$line" = "" ];then
                continue;
            fi
            slot=$(echo "${line}" | awk -F'\\|\\|' '{print $3}' | awk -F':' '{print $2}' | tr -d ' ')
            if [ "X$slotList" == "X" ];then
                slotList=$slot
            else
                slotList=$slotList","$slot
            fi
        done
        restore_ifs
        isPass=1
        echo "[ERR]INFO:Node has HSSD-D5 or Toshiba 15SE hard disk, slots is ($slotList).SAS3008 Version(${FWVersion}),SES Version(${SESVersion})!" >>${resultFile} 2>&1
    else
        isPass=4
    fi
    echo "Product Name:$productName||SAS3008 Version:$FWVersion ||SES Version:${SESVersion}" >>${resultFile} 2>&1

    echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
    LOG "[$FUNCNAME]${curInspectFun}_Pass ${isPass}"
    return 
}
#Hard drive firmware deep inspection
DeepCheckHardDriveFirmware

exit 0


