#!/sbin/openrc-run

pidfile=/run/rabbitmq/rabbitmq.pid
name=rabbitmq
daemon=/usr/sbin/rabbitmq-server

startup_log=/var/log/rabbitmq/startup_log
startup_err=/var/log/rabbitmq/startup_err
shutdown_log=/var/log/rabbitmq/shutdown_log
shutdown_err=/var/log/rabbitmq/shutdown_err

depend() {
	need net localmount
	after firewall
}

start() {
	ebegin "Starting ${name}"
	# We need to test if /var/run/rabbitmq exists, since scirpt will fail if it does not
	checkpath -m 700 -o rabbitmq:rabbitmq -d "$(dirname "$pidfile")"
	checkpath -m 644 -o rabbitmq:rabbitmq --file-truncate -f $startup_log
	checkpath -m 644 -o rabbitmq:rabbitmq --file-truncate -f $startup_err
	start-stop-daemon --start \
		--env RABBITMQ_PID_FILE=$pidfile \
		--pidfile $pidfile \
		--stdout $startup_log \
		--stderr $startup_err \
		--background $daemon
	eend $?
}

stop() {
	ebegin "Stopping ${name}"
	checkpath -m 644 -o rabbitmq:rabbitmq --file-truncate -f $shutdown_log
	checkpath -m 644 -o rabbitmq:rabbitmq --file-truncate -f $shutdown_err
	start-stop-daemon --stop --quiet --pidfile $pidfile \
		--exec /bin/sh -- -c "/usr/sbin/rabbitmqctl \
		--stop $pidfile > $shutdown_log \
		2> $shutdown_err"
	eend $?
}

