#!/bin/bash
#
# BigCouch
#
# chkconfig: 345 13 87
# description: BigCouch is a dynamo-style distributed database based on Apache CouchDB.
# processname: bigcouch
# pidfile: /var/run/bigcouch/kazoo-bigcouch.pid
#

RETVAL=0
LOCK_FILE=${LOCK_FILE:-/var/lock/subsys/kazoo-bigcouch}

. /etc/init.d/functions

start() {
	echo -n $"Starting BigCouch: "
	/usr/sbin/kazoo-bigcouch prepare >/dev/null 2>&1
	daemon "/usr/sbin/kazoo-bigcouch background >/dev/null"
	RETVAL=$?
	if [ ${RETVAL} -eq 0 ]; then
		touch ${LOCK_FILE}
		success $"OK"
		echo
	else
		failure $"Bigcouch is already running or failed to start!"
		echo
	fi
}

stop() {
	echo -n $"Stopping BigCouch: "
	/usr/sbin/kazoo-bigcouch stop >/dev/null 2>&1
	RETVAL=$?
	if [ ${RETVAL} -eq 0 ]; then
		rm -f ${LOCK_FILE} ${pidfile}
		success $"OK"
		echo
	else 
		failure $"Bigcouch is still running!"
		echo
	fi
}

status() {
	/usr/sbin/kazoo-bigcouch status
	RETVAL=$?
}

restart() {
	stop
	start
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart|reload)
		restart
		;;
	condrestart)
		[ ! -e ${LOCK_FILE} ] && restart
		;;
	*)
		echo $"Usage: $0 (start|stop|restart|status)"
		RETVAL=1
esac

exit ${RETVAL}
