#!/bin/sh

# $FreeBSD: databases/couchdb/files/couchdb.in 300896 2012-07-14 13:54:48Z beat $
#
# 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.

. /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"}"

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

	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"
