#!/bin/bash
set +x

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
source $G_INSPECT_MMLPATH/CommonFunc.sh
CurInspectNum="296"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
if [ -L "$RESULTFILE" ]; then
    rm -f "$RESULTFILE"
fi
>"${RESULTFILE}"
LOG_FILE="/var/log/inspect.log"

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

# 元数据位置配置检查
function CheckMetaDataLocationCfg()
{
    local isPass
    local Count
    local diskType
    local NodePool1
    local ret1
    local ret2
    local ret3
    local ret4
    local monsCfg="/opt/huawei/snas/etc/mons.ini"
    local external_version

    isPass=0
    Count=$(awk -F '=' '/\[MetaDataLocation\]/{a=1}a==1&&$1~/Count/{print $2;exit}' "${monsCfg}")
    ret1=$?
    diskType=$(awk -F '=' '/\[MetaDataLocation\]/{a=1}a==1&&$1~/diskType/{print $2;exit}' "${monsCfg}")
    ret2=$?
    NodePool1=$(awk -F '=' '/\[MetaDataLocation\]/{a=1}a==1&&$1~/NodePool1/{print $2;exit}' "${monsCfg}")
    ret3=$?
    external_version=$( grep "^ExternalVersion" /opt/huawei/deploy/package/version |awk -F"=" '{print $2}' |tr -d " " )
    ret4=$?
    LOG "[$FUNCNAME][${LINENO}]ret1=$ret1,ret2=$ret2,ret3=$ret3, ret3=$ret4, Count=$Count, diskType=$diskType, NodePool1=$NodePool1, ExternalVersion=${external_version}!"

    # 仅检查7.1.1版本
    if [ "${external_version}" != "7.1.1" ]; then
        isPass=3
        echo "INFO:Do not involve to inspect(only check ver7.1.1)." >> ${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass $isPass" >> "${RESULTFILE}" 2>&1
        LOG "[$FUNCNAME][${LINENO}]CheckMetaDataLocationCfg end, isPass=$isPass!"
        exit
    fi
    echo "ExternalVersion:${external_version}||Count:$Count||diskType:$diskType||NodePool1:$NodePool1" >> "${RESULTFILE}" 2>&1
    
    # 如果Count的值为0，则检查通过，否则继续下一步检查
    if [ "${Count}" -eq 0 ]; then
        isPass=0
    else
        # 如果没有配置diskType的值，则返回检查不通过，否则继续下一步检查。
        if [ -z "${diskType}" ]; then
            isPass=1
            echo "[ERR]INFO:The value of diskType is not set." >> ${RESULTFILE} 2>&1
        else
            # 如果NodePool1的值中包含有“-”选项，则返回检查不通过，否则检查通过
            echo "${NodePool1}" |grep '-'
            ret1=$?
            if [ ${ret1} -eq 0 ]; then 
                isPass=1
                echo "[ERR]INFO:The value of NodePool1 contains '-'." >> ${RESULTFILE} 2>&1
            else
                isPass=0
            fi
        fi
    fi

    echo "${CurInspectFun}_Pass $isPass" >> "${RESULTFILE}" 2>&1
    LOG "[$FUNCNAME][${LINENO}]CheckMetaDataLocationCfg end, isPass=$isPass!"
}

CheckMetaDataLocationCfg
exit 0