#!/bin/sh

# $FreeBSD: head/databases/couchdb/files/couchdb.in 379802 2015-02-24 14:56:22Z olgeni $
#
# PROVIDE: couchdb
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# couchdb_enable (bool):	Set to NO by default.
# 				Set it to YES to enable couchdb.
#
# couchdb_enablelogs (bool):	Set to YES by default.
#
# couchdb_etcdir (string):	In case you want another dir
# 				for default.ini/local.ini.
#
# couchdb_respawn (int):	Set to none by default. If CouchDB crashes,
# 				respawn after this many seconds.
#
# couchdb_erl_flags (string):	Set to none by default. Additional Erlang flags
# 				that are appended via ERL_ZFLAGS to the Erlang VM parameters.

. /etc/rc.subr

name="couchdb"
rcvar=couchdb_enable

load_rc_config $name

: ${couchdb_enable:="NO"}
couchdb_user="${couchdb_user:-"couchdb"}"
couchdb_enablelogs="${couchdb_enablelogs:-"YES"}"
couchdb_etcdir="${couchdb_etcdir:-"/usr/local/etc/couchdb"}"
couchdb_respawn="${couchdb_respawn:-"0"}"
couchdb_erl_flags="${couchdb_erl_flags:-""}"

command="/usr/local/bin/${name}"
pidfile="/var/run/${name}/${name}.pid"
urifile="/var/run/${name}/couch.uri"

command_args="-p ${pidfile} -b"

couchdb_prestart()
{
	install -o $couchdb_user /dev/null $pidfile
	install -o $couchdb_user /dev/null $urifile

	if [ -n "$couchdb_flags" ]; then
		case "$couchdb_flags" in
		*-[bp]*) err 1 'The -b and -p options should be removed from couchdb_flags' ;;
		esac
		return 0
	fi

	if [ $couchdb_respawn -gt 0 ]; then
		respawn="-r ${couchdb_respawn}"
	fi

	if checkyesno couchdb_enablelogs; then
		logfile=/var/log/${name}/couch.log
		errfile=/var/log/${name}/err.log
	else
		logfile=/dev/null
		errfile=/dev/null
	fi

	# couchdb supports passing options through to the erlang vm directly in
	# ERL_ZFLAGS which is appended to CouchDB own flags.
	ERL_ZFLAGS=${couchdb_erl_flags}
	export ERL_ZFLAGS

	command_args="-a ${couchdb_etcdir}/default.ini -a ${couchdb_etcdir}/local.ini ${respawn} -o ${logfile} -e ${errfile} ${command_args}"
}

start_precmd=${name}_prestart
stop_cmd="${command} -d"
status_cmd="${command} -s"

run_rc_command "$1"
