#!/bin/bash
#链路IO超时ABORT告警功能设置预处理脚本 --- 采用extend.ini做持久化配置
 
PATCH_FILE_LIST=("usr/local/etc/os_debug_mode_cmd_list")
MIMISYSTEM_CONF1=("/OSM/conf/sudoers.ini")
MIMISYSTEM_CONF2=("/etc/sudoers")
PATCH_START()
{
    #将功能脚本添加进相应的白名单
    
    for f in ${PATCH_FILE_LIST[*]}
    do
        if [ -f "/$f" ]; then
        tmpResult=$(cat /$f |grep 'fc_alarm.sh')
        if [ -n "$tmpResult" ]; then
            echo "abort alarm set function is in cmd list."
        else
            sed -i "/upgrade_clearflag.sh/a\fc_alarm.sh" "/$f"
        fi
        fi
    done
    
    echo "[$0] enable set abort alarm function successfully, ret $?."

    sed -i 's/\/OSM\/script\/cli_server_switch_cfg\.sh/\/OSM\/script\/cli_server_switch_cfg\.sh,\/OSM\/script\/fc_alarm\.sh/g' $MIMISYSTEM_CONF1

    echo "edit conf1, ret $?."


    sed -i 's/\/OSM\/script\/cli_server_switch_cfg\.sh/\/OSM\/script\/cli_server_switch_cfg\.sh,\/OSM\/script\/fc_alarm\.sh/g' $MIMISYSTEM_CONF2


    echo "edit conf2, ret $?."

    return 0
}

PATCH_END()
{
    #add your code here
    return 0
}

PATCH_ROLLBACK()
{
	#补丁回滚/卸载:
	###1.将功能脚本从白名单中移除

    for f in ${PATCH_FILE_LIST[*]}
    do
        if [ -f "/${f}" ]; then
        sed -i "/fc_alarm.sh/d" "/$f"
        fi
    done
    
    echo "[$0] disable set fc alarm cmd successfully, ret $?."

    sed -i 's/,\/OSM\/script\/fc_alarm\.sh//g' $MIMISYSTEM_CONF1
    
    sed -i 's/,\/OSM\/script\/fc_alarm\.sh//g' $MIMISYSTEM_CONF2

    echo "clear history switch set, ret $?."

    return 0
}

PATCH_ROLLEND()
{
    #add your code here
    return 0
}

#--------------------------------- main ----------------------

#根据命令执行脚本
L_CMD=$1
case $L_CMD in
    "start") PATCH_START
               ;;
    "end") PATCH_END
               ;;
    "rollstart") PATCH_ROLLBACK
               ;;
    "rollend") PATCH_ROLLEND
               ;;
    *)
    echo "unsupported option."
esac
exit 0