#!/bin/bash
# **************************************************************************** #
# Function Name: READDIRPLUS_CHECK_SWITCH_CAN_DELE_ROLL
# Description:检查业务模块是否可以回滚，readdirplus转发优化开关
# Parameter: 
# Return:  0  成功 1   失败
# **************************************************************************** #

# ------------------------------  function  ---------------------------------- #

# **************************************************************************** #
# Description: 执行 CLI DEBUG 命令
# Parameter: $1 需要执行的命令
# **************************************************************************** #
function space_ExecCliCmds()
{
    echo -e "${1}\nexit\n" | diagnose --set-cli --auto | grep -o "Readdirplus optimization switch : on"
}

# **************************************************************************** #
# Description: 进行操作返回值判断
# Parameter: None
# **************************************************************************** #
function space_checkReaddirplusSwitch()
{
    local switchStatus="off"
	
    queryResult=$(space_ExecCliCmds "pal showstage show_readdirplus_switch")
    if [ -z "$queryResult" ];
    then
        return 0
    fi

    if [[ $queryResult =~ $switchStatus ]]
    then 
       return 0  
    fi 
    return 1
}

# --------------------------------  main  ------------------------------------ #
READDIRPLUS_CHECK_SWITCH_CAN_DELE_ROLL()
{
    local operatetype=$1
    local sph_version=$2
    local switchflag=0

    space_checkReaddirplusSwitch
    switchflag=$?

    if [ "$operatetype" == "del" ] && [ $switchflag == 1 ];then
        OS_echo "please switch off readdirplus switch before delete hotpatch"
        return 1    
    fi
	    
    #不能回滚到SPH033之前的版本
    if [ "$operatetype" == "roll" ] && [ $switchflag == 1 ];then
        local readdirplus_version=033
        if [ -z "$sph_version" ];then
            OS_echo "no roll version, may be wrong"
            return 1 
        fi
        OS_echo "readdirplus version is $readdirplus_version, roll version is $sph_version"
        if [ "$readdirplus_version" \> "$sph_version" ];then
            OS_echo "please switch off readdirplus switch before roll hotpatch"
            return 1    
        fi
    fi
    
    return 0
}

READDIRPLUS_CHECK_SWITCH_CAN_DELE_ROLL "$@"
