#!/bin/bash
#
# Startup script for the Harvest Broker
#
# chkconfig: 345 86 14
# description: HARVEST is an integrated set of tools to gather, extract,
#	       organize, and search information across the Internet.
#	       The BROKER retrieves and manages indexing information from
#	       Gatherers and other Brokers, and provides a WWW query interface
#	       to the indexing information.
#
# processname: broker
# pidfile: /var/run/broker.pid
# config: $HARVEST_HOME/Brokers/Brokers.cf
#
# TODO: - run more than 1 broker
#

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

# Path to where you install harvest
HARVEST_HOME="/opt/harvest"
HARVEST_USER="apache"

## Do not change after this line ############
brokerhome="${HARVEST_HOME}/brokers"
broker_name="`/bin/grep -v '#' "${brokerhome}/Brokers.cf" | /bin/awk '{print $1}'`"
prog=broker
RETVAL=0

start() {
	echo -n "Starting $prog: "

	daemon --user ${HARVEST_USER} "${brokerhome}/${broker_name}/RunBroker -nocol"
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && touch /var/lock/subsys/${prog}
	echo `pidof ${prog}` > /var/run/${prog}.pid
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killproc $prog
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f /var/lock/subsys/${prog} /var/run/${prog}.pid
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status ${prog}
	;;
  restart)
	stop
	start
	;;
  condrestart)
	if [ -f /var/run/${prog}.pid ] ; then
		stop
		start
	fi
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $RETVAL
