#!/bin/bash

G_INSPECT_MMLPATH="/opt/product/snas/script/inspect_mml"
source "${G_INSPECT_MMLPATH}/CheckItems"

function HPatchStatusRepair
{
    local curInspectNum="504"
    local curInspectFun="$(GetInspectType $curInspectNum)"
    local resultFile="/tmp/tmpResult${curInspectFun}"
    >${resultFile}
    local isPass=0
    local patchPath="/var/huawei/patch/cur/sph/"
    local tmpFileX="/tmp/tmpfileGetHPatchstatus"
    local tmpFile="/tmp/tmpfileGetHPatchstatusXX"
    if [ -L "$tmpFile" ];then
        rm -f "$tmpFile"
    fi
    >$tmpFile
    if [ -L "$tmpFileX" ];then
        rm -f "$tmpFileX"
    fi
    >$tmpFileX
    local pkgDirName=$([ -d $patchPath ] && ls $patchPath)
    if [ $? -eq 0 ];then
        ${G_INSPECT_MMLPATH}/GetHPatchstatus > $tmpFileX
        cat $tmpFileX | grep "Hot patch active fail" >$tmpFile
        dos2unix $tmpFile > /dev/null 2>&1

        local fifoContext=`cat $tmpFile`
        if [ "$fifoContext" != "" ];then
            hotpatchPath="$patchPath""$pkgDirName""/*/hotpatch"
            ${hotpatchPath}/hotpatch.sh active ${hotpatchPath} ${hotpatchPath}/hotpatch.conf > /dev/null 2>&1
            if [ $? -ne 0 ];then
                isPass=1
                echo "[ERR]INFO:Hot patch $pkgDirName active fail." >>${resultFile} 2>&1
            else
                echo "CheckResult:Hot patch $pkgDirName active done." >>${resultFile} 2>&1
            fi
            rm $tmpFile $tmpFileX
        else
            echo "CheckResult:Hot patch $pkgDirName status is active." >>${resultFile} 2>&1
        fi
    else
        echo "CheckResult:Have no HotPatch." >>${resultFile} 2>&1
    fi
    echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
}

HPatchStatusRepair
exit 0