#!/bin/bash

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
[ -f ${G_INSPECT_MMLPATH}/CheckItems ] && source ${G_INSPECT_MMLPATH}/CheckItems
source ${G_INSPECT_MMLPATH}/CommonFunc.sh

function CheckCtnrGcConfigured()
{
    local iRet=0
    local isPass=0
    local patchVersion=""
    local curCtnrGcSwitch=""
    local recommendCtnrGcSwitch=""
    local productVersion=""
    local curInspectNum="360"
    local curInspectFun="$(GetInspectType $curInspectNum)"
    local resultFile="/tmp/tmpResult${curInspectFun}"
    local mds_conf="/opt/huawei/snas/etc/mds_c.ini"
    >${resultFile}

    #获取back_ip
    local back_ip=$(GetLocalIp)
    if [ -z "${back_ip}" ]; then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO:Get local back ip fail!" >> ${resultFile} 2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile}
        LOG "[$LINENO]ERR: ${curInspectFun} local back ip is empty, abort!"
        return
    fi

    productVersion=$(grep ExternalVersion /opt/huawei/deploy/package/version |awk -F= '{print $2}')
    local patchInfo=$(/opt/huawei/snas/upd/patch_script/cluster_patch.sh "$back_ip" show)
    iRet=$?
    if [[ ${iRet} -ne 0 ]];then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO: Get hot patch version Failed!" >> ${resultFile} 2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile}
        LOG "[$LINENO]ERR: ${curInspectFun} Get hot patch version Failed!"
        return
    fi

    patchVersion=$(echo "${patchInfo}"| grep "Version:" |awk -F, '{print $3}' | awk -F: '{print $2}' 2>/dev/dull)
    patchVersion=${patchVersion:-null}

    recommendCtnrGcSwitch=2
    if [[ "${patchVersion}" != "null" ]] ;then
        local ver=$(echo "${patchVersion}" | grep -Po '(?<=SPH)[0-9]+')
        if [[ "$ver" == "" ]];then
            isPass=$(check_pass ${isPass} 1)
            echo "ProductVersion:${productVersion}||PatchVersion:${patchVersion}||CtnrGcSwitch:--||RecommendCtnrGcSwitch:--" >> ${resultFile}  2>&1
            echo "[ERR]INFO:Malformed patch name format error!" >> ${resultFile} 2>&1
            echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
            LOG "[$LINENO]ERR: ${curInspectFun} Malformed patch name (${patchVersion}) format errord!"
            return
        fi
        if [[ "${productVersion}" == "7.0.1" ]] && [ ${ver} -ge 2 ] ;then
            recommendCtnrGcSwitch=1
        elif [[ "${productVersion}" == "7.0.2" ]] && [ ${ver} -ge 5 ] ;then
            recommendCtnrGcSwitch=1
        elif [[ "${productVersion}" == "7.1.0" ]] && [ ${ver} -ge 1 ] ;then
            recommendCtnrGcSwitch=1
        fi
    fi

    local ctnrGcInfo=$(/usr/local/bin/MmlBatch 988 "mds show ctnrgc")
    iRet=$?
    if [[ ${iRet} -ne 0 ]];then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO:MML command execution failed!" >> ${resultFile} 2>&1
        echo "${curInspectFun}_Pass $isPass" >>${resultFile}
        LOG "[$LINENO]ERR: ${curInspectFun} MML command execution failed!"
        return
    fi

    curCtnrGcSwitch=$(echo "${ctnrGcInfo}" | sed -n "s/.*CtnrGc Switch.*= \([0-9]\+\).*/\1/p")
    #CtnrGc Switch不是推荐配置
    if [[ "${curCtnrGcSwitch}" != "" ]] && [[ ${curCtnrGcSwitch} -ne  ${recommendCtnrGcSwitch} ]];then

        local areaKey=$(GetAreaKey)
        if [[ ${areaKey} == "true" ]];then
            sed -i 's/ctnrgc_system_switch=.*/ctnrgc_system_switch='"${recommendCtnrGcSwitch}"'/g' ${mds_conf}
            local updateInfo=$(/usr/local/bin/MmlBatch 988 "mds cfg update")
            iRet=$?
            local retSucc=$(echo "${updateInfo}" | grep "mds config update success" )
            if [[ ${iRet} -eq 0 ]] && [[ "X${retSucc}" != "X" ]];then
                curCtnrGcSwitch=${recommendCtnrGcSwitch}
            else
                isPass=$(check_pass ${isPass} 1)
                echo "[ERR]INFO:CtnrGc Switch is not recommended!" >> ${resultFile} 2>&1
            fi
        else
            isPass=$(check_pass ${isPass} 1)
            echo "[ERR]INFO:CtnrGc Switch is not recommended!" >> ${resultFile} 2>&1
        fi

    fi

    echo "ProductVersion:${productVersion}||PatchVersion:${patchVersion}||CtnrGcSwitch:${curCtnrGcSwitch}||RecommendCtnrGcSwitch:${recommendCtnrGcSwitch}" >> ${resultFile}  2>&1
    echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1

}

CheckCtnrGcConfigured
#总是返回成功，以[ERR]标识是否有误
exit 0

