#!@SHELL@
# 
### BEGIN INIT INFO
# Provides:          npcd
# Required-Start:    
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: @PKG_NAME@ NPCD Daemon Version @PKG_VERSION@
# Description:          Nagios Performance Data C Daemon
### END INIT INFO

# chkconfig: 345 99 01
#
# File : npcd
#
  
servicename=@npcd_name@
prefix=@prefix@
exec_prefix=${prefix}
NpcdBin=@bindir@/@npcd_name@
NpcdCfgFile=@sysconfdir@/npcd.cfg
NpcdVarDir=@localstatedir@
NpcdRunFile=/var/run/npcd.pid
NpcdLockDir=/var/lock/subsys
NpcdLockFile=@npcd_name@
NpcdUser=@nagios_user@
NpcdGroup=@nagios_grp@

status_npcd (){
    pid_npcd
    if ps -p $NpcdPID > /dev/null 2>&1; then
        return 0
    else
        if test -f $NpcdLockDir/$NpcdLockFile; then
            return 2
        else
            return 1
        fi
    fi
    return 1
}

printstatus_npcd(){
    if status_npcd $1 $2; then
        echo "$servicename (pid $NpcdPID) is running..."
        exit 0
    elif test $? -eq 2; then
        echo "$servicename is not running but subsystem locked"
        exit 2
    else
        echo "$servicename is not running"
        exit 1
    fi
}

killproc_npcd (){
    kill $2 $NpcdPID
}

pid_npcd (){
    if test ! -f $NpcdRunFile; then
        return 1
    fi
    NpcdPID=`head -n 1 $NpcdRunFile`
    return 0
}


# Source function library
# Solaris doesn't have an rc.d directory, so do a test first
if [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
fi

# Check that npcd exists.
if [ ! -f $NpcdBin ]; then
    echo "Executable file $NpcdBin not found.  Exiting."
    exit 1
fi

# Check that npcd.cfg exists.
if [ ! -f $NpcdCfgFile ]; then
    echo "Configuration file $NpcdCfgFile not found.  Exiting."
    exit 1
fi
          
# See how we were called.
case "$1" in

    start)
        status_npcd
        if [ $? -eq 0 ]; then
            echo "$servicename already started..."
            exit 1
        fi
        echo -n "Starting $servicename:"
        touch $NpcdRunFile
        chown $NpcdUser:$NpcdGroup $NpcdRunFile
        $NpcdBin -d -f $NpcdCfgFile
        if [ -d $NpcdLockDir ]; then touch $NpcdLockDir/$NpcdLockFile; fi
        echo " done."
        exit 0
    ;;

    stop)
        status_npcd
        if ! [ $? -eq 0 ]; then
            echo "$servicename was not running... could not stop"
            exit 1
        fi
        echo -n "Stopping $servicename: "

        pid_npcd
        killproc_npcd npcd

        # now we have to wait for npcd to exit and remove its
        # own NpcdRunFile, otherwise a following "start" could
        # happen, and then the exiting npcd will remove the
        # new NpcdRunFile, allowing multiple npcd daemons
        # to (sooner or later) run - John Sellens
        #echo -n 'Waiting for npcd to exit .'
        for i in 1 2 3 4 5 6 7 8 9 10 ; do
            if status_npcd > /dev/null; then
                echo -n '.'
                sleep 1
                else
                break
            fi
        done
        if status_npcd > /dev/null; then
             echo ''
             echo 'Warning - $servicename did not exit in a timely manner'
        else
             echo 'done.'
        fi
        rm -f $NpcdLockDir/$NpcdLockFile 
    ;;

    status)
        printstatus_npcd
    ;;

    reload)
        $0 restart
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    *)
        echo "Usage: $servicename {start|stop|restart|status}"
        exit 1
    ;;

esac
  
# End of this script
