#!/bin/bash

PATCH_FILE_LIST=("usr/local/etc/os_debug_mode_cmd_list")
MIMISYSTEM_CONF1=("/OSM/conf/sudoers.ini")
MIMISYSTEM_CONF2=("/etc/sudoers")
PATCH_START()
{

    # backup and replace config file
    
    for f in ${PATCH_FILE_LIST[*]}
    do
        if [ -f "/$f" ]; then
        tmpResult=$(cat /$f |grep 'set_fdsa_sysdisk_switch.sh')
        if [ -n "$tmpResult" ]; then
            OS_echo "set fdsa_sysdisk_switch cmd is in cmd list."
        else
            sed -i "/upgrade_clearflag.sh/a\set_fdsa_sysdisk_switch.sh" "/$f"
        fi
        fi
    done
    
    OS_echo "[$0] enable set fdsa_sysdisk_switch cmd successfully, ret $?."

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

    OS_echo "edit conf1, ret $?."


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


    OS_echo "edit conf2, ret $?."
    


    return 0
}

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

PATCH_ROLLBACK()
{
    # delete switch file which generate when last patch
    rm -rf /OSM/coffer_log/log/heal/data/fdsa_sysdisk_switch
    
    for f in ${PATCH_FILE_LIST[*]}
    do
        if [ -f "/${f}" ]; then
        sed -i "/set_fdsa_sysdisk_switch.sh/d" "/$f"
        fi
    done
    
    OS_echo "[$0] disable set netboot cmd successfully, ret $?."

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

    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
               ;;
    *)
    OS_echo "unsupported option."
esac
exit 0

