#!/bin/sh
#
# sphinx searchd Free open-source SQL full-text search engine
#
# chkconfig:	345 20 80
#
# description: Starts and stops the sphinx searchd daemon that handles \
#	       all search requests.
#
# processname:	searchd
#
# $Id$

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

# Get network config
. /etc/sysconfig/network

# Get service config - may override defaults
SEARCHD_CONFIG=/etc/sphinx/sphinx.conf
[ -f /etc/sysconfig/sphinx ] && . /etc/sysconfig/sphinx

# 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 "sphinx searchd"
		exit 1
	fi
else
	exit 0
fi

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

	msg_starting "sphinx searchd"
    daemon --user sphinx /usr/sbin/searchd --config $SEARCHD_CONFIG
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sphinx
}

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

	# Stop daemons.
	msg_stopping "sphinx searchd"
	killproc searchd
	rm -f /var/lock/subsys/sphinx
}

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

	msg_reloading "sphinx searchd"
	killproc searchd -HUP
	RETVAL=$?
}

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

	stop
	start
}

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 sphinx searchd
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
