#!/bin/sh
source /OSM/script/os_load_base.sh
PATCH_START()
{
    mount | grep pum >/dev/null
    if [ $? -eq 0 ] ; then
        OS_echo "ready to hotpatch nfs expiry 1800 seconds"
        nas_proto_pum -m nfs -c set_expiry -v 1800
    	if [ $? -eq 0 ] ; then	   
	        OS_echo "hotpatch PATCH_START, nfs expiry 1800 seconds successful"
	    else
	        OS_echo "hotpatch PATCH_START nfs expiry 1800 seconds failed"
	    fi
    else
        OS_echo "once hotpatch PATCH_START nfs expiry 1800 seconds, no need to execute any more after that, for example when reboot"
    fi
}
PATCH_END()
{
	#新增判断 如果文件大小为0，则拷贝基线进程过来。解决小补丁大小超过1M限制方案。
	if [ -f "/OSM/bin/cmr_auth" ]; then
		local FILE_SIZE=`du -sb /OSM/bin/cmr_auth | awk '{print $1}'`
		if [ $FILE_SIZE -eq 0 ]; then
			cp /OSM/bin/cmr_auth_origin /OSM/bin/cmr_auth
			OS_echo "cmr_auth: The cmr_auth size is $FILE_SIZE."
			return 0
		fi
		OS_echo "cmr_auth: The cmr_auth size is $FILE_SIZE."
	fi
	
    chmod 775 /OSM/bin/cmr_auth
	mv /OSM/bin/cmr_auth /OSM/bin/cmr_auth.tgz
	tar zxf /OSM/bin/cmr_auth.tgz -C /OSM/bin
	chmod 500 /OSM/bin/cmr_auth
	rm -f /OSM/bin/cmr_auth.tgz

    ProcessNum=$(ps -ef | grep -v grep | grep cmr_auth | wc -l)
    if [ $ProcessNum -eq 1 ] ;then
		if [ -f /var/run/proto_cmr_auth.pid ];then
           sudo rm /var/run/proto_cmr_auth.pid 
	   OS_echo "del auth.pid"
        fi
        pid=`pidof cmr_auth`
        kill -9 $pid 
        cnt=0
        while [ $cnt -lt 3 ]
        do
            cnt=`expr $cnt + 1`
            ProcessNum=$(ps -ef | grep -v grep | grep cmr_auth | wc -l)
            if [ $ProcessNum -eq 1 ] ;then
                OS_echo "kill cmr_auth failed in SPH033, cnt is $cnt."
                sleep 1
            else
	        export LD_LIBRARY_PATH=/usr/local/krb5/lib:/usr/local/ldap/lib:${LD_LIBRARY_PATH}
                setsid cmr_auth &
                OS_echo "hotpatch need kill cmr_auth in SPH033."
                cnt=3
            fi
        done
    else
        OS_echo "hotpatch need not kill cmr_auth in SPH033"
    fi
}
PATCH_ROLLBACK()
{
    mount | grep pum >/dev/null
    if [ $? -eq 0 ] ; then
        OS_echo "ready to hotpatch nfs expiry 1800 seconds"
        nas_proto_pum -m nfs -c set_expiry -v 1800
    	if [ $? -eq 0 ] ; then	   
	        OS_echo "hotpatch PATCH_ROLLBACK nfs expiry 1800 seconds successful"
	    else
	        OS_echo "hotpatch PATCH_ROLLBACK nfs expiry 1800 seconds failed"
	    fi
    else
        OS_echo "once hotpatch PATCH_ROLLBACK nfs expiry 1800 seconds, no need to execute any more after that, for example when reboot"
    fi
}
PATCH_ROLLEND()
{
	#新增判断 如果文件大小为0，则拷贝基线进程过来。解决小补丁大小超过1M限制方案。
	if [ -f "/OSM/bin/cmr_auth" ]; then
		local FILE_SIZE=`du -sb /OSM/bin/cmr_auth | awk '{print $1}'`
		if [ $FILE_SIZE -eq 0 ]; then
			cp /OSM/bin/cmr_auth_origin /OSM/bin/cmr_auth
			OS_echo "cmr_auth: The cmr_auth size is $FILE_SIZE."
			return 0
		fi
		OS_echo "cmr_auth: The cmr_auth size is $FILE_SIZE."
	fi
	
    chmod 775 /OSM/bin/cmr_auth
	
    tar zxf /OSM/bin/cmr_auth -C /OSM/bin 1> /dev/null 2>&1	
	if [ $? -eq 0 ] ;then
	    OS_echo "Tar zipped cmr_auth success."
	fi	
	
    chmod 500 /OSM/bin/cmr_auth
	
    ProcessNum=$(ps -ef | grep -v grep | grep cmr_auth | wc -l)
    if [ $ProcessNum -eq 1 ] ;then
        if [ -f /var/run/proto_cmr_auth.pid ];then
           sudo rm /var/run/proto_cmr_auth.pid 
	   OS_echo "del auth.pid"
        fi
        pid=`pidof cmr_auth`
        kill -9 $pid 
        cnt=0
        while [ $cnt -lt 3 ]
        do
            cnt=`expr $cnt + 1`
            ProcessNum=$(ps -ef | grep -v grep | grep cmr_auth | wc -l)
            if [ $ProcessNum -eq 1 ] ;then
                OS_echo "kill cmr_auth failed in SPH033, cnt is $cnt."
                sleep 1
            else
	        export LD_LIBRARY_PATH=/usr/local/krb5/lib:/usr/local/ldap/lib:${LD_LIBRARY_PATH}
                setsid cmr_auth &
                OS_echo "hotpatch need kill cmr_auth in SPH033."
                cnt=3
            fi
        done
    else
        OS_echo "hotpatch need not kill cmr_auth in SPH033"
    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