#!/bin/sh
#
# Copyright (c) 2016 Nagios(R) Core(TM) Development Team
#
# chkconfig: - 80 30
# description: Starts and stops the Nagios Data Out Daemon \
#              to store data in a database

#
### BEGIN INIT INFO
# Provides:           ndo2db
# Required-Start:     $local_fs $remote_fs $time
# Required-Stop:      $local_fs $remote_fs
# Should-Start:       $syslog $network
# Should-Stop:        $syslog $network
# Default-Start:      2 3 4 5
# Default-Stop:       0 1 6
# Short-Description:  Starts and stops the Nagios Data Out Daemon
# Description:        Starts and stops the Nagios Data Out Daemon
#                     to store data in a database
### END INIT INFO

export SYSTEMCTL_SKIP_REDIRECT=1

NDO2DB_BIN=@sbindir@/ndo2db
NDO2DB_CFG=@pkgsysconfdir@/ndo2db.cfg
LOCK_DIR=@subsyslockdir@
LOCK_FILE=@subsyslockfile@
PID_FILE=@piddir@/ndo2db.pid

test -x $NDO2DB_BIN || exit 5

RETVAL=0

# Default these commands/functions to RedHat/CentOS etc. values
MSG_CMD="echo -n"
START_CMD="daemon --pidfile $PID_FILE"
TERM_CMD="killproc -p $PID_FILE $NDO2DB_BIN -TERM"
HUP_CMD="killproc -p $PID_FILE $NDO2DB_BIN -HUP"
PRT_STAT="echo"
STAT_MSG="echo -n Checking for ndo2db daemon... "
STAT_CMD="status ndo2db"
EXIT_CMD="exit"

_set_rc (){ return; }

# Source the function library
if [ -f /etc/rc.status ]; then

	. /etc/rc.status

	_set_rc (){ return $RETVAL; }

	# Set these commands/functions to SuSE etc. values
	START_CMD="startproc -p $PID_FILE"
	TERM_CMD="killproc -p $PID_FILE -TERM $NDO2DB_BIN"
	HUP_CMD="killproc -p $PID_FILE -HUP $NDO2DB_BIN"
	PRT_STAT="rc_status -v -r"
	STAT_CMD="checkproc -p $PID_FILE $NDO2DB_BIN"
	EXIT_CMD="rc_exit"
	rc_reset

elif [ -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

elif [ -f /lib/lsb/init-functions ]; then

	. /lib/lsb/init-functions

	MSG_CMD="log_daemon_msg"
	START_CMD="start_daemon -p $PID_FILE"
	PRT_STAT="log_end_msg"
	STAT_MSG=
	STAT_CMD="status_of_proc -p $PID_FILE $NDO2DB_BIN ndo2db"

elif [ -f /etc/rc.d/functions ]; then

	. /etc/rc.d/functions

fi


# See how we were called.
case "$1" in

start)
	# Start daemons.
	$MSG_CMD "Starting ndo2db "
	$START_CMD $NDO2DB_BIN -c $NDO2DB_CFG
	RETVAL=$?
	if test "$PRT_STAT" = log_end_msg; then
		$PRT_STAT $RETVAL
	else
		_set_rc; $PRT_STAT
	fi
	if [ $RETVAL = 0 ]; then
		[ -d $LOCK_DIR ] && touch $LOCK_FILE || true
	fi
	;;

stop)
	# Stop daemons.
	$MSG_CMD "Shutting down ndo2db "
	$TERM_CMD
	RETVAL=$?
	if test "$PRT_STAT" = log_end_msg; then
		$PRT_STAT $RETVAL
	else
		_set_rc; $PRT_STAT
	fi
	if [ $RETVAL = 0 ]; then
		[ -d $LOCK_DIR ] && rm -f $LOCK_FILE
	fi
	;;

restart|force-reload)
	$0 stop
	$0 start
	RETVAL=$?
	;;

try-restart|condrestart)
	$STAT_CMD || exit 0
	$0 stop
	$0 start
	RETVAL=$?
	;;

status)
	$STAT_MSG
	$STAT_CMD
	RETVAL=$?
	if test "$PRT_STAT" != log_end_msg; then
		_set_rc; $PRT_STAT
	fi
	;;

*)
	echo "Usage: ndo2db {start|stop|restart|try-restart|condrestart|status}"
	exit 1
esac

$EXIT_CMD $RETVAL
