#!/bin/bash
#
# description: The functions of ESMPRO/ServerAgent.
#
TRUE=0
FALSE=1
FAILED=2

esmsa_checkpidfile(){
        local filepid
        local progpidlist
        local progpid

        if [ -f /var/run/$1.pid ] ; then
                filepid=`cat /var/run/$1.pid 2> /dev/null`
                progpidlist=`/sbin/pidof $1 2> /dev/null`

                if [ -n "${progpidlist:-}" ] ;  then
                        for progpid in $progpidlist ; do
                                if  [ "$progpid" = "$filepid" ] ; then
                                        return $TRUE
                                fi
                        done
                fi

                /bin/rm -f /var/run/$1.pid 2> /dev/null
		if [ -f /var/lock/subsys/$1 ] ; then
		    /bin/rm -f /var/lock/subsys/$1 2> /dev/null
		fi
                return $FAILED
        fi

        if [ -f /var/lock/subsys/$1 ] ; then
                /bin/rm -f /var/lock/subsys/$1 2> /dev/null
        fi
        return $FALSE
}

esmsa_start() {
        local rc=0
        echo -n $"Starting $1: "
        if [ $UID -ne 0 ]; then
                failure
                rc=1
        else
            esmsa_checkpidfile $"$1"
            if [ $? -eq $TRUE ]; then
            	/usr/bin/logger -t $1 "Daemon is already started!"
                success $"$1 startup"
    			rc=0
    		else
                daemon /opt/nec/esmpro_sa/bin/$1
                rc=$?
                [ $rc -eq 0 ] && /bin/touch /var/lock/subsys/$1
            fi
        fi
        echo
        return $rc
}

esmsa_stop() {
        local rc=0
        local rtn=0
        if [ $UID -ne 0 ]; then
		echo -n $"Stopping $1: "
                failure
                rc=1
		echo
        else
	        esmsa_checkpidfile $"$1"
	        rtn=$?
	        if [ $rtn -eq $TRUE ]; then
		    echo -n $"Stopping $1: "
                    #changed 20120724 for FPL3901 
		    #killproc $1
		    esmsa_killproc $"$1"
		    rc=$?
                if [ $rc -ne 0 ];then
                    sleep 30
                    #changed 20120724 for FPL3901 
                    #killproc $1 -KILL
                    esmsa_killproc $"$1" -KILL
                    rc=$?
                fi
                [ $rc -eq 0 ] && rm -f /var/lock/subsys/$1
        		echo
    		elif [ $rtn -eq $FAILED ]; then
                echo -n $"Stopping $1: "
    			/usr/bin/logger -t $1 "Daemon is not active!"
    			failure $"$1 shutdown"
                rc=1
                echo
			fi
        fi
        return $rc
}

esmsa_killall() {
        local pidofesmsa
        pidofesmsa=`pidofproc $1`
        if [ -n "${pidofesmsa:-}" ] ; then
                /usr/bin/killall -KILL $"/opt/nec/esmpro_sa/bin/$1"
        fi
}

esmsa_status(){
        esmsa_checkpidfile $"$1"
        status $1
        return $?
}

esmsa_killproc() {
    local notset=0
    local killlevel=
    local rc=1
    local delay=30

    if [ -n "$2" ]; then
        killlevel=$2
    else
        notset=1
    fi

    if [ "$notset" -eq "1" ] ; then
        if [ -n "`pidof $1`" ] ; then
            kill -TERM `pidof $1` >/dev/null 2>&1
        fi
	while [ $delay -gt 0 ] && [ -n "`pidof $1`" ] ; do
            sleep 1
            delay=$((delay - 1))
	done
        if [ -n "`pidof $1`" ] ; then
            kill -KILL `pidof $1` >/dev/null 2>&1
        fi
        [ -z "`pidof $1`" ] && success $"$1 shutdown" || failure $"$1 shutdown"
    else
        if [ -n "`pidof $1`" ] ; then
            kill $killlevel `pidof $1` >/dev/null 2>&1
            rc=$?
            [ "$rc" -eq 0 ] && success $"$1 $killlevel" || failure $"$1 $killlevel"
        fi
    fi
    if [ -z "`pidof $1`" ] ; then
        rc=0
        rm -f /var/run/$1.pid
    fi
    return $rc
}
