#!/bin/sh
#
# mongod		mongod
#
# chkconfig:	345 60 40
#
# description:	mongod is a NoSQL database daemon.
#
# processname:	mongod
# pidfile:      /var/run/mongod.pid
#
### BEGIN INIT INFO
# Provides:          mongod
# Required-Start: $syslog $local_fs $network
# Required-Stop:  $syslog $local_fs $network
# Should-Start:   $remote_fs
# Should-Stop:    $remote_fs
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: MongoDB server
# Description:       Starts and stops the MongoDB daemon
### END INIT INFO

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "mongod"
		exit 1
	fi
else
	exit 0
fi

MONGOD_BIN="/usr/bin/mongod"
MONGOD_PIDFILE="/var/run/mongod.pid"
MONGOD_CONFIG="/etc/sysconfig/mongod"

MONGOD_USER=mongod
MONGOD_GROUP=mongod

# Get service config
[ -f /etc/sysconfig/mongod ] && . /etc/sysconfig/mongod

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/mongod ]; then
		msg_already_running "mongod"
		return
	fi

	started=0
	for config in /etc/mongod/*.conf ; do
		instance=$(basename "$config" .conf)
		msg_starting "mongod '$instance' instance"
		if [ "$instance" = "default" ] ; then
			pidfile="$MONGOD_PIDFILE"
		else
			pidfile="${MONGOD_PIDFILE%.pid}-$instance.log"
		fi
		daemon --pidfile "$pidfile" --user $MONGOD_USER \
			$MONGOD_BIN --config "$config" run
		[ $? -eq 0 ] && started=$(($started + 1))
	done
	# at least one started - the service is running
	[ $started -eq 0 ] && RETVAL=1
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}

stop() {
	if [ ! -f /var/lock/subsys/mongod ]; then
		msg_not_running "mongod"
		return
	fi

	for config in /etc/mongod/*.conf ; do
		instance=$(basename "$config" .conf)
		msg_stopping "mongod '$instance' instance"
		if [ "$instance" = "default" ] ; then
			pidfile="$MONGOD_PIDFILE"
		else
			pidfile="${MONGOD_PIDFILE%.pid}-$instance.log"
		fi
		killproc --pidfile "$pidfile" mongod
	done
	rm -f /var/lock/subsys/mongod >/dev/null 2>&1
}

condrestart() {
	if [ ! -f /var/lock/subsys/mongod ]; then
		msg_not_running "mongod"
		RETVAL=$1
		return
	fi

	stop
	start
}

reload() {
	if [ ! -f /var/lock/subsys/mongod ]; then
		msg_not_running "mongod"
		RETVAL=7
		return
	fi

	for config in /etc/mongod/*.conf ; do
		instance=$(basename "$config" .conf)
		msg_reloading "mongod '$instance' instance"
		if [ "$instance" = "default" ] ; then
			pidfile="$MONGOD_PIDFILE"
		else
			pidfile="${MONGOD_PIDFILE%.pid}-$instance.log"
		fi
		killproc --pidfile "$pidfile" mongod -HUP
	done
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload)
  	reload
	;;
  status)
	status mongod
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
