#!/bin/sh
#
# jboss		This is the init script for starting up the JBoss J2EE Container
#
# chkconfig: 345 80 20
# description: Starts and stops JBoss J2EE Container.
# pidfile: /var/run/jboss.pid
# 
# This is a startup/shutdown script for RedHat LINUX systems. It assumes that
# the standard JBoss distribution is located either in predefined $JBOSS_HOME
# or in /usr/local/jboss and that user 'jboss' exists.
#
# To use this script: 
# 1. Add the 'jboss' user and group to your system and set its home in
#    $JBOSS_HOME. Don't forget to chown -R jboss.jboss $JBOSS_HOME.
# 2. Copy this file to the /etc/init.d/ directory and run chkconfig to 
#    start jboss in runlevel you choose.

# Version 3.0.0 Ivan Bilenjkij <ibilenjkij@yahoo.com>
# Took postgresql init script as a reference and bits from Bojan's
# jboss_init_redhat.sh to write this script.
#

# TODO: - Get rid of run.sh and shutdown.sh scripts, start and stop java
#         processes from here and obtain proper jboss ppid.


# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions

# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0


# Define where jboss is - this is the directory containing directories log,
# bin, conf etc.
JBOSS_HOME=${JBOSS_HOME:-"/usr/local/jboss"}

[ -f $JBOSS_HOME/bin/run.sh ] || exit 0

# Define pid file.
PIDFILE=/var/run/jboss.pid

# Define what will be done with the console log.
CONSOLE=${JBOSS_CONSOLE:-"/dev/null"}

START_SCRIPT="$JBOSS_HOME/bin/run.sh"

start() {
	JBOSS_START=$"Starting jboss service: "
	
	# Check for jboss already running started by this script ...
	if [ -f $PIDFILE ] ; then
		pid=`cat $PIDFILE`
	fi
	
	if [ $pid ] ; then
		echo $"JBoss already running."
	else
		echo -n "$JBOSS_START"
		su -l jboss -s /bin/sh -c "nohup sh $START_SCRIPT > $CONSOLE 2>&1" < /dev/null &
		#su -l jboss -s /bin/sh -c "nohup sh $START_SCRIPT > $CONSOLE 2>&1 &" < /dev/null
		sleep 5
		# Get the parent pid as a reference.
		pid=`ps -eo pid,user,args | grep -v grep | grep -v root | grep $START_SCRIPT | awk '{print $1}'`
		
		if [ $pid ] ; then
			success "$JBOSS_START"
			
			echo $pid > $PIDFILE
			echo
		else
			failure "$JBOSS_START"
			echo
		fi
	fi
}

stop() {
	# Check for jboss already running ...
	if [ -f $PIDFILE ] ; then
		pid=`cat $PIDFILE`
		if [ "$pid" == "" ] ; then
			rm -f $PIDFILE
			exit 0;
		fi
	fi
	
	echo -n $"Stopping jboss service:"
	
	STOP_SCRIPT="sh $JBOSS_HOME/bin/shutdown.sh"
	su -l jboss -s /bin/sh -c "$STOP_SCRIPT" > /dev/null 2>&1
	ret=$?
	
	if [ $ret -eq 0 ] ; then
		echo_success
	else
		echo_failure
	fi
	
	echo
	rm -f $PIDFILE
}

status() {
	if [ -f $PIDFILE ] ; then
		pid=`cat $PIDFILE`
		if [ -d /proc/$pid ] ; then
			cmdline=`cat /proc/$pid/cmdline | grep -a $START_SCRIPT`
			if [ "$cmdline" != "" ] ; then
				echo $"jboss (pid $pid) is running ..."
				return 0
			else
				echo $"jboss dead but pid file exists"
				rm -f $PIDFILE
				return 1
			fi
		fi
	else
		echo $"jboss is not running."
		return 1
	fi
}

restart() {
	stop
	start
}

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

exit 0
