#!/bin/bash
##链路IO超时ABORT告警功能设置 --- 功能脚本

##############     脚本内部函数定义     #################
ABORT_ALARM_SET_PRINT_HELP_INFO()
{
    echo -e "fc_alarm.sh -h                                                   : show abort alarm set usage."
    echo -e "fc_alarm.sh setabortalarmswitch [status]                         : set the switch status for abort alarm [1:open, 0:close]."
    echo -e "fc_alarm.sh setabortalarmsuppressionperiod [period]              : set the suppression period for reporting abort alarms [30-1440]min."
}

ABORT_ALARM_SWITCH_CHECK_AND_SET()
{
    echo "WARNING: You are about to switch abort alarm function."
    echo "Suggestion: Exercise caution when performing this operation."
    echo "Are you sure you really want to perform the operation?(y/n)"
    read -p "" L_INPUT
    if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ]; then
        SET_ABORT_ALARM_SWITCH=`echo "$G_CMD_PARAM" | awk 'END{if(NF != 2) print 1; else print 0}'`
        if [ "$SET_ABORT_ALARM_SWITCH" -ne "0" ];then
            echo "Input parameter number error!"
            echo "Try 'help' for more information."
            return 1
        fi
		
		#minisystem下开关状态参数校验
        SWITCH_FLAG=`echo "$G_CMD_PARAM" | awk '{print $2}' | grep -E "^[0-1]{1}$"`
        if [ -z "$SWITCH_FLAG" ];then
            echo "paramenter value is error, please check your input."
            return 1
        fi
		
		#调用接口配置开关，底层在extend.ini中新增字段用于持久化保存开关状态，以供驱动代码读取并将开关状态配置给芯片
        result=`echo -e "econf renewcfg GLOBAL ABORT_ALARM_SWITCH $SWITCH_FLAG\nexit\n" | diagnose --auto |grep Success`
        if [ $? != 0 ]; then
            echo "Status: Fail."
            echo "Detail: set abort alarm switch failed, Please Retry."
            return 1
        else
            echo "Status: Success."
            echo "Set the abort alarm switch success."
        fi
    fi
    return 0
}

ABORT_ALARM_SUPPRESSION_PERIOD_CHECK_AND_SET()
{
    echo "WARNING: You are about to set the suppression period for reporting abort alarms [30-1440]min."
    echo "Suggestion: Exercise caution when performing this operation."
    echo "Are you sure you really want to perform the operation?(y/n)"
    read -p "" L_INPUT
    if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ]; then
        SUPPRESSION=`echo "$G_CMD_PARAM" | awk 'END{if(NF != 2) print 1; else print 0}'`
        if [ "$SUPPRESSION" -ne "0" ];then
            echo "Input parameter number error!"
            echo "Try 'help' for more information."
            return 1
        fi
		
		#minisystem下时间参数校验
        SUPPRESSION_PERIOD=`echo "$G_CMD_PARAM" | awk '{print $2}' | grep -E "^([3-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"`
        if [ -z "$SUPPRESSION_PERIOD" ];then
            echo "paramenter value is error, please check your input."
            return 1
        fi
		
		#调用接口配置超时时间
        result=`echo -e "econf renewcfg GLOBAL ABORT_ALARM_SUPPRESSION_PERIOD $SUPPRESSION_PERIOD\nexit\n" | diagnose --auto |grep Success`
        if [ $? != 0 ]; then
            echo "Status: Fail."
            echo "Detail: Set the set the suppression period for reporting abort alarms failed, Please Retry."
            return 1
        else
            echo "Status: Success."
            echo "Set the set the suppression period for reporting abort alarms success."
        fi
    fi
    return 0
}

# --------------------------  main  ------------------------------------------ #
G_CMD_PARAM=$*
case $1 in
    "-h")
        ABORT_ALARM_SET_PRINT_HELP_INFO
        exit 0
        ;;
    "setabortalarmswitch")
        ABORT_ALARM_SWITCH_CHECK_AND_SET
        exit 0
        ;;
    "setabortalarmsuppressionperiod")
        ABORT_ALARM_SUPPRESSION_PERIOD_CHECK_AND_SET
        exit 0
        ;;
    *)
        echo "usage: fc_alarm.sh -h"
        exit 1
esac
ABORT_ALARM_SET_PRINT_HELP_INFO
exit 0