#!/bin/bash
##队列例测卡修复时间设置脚本 --- 功能脚本
####实现原理：
######1.打补丁时借助预处理脚本添加白名单
######2.使用者在minisystem下执行本脚本，配置超时时间，同步将时间写入extend.ini中新增字段
######3.队列例测时读取extend.ini中用户设置的超时时间

##############     脚本内部函数定义     #################
ROUTE_TIME_INTERVAL_SET_PRINT_HELP_INFO()
{
    echo -e "fc_routineset.sh -h                                                   : show route time set usage."
    echo -e "fc_routineset.sh settimeoutinterval [interval]                        : set the timeout interval for routine. interval_threshold[5-10](s),default interval 10s."
}

ROUTE_TIME_INTERVAL_CHECK_AND_SET()
{
    echo "WARNING: You are about to set the timeout interval for routine."
    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
        ROUTE_TIMEOUT=`echo "$G_CMD_PARAM" | awk 'END{if(NF != 2) print 1; else print 0}'`
        if [ "$ROUTE_TIMEOUT" -ne "0" ];then
            echo "Input parameter number error!"
            echo "Try 'help' for more information."
            return 1
        fi
		
		#minisystem下时间参数校验
        TIMEOUT_INTERVAL=`echo "$G_CMD_PARAM" | awk '{print $2}' | grep -E "^([5-9]|10)$"`
        if [ -z "$TIMEOUT_INTERVAL" ];then
            echo "paramenter value is error, please check your input."
            return 1
        fi
		
		#调用接口配置超时时间
        result=`echo -e "econf renewcfg GLOBAL ROUTE_TIMEOUT_INTERVAL $TIMEOUT_INTERVAL\nexit\n" | diagnose --auto |grep Success`
        if [ $? != 0 ]; then
            echo "Status: Fail."
            echo "Detail: Set the timeout interval for routine failed, Please Retry."
            return 1
        else
            echo "Status: Success."
            echo "Set the timeout interval for routine success."
        fi
    fi
    return 0
}

# --------------------------  main  ------------------------------------------ #
G_CMD_PARAM=$*
case $1 in
    "-h")
        ROUTE_TIME_INTERVAL_SET_PRINT_HELP_INFO
        exit 0
        ;;
    "settimeoutinterval")
        ROUTE_TIME_INTERVAL_CHECK_AND_SET
        exit 0
        ;;
    *)
        echo "usage: fc_routineset.sh -h"
        exit 1
esac
ROUTE_TIME_INTERVAL_SET_PRINT_HELP_INFO
exit 0