#!/bin/sh

#
# chkconfig: 345 86 14
# description: Starts and stops the handle service
#

# Source function library.
if [ -f /etc/init.d/functions ] ; then
        . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
        . /etc/rc.d/init.d/functions
else
        exit 0
fi


start() {
	KIND="handle"
	su - dspace -c /dspace/bin/start-handle-server 2>&1 > /dev/null
	RETVAL=$?
	echo -n $"Starting $KIND services: "
	return $RETVAL
}

stop() {
	echo "Stopping handle server..."
	PID=`/bin/ps -ef | grep java | grep log4j-handle | grep -v 'grep' | awk '{print $2}'`
	if [ ! -z "$PID" ] ; then
		for process in ${PID}
		do
			/bin/kill $process
		echo "Killed process $process"
		done
	fi
}

restart() {
	stop
	start
}

status() {
	PID=`/bin/ps -ef | grep java | grep log4j-handle | grep -v 'grep' | awk '{print $2}'`
	if [ ! -z "$PID" ] ; then
		echo "Handle server service is running (PID $PID)."
	else
		echo "Handle server service is stopped."
	fi
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart	
		;;
	status)
		status
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|status}"
		exit 1
esac
exit $?
