#!/bin/sh
source /OSM/script/os_load_base.sh
PATCH_START()
{
    return;
}
PATCH_END()
{
	#新增判断 如果文件大小为0，则拷贝基线进程过来。解决小补丁大小超过1M限制方案。
	if [ -f "/OSM/bin/cmr_kagent" ]; then
		local FILE_SIZE=`du -sb /OSM/bin/cmr_kagent | awk '{print $1}'`
		if [ $FILE_SIZE -eq 0 ]; then
			cp /OSM/bin/cmr_kagent_origin /OSM/bin/cmr_kagent
			OS_echo "cmr_kagent: The cmr_kagent size is $FILE_SIZE."
			return 0
		fi
		OS_echo "cmr_kagent: The cmr_kagent size is $FILE_SIZE."
	fi
	
    chmod 775 /OSM/bin/cmr_kagent
	mv /OSM/bin/cmr_kagent /OSM/bin/cmr_kagent.tgz
	tar zxf /OSM/bin/cmr_kagent.tgz -C /OSM/bin
	chmod 500 /OSM/bin/cmr_kagent
	rm -f /OSM/bin/cmr_kagent.tgz

    ProcessNum=$(ps -ef | grep -v grep | grep cmr_kagent | wc -l)
    if [ $ProcessNum -eq 1 ] ;then
		if [ -f /var/run/proto_cmr_kagent.pid ];then
           sudo rm /var/run/proto_cmr_kagent.pid 
	   OS_echo "del kagent.pid"
        fi
        pid=`pidof cmr_kagent`
        kill -9 $pid 
        cnt=0
        while [ $cnt -lt 3 ]
        do
            cnt=`expr $cnt + 1`
            #ProcessNum=$(ps -ef | grep -v grep | grep cmr_kagent | wc -l)
			pid2=`pidof cmr_kagent`
            if [ $pid -eq $pid2 ] ;then
                OS_echo "kill cmr_kagent failed in SPH050, cnt is $cnt."
                sleep 1
            else
                OS_echo "hotpatch need kill cmr_kagent in SPH050."
                cnt=3
            fi
        done
    else
        OS_echo "hotpatch need not kill cmr_kagent in SPH050"
    fi
}
PATCH_ROLLBACK()
{
    return;
}
PATCH_ROLLEND()
{
	#新增判断 如果文件大小为0，则拷贝基线进程过来。解决小补丁大小超过1M限制方案。
	if [ -f "/OSM/bin/cmr_kagent" ]; then
		local FILE_SIZE=`du -sb /OSM/bin/cmr_kagent | awk '{print $1}'`
		if [ $FILE_SIZE -eq 0 ]; then
			cp /OSM/bin/cmr_kagent_origin /OSM/bin/cmr_kagent
			OS_echo "cmr_kagent: The cmr_kagent size is $FILE_SIZE."
			return 0
		fi
		OS_echo "cmr_kagent: The cmr_kagent size is $FILE_SIZE."
	fi
	
    chmod 775 /OSM/bin/cmr_kagent
	
    tar zxf /OSM/bin/cmr_kagent -C /OSM/bin 1> /dev/null 2>&1	
	if [ $? -eq 0 ] ;then
	    OS_echo "Tar zipped cmr_kagent success."
	fi	
	
    chmod 500 /OSM/bin/cmr_kagent
	
    ProcessNum=$(ps -ef | grep -v grep | grep cmr_kagent | wc -l)
    if [ $ProcessNum -eq 1 ] ;then
        if [ -f /var/run/proto_cmr_kagent.pid ];then
           sudo rm /var/run/proto_cmr_kagent.pid 
	   OS_echo "del kagent.pid"
        fi
        pid=`pidof cmr_kagent`
        kill -9 $pid 
        cnt=0
        while [ $cnt -lt 3 ]
        do
            cnt=`expr $cnt + 1`
            #ProcessNum=$(ps -ef | grep -v grep | grep cmr_kagent | wc -l)
			pid2=`pidof cmr_kagent`
            if [ $pid -eq $pid2 ] ;then
                OS_echo "kill cmr_kagent failed in SPH050, cnt is $cnt."
                sleep 1
            else
                OS_echo "hotpatch need kill cmr_kagent in SPH050."
                cnt=3
            fi
        done
    else
        OS_echo "hotpatch need not kill cmr_kagent in SPH050"
    fi
}
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