#!/bin/sh
source /OSM/script/os_load_base.sh
G_DST_SCRIPT_FILE="upgrade_serious_reset_DTS2021102819381_SPH026.sh"
G_SRC_SCRIPT_FILE="upgrade_serious_reset_DTS2021102819381_SPH026.sh"
G_PATCH_ROOT_PATH="/OSM/coffer_bin/hotpatch"
WORK_DIR=$(cd "$(dirname "$0")";pwd)

# **************************************************************************** #
# Function Name: CHECK_REPLACE_SCRIPT
# Description: 判断并替换补丁目录下的脚本
# Parameter: 
# Return:
# **************************************************************************** #
CHECK_REPLACE_SCRIPT()
{
    local patch_dir_name=$1
    if [ ! -d "$G_PATCH_ROOT_PATH/$patch_dir_name" ]; then
        OS_echo "[$0] The patch_dir $G_PATCH_ROOT_PATH/$patch_dir_name is not exist." 
        return 0
    fi
    local replace_file_dir=$G_PATCH_ROOT_PATH/$patch_dir_name
    if [ ! -f "$replace_file_dir/$G_DST_SCRIPT_FILE" ]; then
        OS_echo "[$0] The file $replace_file_dir/$G_DST_SCRIPT_FILE is not exist." 
        return 0
    fi
    cp -f $WORK_DIR/$G_SRC_SCRIPT_FILE $replace_file_dir/$G_DST_SCRIPT_FILE 
    if [ "$?" != "0" ]; then
        OS_echo "[$0] cp the file failed." 
        return 1
    fi
    CHECKSUM_SYNC=$replace_file_dir/patch_sha256sum	
    sed -i "/$G_DST_SCRIPT_FILE/d" $CHECKSUM_SYNC
    if [ "$?" != "0" ]; then
        OS_echo "[$0] delete the sha256sum failed,retry once." 
        sleep 1
        sed -i "/$G_DST_SCRIPT_FILE/d" $CHECKSUM_SYNC		
        if [ "$?" != "0" ]; then
            OS_echo "[$0] delete the sha256sum failed."
            return 1
        fi
    fi
    return 0
}

PATCH_START()
{
    #上电恢复补丁时不处理 
    local pid=$(ps -ww -eo pid,cmd | grep "/OSM/bin/omm_upd_server.bin" | grep -v grep | awk '{print $1}')
    if [ "$pid" = "" ]; then
        OS_echo "[$0] It is repoweron, no need process."
        return 0
    fi 
    CHECK_REPLACE_SCRIPT "patch_cur"
    if [ "$?" != "0" ]; then
        OS_echo "[$0] replace the patch_cur dir failed."
        return 1
    fi
    CHECK_REPLACE_SCRIPT "patch_bak"
    if [ "$?" != "0" ]; then
        OS_echo "[$0] replace the patch_bak dir failed."
        return 1
    fi
    return 0
}


PATCH_END()
{
#add your code here
return
}

PATCH_ROLLBACK()
{
#add your code here
return
}

PATCH_ROLLEND()
{
#add your code here
return
}

L_CMD=$1
case $L_CMD in
    "start") PATCH_START
               ;;
    "rollstart") PATCH_ROLLBACK
               ;;
    *)
        OS_echo "unsupported option."
esac
exit 0