#!/bin/sh

### BEGIN INIT INFO
# Provides: handled
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start handled
### END INIT INFO
#
# description: Starts and stops the handle service
#

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}'`
        PID=`/bin/ps -ef | grep java | grep 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}'`
        PID=`/bin/ps -ef | grep java | grep 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 $?
