#!/bin/sh
# $Id$
#
# netxmsd This script starts and stops the netxmsd daemon
#
# chkconfig: 2345 95 05
#
# description: netxmsd is a NetXMS server daemon process

NETXMS_BINDIR="/opt/netxms/bin"
NETXMS_LIBDIR="/opt/netxms/lib"

# Source function library.
if [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
else
	echo "Could not find functions file, your system may be broken."
	exit 1
fi

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

# Default config
NETXMSD_OPTIONS=""
NETXMSD_CONFIG="/etc/netxmsd.conf"
NETXMSD_PID_FILE="/var/run/netxmsd.pid"

# Source netxmsd configuration.
if [ -f /etc/sysconfig/netxmsd ] ; then
        . /etc/sysconfig/netxmsd
fi

[ -f $NETXMS_BINDIR/netxmsd ] || exit 0
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$NETXMS_LIBDIR

# See how we were called.
case "$1" in
  start)
	# Start daemon.
	echo -n "Starting netxmsd: "
	daemon $NETXMS_BINDIR/netxmsd -d $NETXMSD_OPTIONS --config $NETXMSD_CONFIG --pid-file $NETXMSD_PID_FILE
	RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/netxmsd
        ;;
  stop)
        # Stop daemons.
        echo -n "Shutting down netxmsd: "
        kill `cat $NETXMSD_PID_FILE 2>/dev/null` 2> /dev/null
        RETVAL=$?
        if [ $RETVAL = 0 ] ; then
                success
        else
                failure
        fi
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/netxmsd
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  condrestart)
       [ -e /var/lock/subsys/netxmsd ] && $0 restart
       ;;
  status)
	status netxmsd
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status|condrestart}"
	exit 1
esac

exit 0
