#!/bin/bash

#检查存储版本
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source ${G_INSPECT_MMLPATH}/CheckItems
CurInspectNum="350"
CurInspectFun="$( GetInspectType $CurInspectNum )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

#视频监控场景
surveillance_service_type="surveillance"
expect_C20_version="V300R006C20SPC300"
expect_C00_version="V300R006C00SPC200"
expect_C00SPH_version="V300R006C00SPH207"
expect_C20_svn="114889"
expect_C00_svn="98356"
configFile="/opt/huawei/snas/etc/snas.ini"
patch_confirm_path="/var/huawei/patch/cur/sph"
patch_active_path="/var/huawei/patch/tmp/sph"

function StorageVersionCheck()
{
    local service_type=$( grep "^serviceType" /opt/huawei/snas/etc/snas.ini | awk -F"=" '{print $2}' | tr -d " " )
    local product_version=$( grep "^ProductVersion" /opt/huawei/deploy/package/version |awk -F"=" '{print $2}' |tr -d " " )
    local svn_file="/opt/huawei/deploy/package/svn.txt"
    local patch_expect_process="100 0"
    local Patch_Version="--"
    local Patch_Status="--"
    local product_svn="--"
    local streamType="--"
    local VM_NUM="--"
    local VM_MEM="--"
    local isPass=0

    if [ ! -f "${svn_file}" ];then
        product_svn="NULL"
    else
        product_svn=$( tail -n 1 ${svn_file} )
    fi
    if [ "X${product_version}" == "X${expect_C20_version}" ];then

	if [ "X${expect_C20_svn}" != "X${product_svn}" ];then
	    isPass=1
	    echo "[ERR]${product_version} Product Child version ${product_svn} is not match the standard version ${expect_C20_svn}" >>${RESULTFILE} 2>&1
            LOG "[$CurInspectFun][FUNCNAME] ${product_version} Product Child version ${product_svn} is not match the standard version ${expect_C20_svn}"
	fi
	if [ "X${service_type}" == "X${surveillance_service_type}" ];then
	     streamType=$( grep "streamStoreMode" /opt/huawei/snas/etc/snas.ini |awk -F"=" '{print $2}' )
	     VM_NUM=$( grep "VM_NUM" /opt/huawei/snas/etc/snas.ini |awk -F"=" '{print $2}' )
	     VM_MEM=$( grep "VM_MEM" /opt/huawei/snas/etc/snas.ini |awk -F"=" '{print $2}' )
        fi
        echo "ProductVersion:${product_version}||ProductChildVersion:${product_svn}||Patch_Version:${Patch_Version}||Patch_Status:${Patch_Status}||ServiceType:${service_type}||streamType:${streamType}||VM_NUM:${VM_NUM}||VM_MEM:${VM_MEM}" >>${RESULTFILE} 2>&1

    elif [ "X${product_version}" == "X${expect_C00_version}" ];then

        if [ "X${service_type}" == "X${surveillance_service_type}" ];then
	    isPass=1
            echo "[ERR]ProductVersion=${product_version}. The surveillance scene expected version is ${expect_C20_version} ." >>${RESULTFILE} 2>&1
        fi
	if [ "X${expect_C00_svn}" != "X${product_svn}" ];then
	    isPass=1
	    echo "[ERR]${product_version} Product Child version ${product_svn} is not match the standard version ${expect_C00_svn}" >>${RESULTFILE} 2>&1
            LOG "[$CurInspectFun][FUNCNAME] ${product_version} Product Child version ${product_svn} is not match the standard version ${expect_C00_svn}"
	fi
        if [ ! -d "${patch_confirm_path}" ];then
	    isPass=1
            echo "[ERR]ProductVersion=${product_version}. There is no hotpatch,please apply the ${expect_C00SPH_version} or later version of hotpatch." >>${RESULTFILE} 2>&1

        else
            Patch_Version=$( ls -l "${patch_confirm_path}" |grep "^d" |awk '{print $NF}' |awk -F"_" '{print $NF}')
            Patch_Status=$( cat /var/huawei/patch/node_state_file |awk '{print $1}' )
            patch_process=$( cat /var/huawei/patch/node_state_file |awk '{print $2" "$3}' )
	    LOG "[${CurInspectFun}][${CurInspectNum}][FUNCNAME] Patch_Version:$Patch_Version,Patch_Status:$Patch_Status"
            if [ "X${patch_process}" != "X${patch_expect_process}" ];then
	        isPass=1
                echo "[ERR]patch_process=${patch_process}.The hotpatch status is not right.Please handle this hotpatch and make sure it is normal." >>${RESULTFILE} 2>&1
            fi
            if [ "X${Patch_Version}" == "X" ];then
	    	isPass=1
                echo "[ERR]There are no hotpatch status is confirm,Please make sure the hotpatch version is ${expect_C00SPH_version} and status is confirm." >>${RESULTFILE} 2>&1
            fi
            if [[ "X${Patch_Version}" < "X${expect_C00SPH_version}" ]];then
	        isPass=1
                echo "[ERR]ProductVersion=${product_version},product_patch_version=${Patch_Version}.Please make sure the hotpatch version is ${expect_C00SPH_version} or later." >>${RESULTFILE} 2>&1
            fi
            if [ "X${Patch_Status}" == "Xactive" ];then
	        isPass=1
                echo "[ERR]ProductVersion=${product_version},product_patch_status=${Patch_Status}.Please make sure the hotpatch status is confirm." >>${RESULTFILE} 2>&1
            fi
        fi
        echo "ProductVersion:${product_version}||ProductChildVersion:${product_svn}||Patch_Version:${Patch_Version}||Patch_Status:${Patch_Status}||ServiceType:${service_type}||streamType:${streamType}||VM_NUM:${VM_NUM}||VM_MEM:${VM_MEM}" >>${RESULTFILE} 2>&1
    else
        isPass=1
        echo "ProductVersion:${product_version}||ProductChildVersion:${product_svn}||Patch_Version:${Patch_Version}||Patch_Status:${Patch_Status}||ServiceType:${service_type}||streamType:${streamType}||VM_NUM:${VM_NUM}||VM_MEM:${VM_MEM}" >>${RESULTFILE} 2>&1
    fi

    return ${isPass}
}

function main
{
    local iRet=0
    StorageVersionCheck
    iRet=$?
    echo "${CurInspectFun}_Pass ${iRet}" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass ${iRet}"
    return
}

main


