#!/bin/bash

source /OSM/script/os_load_base.sh

function PATCH_START()
{
    return 
}
function PATCH_END()
{
    #通过ELF格式区分二进制文件与压缩文件，回显/OSM/bin/upgrade_atom: ELF 64-bit LSB executable, x86-64代表为二进制文件
	#停止DHA
	sudo daemon -s /OSM/bin/dha_scheduler
    local red=$(file /OSM/bin/dha_scheduler | awk '{print $2}')	
    if [ "$red" != "ELF" ]; then
		chmod 770 /OSM/bin/dha_scheduler
		mv /OSM/bin/dha_scheduler /OSM/bin/dha_scheduler.tgz
        tar -xzf /OSM/bin/dha_scheduler.tgz -C /OSM/bin
        chmod 770 /OSM/bin/dha_scheduler
        rm -f /OSM/bin/dha_scheduler.tgz
        OS_echo "UPGRADE_SH: finish to decompress and replace dha_scheduler"
    else
        OS_echo "UPGRADE_SH: no need to decompress and replace dha_scheduler"
    fi
	#重启DHA
	sudo daemon "/OSM/bin/dha_scheduler" "/OSM/bin/cgroup_manage.sh -g dha"
	
    return
}
function PATCH_ROLLSTART()
{
    return
}
function PATCH_ROLLEND()
{
    #通过ELF格式区分二进制文件与压缩文件，回显/OSM/bin/upgrade_atom: ELF 64-bit LSB executable, x86-64代表为二进制文件
	#停止DHA
	sudo daemon -s /OSM/bin/dha_scheduler
    local red=$(file /OSM/bin/dha_scheduler | awk '{print $2}')	
    if [ "$red" != "ELF" ]; then
        chmod 770 /OSM/bin/dha_scheduler
        mv /OSM/bin/dha_scheduler /OSM/bin/dha_scheduler.tgz
        tar -xzf /OSM/bin/dha_scheduler.tgz -C /OSM/bin
        chmod 770 /OSM/bin/dha_scheduler
        rm -f /OSM/bin/dha_scheduler.tgz
        OS_echo "UPGRADE_SH: finish to decompress and replace dha_scheduler"
    else
        OS_echo "UPGRADE_SH: no need to decompress and replace dha_scheduler"
    fi
	#重启DHA
	sudo daemon "/OSM/bin/dha_scheduler" "/OSM/bin/cgroup_manage.sh -g dha"
	
    return
}
#start：打补丁前需要执行的操作，脚本的执行时间点在替换文件前
#end：打补丁后需要执行的操作，脚本的执行时间点在替换文件后
#rollstart：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之前。
#rollend：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之后。

L_CMD=$1
OS_echo "command is $L_CMD"
case $L_CMD in
        "start") 
                PATCH_START
                ;;
        "end") 
                PATCH_END
                ;;
        "rollstart") 
                PATCH_ROLLSTART
                ;;
        "rollend") 
                PATCH_ROLLEND
                ;;
        *)
                OS_echo  "unsupported option."
esac
exit 0

