#!/bin/bash
set +x
LOG_FILE="/var/log/inspect.log"
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
source $G_MML_FILE_PATH/CheckItems
source $G_MML_FILE_PATH/CommonFunc.sh
CurInspectNum="128"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
LOCAL_BACK_IP=""
isPass=0
>${RESULTFILE}

function LOG
{
    time=$(date)
    echo [${time}][$$][$CurInspectFun]$@ >> $LOG_FILE
}


function checkFwVersion()
{
    local status=""

    LOCAL_BACK_IP=`GetLocalIp`
    if [ "${LOCAL_BACK_IP}X" == "X" ];then
        echo "[ERR]Node:$(hostname) ||INFO:Can't get backend IP." >> ${RESULTFILE} 2>&1
        LOG "[ERR]Node:$(hostname) ||INFO:Command exec faild."
        isPass=1
        return
    fi

    cat /opt/huawei/deploy/etc/deploy.ini | grep devicename | grep "9000 C72 Node" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Node:$LOCAL_BACK_IP(Not C72 node)||INFO:Do not involve to inspect." >> ${RESULTFILE} 2>&1
        LOG "Node:$LOCAL_BACK_IP(Not C72 node)||INFO:No need to check."
        return
    fi

    echo > /var/log/hdfw_precheck.flag
    if [ $? -ne 0 ]; then
        echo "[ERR]Node: ${LOCAL_BACK_IP}(C72 node)||INFO: Failed to execute command(echo > /var/log/hdfw_precheck.flag)"  >> ${RESULTFILE} 2>&1
        LOG "[ERR]Node: ${LOCAL_BACK_IP}(C72 node)||INFO: Failed to execute command(echo > /var/log/hdfw_precheck.flag)"
        isPass=1
        return
    fi

    /opt/huawei/snas/bin/snas_cm_cmd updfw 10303 0
    if [ $? -ne 0 ]; then
        echo "[ERR]Node: ${LOCAL_BACK_IP}(C72 node)||INFO: Failed to execute command(/opt/huawei/snas/bin/snas_cm_cmd updfw 10303 0)" >> ${RESULTFILE} 2>&1
        LOG "[ERR]Node: ${LOCAL_BACK_IP}(C72 node)||INFO: Failed to execute command(/opt/huawei/snas/bin/snas_cm_cmd updfw 10303 0)"
        isPass=1
        return
    fi

    status=$(cat /var/log/hdfw_precheck.flag)
    if [ "${status}" == "matched" ];then
        echo "Node:${LOCAL_BACK_IP}(C72 node)||INFO:${status}" >> ${RESULTFILE} 2>&1
    elif [ "${status}" == "unmatched" ];then
        if [ "${isPass}" -ne 1 ];then
            isPass=4
        fi
        echo "[ERR]Node:${Item}(C72 node)||INFO:status(${status}) is not matched." >> ${RESULTFILE} 2>&1
    else
        isPass=1
        echo "[ERR]Node:${Item}(C72 node)||INFO:status(${status}) is not matched." >> ${RESULTFILE} 2>&1
    fi
}

checkFwVersion
echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1

