#!/bin/sh
source /OSM/script/os_load_base.sh
PATCH_START()
{
    return;
}
PATCH_END()
{
    chmod 775 /OSM/bin/cmr_cm
	mv /OSM/bin/cmr_cm /OSM/bin/cmr_cm.tgz
	tar zxf /OSM/bin/cmr_cm.tgz -C /OSM/bin
	chmod 500 /OSM/bin/cmr_cm
	rm -f /OSM/bin/cmr_cm.tgz

    ProcessNum=$(ps -ef | grep -v grep | grep cmr_cm | wc -l)
    if [ $ProcessNum -eq 1 ] ;then
		if [ -f /var/run/proto_cmr_cm.pid ];then
           sudo rm /var/run/proto_cmr_cm.pid 
	   OS_echo "del cm.pid"
        fi
        pid=`pidof cmr_cm`
        kill -9 $pid 
        cnt=0
        while [ $cnt -lt 3 ]
        do
            cnt=`expr $cnt + 1`
            ProcessNum=$(ps -ef | grep -v grep | grep cmr_cm | wc -l)
            if [ $ProcessNum -eq 1 ] ;then
                OS_echo "kill cmr_cm failed in sph009, cnt is $cnt."
                sleep 1
            else
                setsid cmr_cm &
                OS_echo "hotpatch need kill cmr_cm in sph009."
                cnt=3
            fi
        done
    else
        OS_echo "hotpatch need not kill cmr_cm in sph009"
    fi
}
PATCH_ROLLBACK()
{
    return;
}
PATCH_ROLLEND()
{
    chmod 775 /OSM/bin/cmr_cm
	
    tar zxf /OSM/bin/cmr_cm -C /OSM/bin 1> /dev/null 2>&1	
	if [ $? -eq 0 ] ;then
	    OS_echo "Tar zipped cmr_cm success."
	fi	
	
    chmod 500 /OSM/bin/cmr_cm
	
    ProcessNum=$(ps -ef | grep -v grep | grep cmr_cm | wc -l)
    if [ $ProcessNum -eq 1 ] ;then
        if [ -f /var/run/proto_cmr_cm.pid ];then
           sudo rm /var/run/proto_cmr_cm.pid 
	   OS_echo "del cm.pid"
        fi
        pid=`pidof cmr_cm`
        kill -9 $pid 
        cnt=0
        while [ $cnt -lt 3 ]
        do
            cnt=`expr $cnt + 1`
            ProcessNum=$(ps -ef | grep -v grep | grep cmr_cm | wc -l)
            if [ $ProcessNum -eq 1 ] ;then
                OS_echo "kill cmr_cm failed in sph009, cnt is $cnt."
                sleep 1
            else
	        setsid cmr_cm &
                OS_echo "hotpatch need kill cmr_cm in sph009."
                cnt=3
            fi
        done
    else
        OS_echo "hotpatch need not kill cmr_cm in sph009"
    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