#!/bin/sh

# PROVIDE: stanchion
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable stanchion:
# stanchion_enable (bool):		Set to "NO" by default.
#				Set it to "YES" to enable stanchion on boot.
#

. /etc/rc.subr

name="stanchion"
rcvar=stanchion_enable

pidfile=/var/run/stanchion/stanchion.pid

start_cmd="stanchion_start"
stop_cmd="stanchion_stop"
restart_cdm="stanchion_restart"
status_cmd="stanchion_status"
command="/usr/local/sbin/stanchion"

load_rc_config $name

# Read rc.d config and set defaults
load_rc_config "$name"
: ${stanchion_enable="NO"}

stanchion_start()
{
	echo "Starting Riak."
	/usr/local/sbin/stanchion start
	return 0
}

stanchion_stop()
{
	echo "Stopping Riak processes"
	/usr/local/sbin/stanchion stop
	killall -9 epmd
	return 0
}

stanchion_restart()
{
	stanchion_stop
	stanchion_start
	return 0
}

stanchion_status()
{
	if stanchion_running; then
		echo "Riak is running."
		return 0
	else
		echo "Riak is not running"
		return 1
	fi
}

stanchion_running()
{
	local pid result ps
	pid=`/usr/local/sbin/stanchion getpid`
	result=`echo $?`
	if [ "$result" == 0 ]; then
		ps=`ps -waux | grep ${pid} | grep stanchion`
		result=`echo $?`
		if [ "$result" ]; then
			return 0
		else
			return 1
		fi
	else
		return 1
	fi
}

run_rc_command "$1"
