#!/bin/sh
# sphinx - This shell script takes care of starting and stopping
# Sphinx search daemon. It also has options to initialize Drupal
# index queue, build main and delta indexes.
#
# ********** WARNING **********
# This script may not work as-is on your environment.
# Please, use it only if you know what you are doing.
# ********** WARNING **********

###############
### WARNING ###
###############
echo "Edit, review and adjust script to suit your needs."
exit 1
###############
### WARNING ###
###############


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

#####################
### Program paths ###
#####################

BASE_PATH=/usr/local/sphinx
SEARCHD=${BASE_PATH}/bin/searchd
INDEXER=${BASE_PATH}/bin/indexer
CONFIG_FILE=${BASE_PATH}/etc/sphinx.conf
PID_FILE=${BASE_PATH}/var/log/searchd.pid
LOCK_FILE=/var/lock/subsys/searchd
PS=/bin/ps
HEAD=/usr/bin/head
GREP=/bin/grep
WGET=/usr/bin/wget
STAT=/usr/bin/stat


#################################
### Drupal site/index options ###
#################################

# Command to invoke index queue initialization.
# Needs to be executed before main index generation.
XMLPIPE_INIT=http://www.example.com/sphinx-xmlpipe/init

# Name of main and delta indexes.
MAIN_INDEX=index_drupal_main
DELTA_INDEX=index_drupal_delta

# .spd file stores matching document ID lists for each word ID
# If size if greater than 1, then delta can be merged into main.
DELTA_SPD=/usr/local/sphinx/var/data/index_drupal_delta.spd


#############################
### Script starts here... ###
#############################

RETVAL=0

#
# Start searchd daemon.
#
start() {
	echo -n $"Starting searchd: "
	daemon $SEARCHD --config $CONFIG_FILE
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && touch $LOCK_FILE
	return $RETVAL
}

#
# Stop searchd daemon.
#
stop() {
	echo -n $"Stopping searchd: "
	killproc searchd
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f $LOCK_FILE $PID_FILE
	return $RETVAL
}

#
# Display ps auxw for searchd daemon.
#
status() {
	if [ -f $LOCK_FILE ]; then
		$PS auxw | $HEAD -n 1
		$PS auxw | $GREP $SEARCHD | $GREP -v grep
		RETVAL=0
	else
		echo "Sphinx searchd is stopped."
		RETVAL=1
	fi
	return $RETVAL
}

#
# Initialize index queue and build main index.
# Stops and restarts searchd daemon if necessary.
#
build_main_index() {
	NEEDS_RESTART=0
	if [ -f $LOCK_FILE ]; then
		NEEDS_RESTART=1
		stop
	fi
	echo "Initializing site index queue..."
	$WGET -O - -q -t 1 $XMLPIPE_INIT
	echo "Building main index..."
	$INDEXER --config $CONFIG_FILE $MAIN_INDEX
	RETVAL=$?
	if [ $NEEDS_RESTART -eq 1 ]; then
		start
	fi
	return $RETVAL
}

#
# Build delta index and, if necessary, merges into main.
# Stops and restarts searchd daemon if necessary.
#
build_delta_index() {
	NEEDS_RESTART=0
	if [ -f $LOCK_FILE ]; then
		NEEDS_RESTART=1
		stop
	fi
	echo "Building delta index..."
	$INDEXER --config $CONFIG_FILE $DELTA_INDEX
	if [ $? -eq 0 ]; then
		DELTA_SPD_SIZE=`$STAT -c%s $DELTA_SPD`
		if [ $DELTA_SPD_SIZE -gt 1 ]; then
			$INDEXER --config $CONFIG_FILE --merge $MAIN_INDEX $DELTA_INDEX
		fi
	fi
	if [ $NEEDS_RESTART -eq 1 ]; then
		start
	fi
}


# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	status)
		status
		;;
	main)
		build_main_index
		;;
	delta)
		build_delta_index
		;;
	*)
		echo $"Usage: sphinx {start|stop|restart|status|main|delta}"
		echo "    start    - Start searchd daemon."
		echo "    stop     - Stop searchd daemon."
		echo "    restart  - Start and then stop searchd daemon."
		echo "    status   - Display ps auxw for searchd daemon."
		echo "    main     - Initialize index queue and build main index."
		echo "    delta    - Build delta index and, if necessary, merges into main."
		exit 1
esac

exit $RETVAL
