#!/bin/bash

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="312"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

patch_patch=""
LOG_FILE="/var/log/inspect.log"
OLD_VER="V100R001C01SPC100 V100R001C01SPC200 V100R001C01SPC210 V100R001C01SPC290 V100R001C20SPC100 V100R001C20SPC001 V100R001C20 V100R001C30 V100R001C30SPC100 V100R001C30SPC110 V100R001C30 V100R001C30SPC200 V300R005C00SPC100 V300R005C00SPC110 V300R005C00SPC160 V300R005C00SPC170 V300R005C00"
NEW_VER="V300R006C00 V300R006C00SPC100 V300R006C00SPC200 V300R006C10 V300R006C10SPC100 V300R006C20 V300R006C20SPC100 V300R006C20SPC200 V300R006C20SPC300"
#节点补丁状态参数定义
patch_status=""
patch_process=0
patch_result=0
patch_version=""
#0:OK,1:ERR,4:no hotpatch

function LOG
{
   time=$(date)
   echo [${time}][$$]$@ >> ${LOG_FILE}
}
function get_basepathfromver
{
    local productversion="$( grep ProductVersion /opt/huawei/deploy/package/version | awk -F "=" '{print $2}' )"
    if [ $? -ne 0 -o "X${productversion}" == "X" ];then
        LOG "get productversion failed." 
        return 1
    fi
    
    echo ${OLD_VER} | grep -w ${productversion} >/dev/null 2>&1
    if [ $? -eq 0 ];then
        patch_patch="/opt/huawei/patch"
        return 0
    fi
    echo ${NEW_VER} | grep -w ${productversion} >/dev/null 2>&1
    if [ $? -eq 0 ];then
        patch_patch="/var/huawei/patch"
        return 0
    fi
    LOG "[$FUNCNAME]ERR Ver:${productversion}."
    echo "INFO:This ${productversion} has no hotpatch." >>${RESULTFILE} 2>&1
    return 4
}

function get_checkpath()
{
    local ProssFlag=0
    local IsExitFileFlag=0
    patch_status_file="${patch_patch}/node_state_file"
    #如果文件不存在，说明没有打过补丁
    if [ ! -f ${patch_status_file} ];then
        echo "INFO:no hotpatch." >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]not exist path,pass."
        return 4
    fi
    patch_status="$( cat $patch_status_file | awk '{print $1}' )"
    patch_process="$( cat $patch_status_file | awk '{print $2}' )"
    patch_result="$( cat $patch_status_file | awk '{print $3}' )"
    if [ "X${patch_result}" != "X0" -o "${patch_process}" != "100" ];then
        ProssFlag=1
    fi
    if [ ! -d ${patch_patch}/tmp/sph -a  ! -d ${patch_patch}/cur/sph ];then
        echo "INFO:no hotpatch." >>${RESULTFILE} 2>&1
        return 4
    fi
    
    #判断rollback状态时环境还是否有效
    local IsExitFile="$( find "${patch_status}""/cur/" ! -type d |wc -l )"
    if [ "${IsExitFile}" != "0" ];then
        IsExitFileFlag=1
    fi
    #判断tmp/sph目录下是否有热补丁,cur/sph下是否有热补丁
    COUNTtmp="$( ls -l ${patch_patch}/tmp/sph |grep '^d' |wc -l )"
    COUNTcur="$( ls -l ${patch_patch}/cur/sph |grep '^d' |wc -l )"
    if [ 1 -eq ${COUNTtmp} ];then
        if [ "X${patch_status}" == "Xload" -a ${COUNTcur} -eq 1 ];then 
            patch_patch="${patch_patch}""/cur/sph/"
            return 0
        elif [ "X${patch_status}" == "Xactive" -a 0 -eq ${ProssFlag} ];then
            patch_patch="${patch_patch}""/tmp/sph/"
            return 0
        fi
    fi
    if [ 1 -eq ${COUNTcur} ];then
        if [ "X${patch_status}" == "Xconfirm" -a 0 -eq ${ProssFlag} ];then
            patch_patch="${patch_patch}""/cur/sph/"
            return 0
        elif [ "X${patch_status}" == "Xrollback" -a 0 -eq ${ProssFlag} -a 1 -eq ${IsExitFileFlag} ];then
            patch_patch="${patch_patch}""/cur/sph/"
            return 0
        elif [ "X${patch_status}" == "Xactive" -a 0 -ne ${ProssFlag} ];then
            patch_patch="${patch_patch}""/cur/sph/"
            return 0
        fi
    fi
    echo "INFO:no hotpatch.">>${RESULTFILE} 2>&1
    return 4
}
function check_hotpatch()
{
    local hotpatch_num=0
    local hotpatch_list=""
    local hotpatch_activenum=0
    
    count="$( ls -ld ${patch_patch} |wc -l )"
    if [ ${count} -ne 1 ];then
        LOG "there another path below ${patch_patch}."
        return 1
    fi
    cd ${patch_patch}
    if [ $? -ne 0 ];then
        LOG "[$FUNCNAME]cd ${patch_patch} ERR."
        return 1
    fi
    patch_name="$( ls -l |grep '^d'|awk '{print $NF}' )"
    patch_version="$( echo $patch_name|awk -F"_" '{print $3}' )"
    patch_patch="${patch_patch}""$patch_name""/V300R006C20/hotpatch/"
    
    cd ${patch_patch}
    if [ $? -ne 0 ];then
        LOG "[$FUNCNAME]cd Dir:${patch_patch} ERR."
        return 1
    fi
    hotpatch_num="$( ls -l | grep "\.patch" | awk '{print $NF}'|wc -l )"
    if [ ${hotpatch_num} -eq 0 ];then
        LOG "[$FUNCNAME]no hotpatch."
        return 0
    fi
    hotpatch_list="$( ls -l | grep "\.patch" | awk '{print $NF}'|awk -F"." '{print $1}'|awk -F "_" '{print $NF}' )"
    for line in ${hotpatch_list};do
        if [ -z ${line} ];then
            LOG "[$FUNCNAME]${line} is empty."
            continue
        fi
        livepatch -q${line} | grep "^Patch State" | grep -q "Active"  >/dev/null 2>&1
        if [ $? -eq 0 ];then
            LOG "[$FUNCNAME]Get info from ${line} success."
            let "hotpatch_activenum++"
        else
            echo "INFO:hotpatch ${line} active failed." >>${RESULTFILE} 2>&1
        fi
    done
    if [ ${hotpatch_num} -eq  ${hotpatch_activenum} ];then
        echo "INFO:TotalPatchNumber=$hotpatch_num;TotalActiveNumber=$hotpatch_activenum" >>${RESULTFILE} 2>&1
        return 0
    fi
    return 1
}

function main
{
    local funlist="get_basepathfromver get_checkpath check_hotpatch"
    local iRet=0
    for fun in ${funlist};do
        $fun
        iRet=$?
        if [ ${iRet} -eq 1 ];then
            echo "${CurInspectFun}_Pass $iRet" >>${RESULTFILE} 2>&1
            LOG "[$FUNCNAME]${CurInspectFun}_Pass $iRet,failed at $fun"
            return 1
        elif [ ${iRet} -eq 4 ];then
            break
        fi
    done
    echo "${CurInspectFun}_Pass 0" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass 0"
    return 0
}

main

