#! /bin/sh
#
# /etc/init.d/activemq
#
# System startup script for the Apache ActiveMQ
#
### BEGIN INIT INFO
# Provides: activemq
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 4 6
# Description: Apache MQ server
# Short-Description: ApacheMQ server
### END INIT INFO

ACTIVEMQ_HOME=/usr/lib/activemq
ACTIVEMQ_BASE=/etc/activemq

ACTIVEMQ_USER=activemq
ACTIVEMQ_PID=/var/run/activemq.pid
ACTIVEMQ_LOG=/var/log/activemq.log

test -x ${ACTIVEMQ_HOME}/bin/activemq || exit 5

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
# 7 - program is not running
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

case "$1" in
    start)
        echo "Starting activemq server..."
        if test -n "${ACTIVEMQ_LOG}"; then
          touch ${ACTIVEMQ_LOG}
          chown ${ACTIVEMQ_USER} ${ACTIVEMQ_LOG}
        fi
        ACTIVEMQ_HOME=${ACTIVEMQ_HOME} ACTIVEMQ_BASE=${ACTIVEMQ_BASE} startproc -u ${ACTIVEMQ_USER} -p ${ACTIVEMQ_PID} -l ${ACTIVEMQ_LOG} ${ACTIVEMQ_HOME}/bin/activemq
        rc_failed 0
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down activemq server"
        #killproc -TERM ${ACTIVEMQ_HOME}/bin/activemq
        #rc_status -v

       if [ `ps -C java -f | grep activemq | wc -l` -ge 1 ]
       then
           kill `ps -C java -f | grep activemq | awk '{print $2}'`
           rc_failed 0
        else 
           echo -n " (not running)"
           rc_failed 0
        fi
        rc_status -v
        ;;
    reload) # no reloading possible, do a restart
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        sleep 2
        $0 start
        # Remember status and be quiet
        rc_status
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        sleep 2
        $0 start
        # Remember status and be quiet
        rc_status
        ;;
    status)
        echo -n "Checking for activemq"
        if [ `ps -C java -f | grep activemq | wc -l` -ge 1 ]
        then
           rc_failed 0
          # rc_status -v 0
        else
           rc_failed 3
          # rc_status -v 3
        fi
        rc_status -v
        #checkproc -k -p ${ACTIVEMQ_PID} ${ACTIVEMQ_HOME}/bin/activemq
        #rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
        ;;
esac
rc_exit

