#!/bin/bash
#
# Startup script for the Harvest Gatherer
#
# chkconfig: 345 86 14
# description: HARVEST is an integrated set of tools to gather, extract,
#	       organize, and search information across the Internet.
#	       The GATHERER retrieves information resources using a variety
#	       of standard access methods (FTP, Gopher, HTTP, NNTP, and local
#	       files), and then summarizes those resources in various
#	       type-specific ways to generate structured indexing information.
#
#	       Starts all Gatherer in ${HARVEST_HOME}/gatherers
#
# processname: gatherd
# pidfile: /var/run/gatherd.pid
#

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

# Path to where you install harvest
HARVEST_HOME="/opt/harvest"
HARVEST_USER="apache"
GATHERER_HOME="${HARVEST_HOME}/gatherers"

## Do not change after this line ############
prog=gatherd
RETVAL=0

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

	if [ -f /var/run/${prog}.pid ] ; then
	  echo "Already running"
	  return 0
	fi
	for name in ${GATHERER_HOME}/*/RunGatherd ;
	do
	  n=`echo ${name} | sed 's/RunGatherd//'`;
	  echo "  `basename ${n}`"

	  daemon --user ${HARVEST_USER} "${name}"
	  RETVAL=$?
	  if [ "$RETVAL" -gt 0 ];then return $RETVAL; fi
	done
	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
