#!/bin/sh
#
# $FreeBSD: ports/net/rabbitmq/files/rabbitmq.in,v 1.9 2012/01/14 08:56:24 dougb Exp $
#
# PROVIDE: rabbitmq
# REQUIRE: DAEMON
# BEFORE: LOGIN
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf to enable RabbitMQ
#
#  rabbitmq_enable="YES"

. /etc/rc.subr

name=rabbitmq
rcvar=rabbitmq_enable

load_rc_config $name

# Set some defaults
: ${rabbitmq_enable="NO"}
: ${rabbitmq_user="rabbitmq"}

rabbitmq_server="/usr/local/sbin/rabbitmq-server"
rabbitmq_ctl="/usr/local/sbin/rabbitmqctl"
pidfile="/var/run/rabbitmq.pid"

start_cmd="${name}_start"
stop_cmd="env HOME=/var/db/rabbitmq su -m ${rabbitmq_user} -c 'sh -c \"${rabbitmq_ctl} stop\"'"
stop_postcmd="wait_for_pids $rc_pid"
rotate_logs_cmd="env HOME=/var/db/rabbitmq su -m ${rabbitmq_user} -c 'sh -c \"${rabbitmq_ctl} rotate_logs\"'"
status_cmd="env HOME=/var/db/rabbitmq su -m ${rabbitmq_user} -c 'sh -c \"${rabbitmq_ctl} status\"'"
extra_commands="rotate_logs status"

rabbitmq_start()
{
	HOME=/var/db/rabbitmq
	su -m ${rabbitmq_user} \
		-c "sh -c \"${rabbitmq_server} -detached\""
# Grab pid (it appears that parsing the output of
# status is the most reliable method)
	su -m ${rabbitmq_user} \
		-c "sh -c \"${rabbitmq_ctl} status\"" \
		| sed -n 's/[^p]*pid,\([0-9]*\)[^0-9]*/\1/p' > ${pidfile}
}

run_rc_command "$1"
