#!/bin/bash
#例测超时时间设置预处理脚本 --- 采用extend.ini做持久化配置
#预处理脚本实现功能：
###1.打补丁时写白名单，确保minisystem下能调用开关功能脚本
###2.卸载补丁需要恢复原白名单
 
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_routineset.sh')
        if [ -n "$tmpResult" ]; then
            echo "routineset cmd is in cmd list."
        else
            sed -i "/upgrade_clearflag.sh/a\fc_routineset.sh" "/$f"
        fi
        fi
    done
    
    echo "[$0] enable set routine cmd successfully, ret $?."

    sed -i 's/\/OSM\/script\/cli_server_switch_cfg\.sh/\/OSM\/script\/cli_server_switch_cfg\.sh,\/OSM\/script\/fc_routineset\.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_routineset\.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_routineset.sh/d" "/$f"
        fi
    done
    
    echo "[$0] disable set delayrspswitch cmd successfully, ret $?."

    sed -i 's/,\/OSM\/script\/fc_routineset\.sh//g' $MIMISYSTEM_CONF1
    
    sed -i 's/,\/OSM\/script\/fc_routineset\.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