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

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