#!/bin/sh
#
# $NetBSD: munin-node.sh,v 1.4 2006/12/29 06:37:03 obache Exp $
#
# PROVIDE: munin-node
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# You will need to set some variables in /etc/rc.conf to start munin-node:
#
# munin_node=YES
#

if [ -f /etc/rc.subr ]
then
    . /etc/rc.subr
fi

name="munin_node"
rcvar=$name
command=/usr/pkg/sbin/munin-node
command_interpreter=/usr/pkg/bin/perl
pidfile="/var/run/munin/munin-node.pid"
required_files=/usr/pkg/etc/munin/munin-node.conf

stop_cmd="munin_node_stop"

# Net::Server breaks rc.subr's techniques for detecting whether the
# process running at a certain PID is actually the process we wish to
# stop.  Just unconditionally send SIGTERM to the PID instead.
#
munin_node_stop()
{
	echo "Stopping ${name}."
	if [ -f ${pidfile} ]; then
		pid=`/usr/bin/head -1 ${pidfile}`
		doit="kill ${pid}"
		if ! eval $doit && [ -z "$rc_force" ]; then
			return 1
		fi
		wait_for_pids $pid
	fi
	/bin/rm -f ${pidfile}
}

if [ ! -d /var/munin/plugin-state ]; then
	mkdir /var/munin/plugin-state
	chown munin:munin /var/munin/plugin-state
fi
if [ ! -d /var/run/munin ]; then
	mkdir /var/run/munin
	chown munin:munin /var/run/munin
fi

if [ -f /etc/rc.subr ]; then
	load_rc_config $name
	run_rc_command "$1"
else
	echo -n " ${name}"
	exec ${command} ${command_args}
fi
