#!/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 directory.
#
# 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.
#    You can do this by 
# 2. Copy this file to the /etc/init.d and jboss.conf to /etc directories, run
#    init function to setup jboss system group and user, and the $JBOSS_HOME
#    ownership.
# 3. Run chkconfig and setup jboss to start in the runlevels you choose.

# Version 3.0.0-1  Ivan Bilenjkij <ibilenjkij@yahoo.com>
# Do not use run.sh and shutdown.sh scripts; start java from here as we'll end
# up having one process less: the actual run.sh .
# Added init function to set up jboss user and group and setup the $JBOSS_HOME
# ownership.

# Version 3.0.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: - This is pretty much what I want for now.

# OTHER: - .


# 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

# Source the jboss configuration if it exists:
[ -f /etc/jboss.conf ] && . /etc/jboss.conf

# Determine java executable:
if [ "$JAVA" == "" ] ; then
	if [ -x $JAVA_HOME/bin/java ] ; then
		JAVA="$JAVA_HOME/bin/java"
	else
		JAVA="java"
	fi
fi

# 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.jar ] || exit 0

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

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


start() {
	# 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 "Starting jboss service: "
		
		# Set the java options; if not already specified in /etc/jboss.conf:
		if [ "$OPTIONS" == "" ] ; then 
			
			# Enable HotSpot if present:
			HOTSPOT=`$JAVA -version 2>&1 | grep HotSpot`
			if [ "$HOTSPOT" != "" ] ; then
				OPTIONS="-server"
			fi
			
			# :
			PROGNAME=`basename $0`
			OPTIONS="$OPTIONS -Djboss.boot.loader.name=$PROGNAME"
			
			# Setup the JAXP parser to use:
			if [ "$JAXP" == "" ] ; then
				JAXP="crimson"
			fi
			
			case "$JAXP" in
				crimson)
					DOM_FACTORY="org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"
					SAX_FACTORY="org.apache.crimson.jaxp.SAXParserFactoryImpl"
					;;
				xerces)
					DOM_FACTORY="org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"
					SAX_FACTORY="org.apache.xerces.jaxp.SAXParserFactoryImpl"
					;;
				*)
					if [ "$DOM_FACTORY" == "" ] && [ "$SAX_FACTORY" == "" ]
					then
						echo -n "DOM or SAX factory not set"
						failure ; echo ; exit 1
					fi
					;;
			esac
			
			OPTIONS="$OPTIONS -Djavax.xml.parsers.DocumentBuilderFactory=$DOM_FACTORY"
			OPTIONS="$OPTIONS -Djavax.xml.parsers.SAXParserFactory=$SAX_FACTORY"
		fi
		
		# Set the jboss classpath; if not set in /etc/jboss.conf:
		if [ "$JBOSS_CLASSPATH" == "" ] ; then
			JBOSS_CLASSPATH="$JBOSS_HOME/bin/run.jar"
			
			# Include the JDK javac compiler for JSP pages. The default is for
			# a Sun JDK compatible distribution which JAVA_HOME points to.
			if [ "$JAVAC_JAR" == "" ] ; then
				JAVAC_JAR="$JAVA_HOME/lib/tools.jar"
			fi
			
			JBOSS_CLASSPATH="$JBOSS_CLASSPATH:$JAVAC_JAR"
		fi
		
		# Prepare the log file if specified in /etc/jboss.conf:
		if [ "$CONSOLE" != "/dev/null" ] ; then
			touch $CONSOLE
			chown jboss.jboss $CONSOLE
		fi
		
		# Compose the command and start jboss server:
		COMMAND="$JAVA $OPTIONS -classpath $JBOSS_CLASSPATH org.jboss.Main $CONF"
		su -l jboss -s /bin/sh -c\
			"cd $JBOSS_HOME/bin && nohup $COMMAND > $CONSOLE 2>&1 &" < /dev/null
		
		# Get the jboss ppid (this has not been tested with green threads):
		pid=`ps -eo pid,user,args | grep -v grep | grep jboss\
			| grep "$JBOSS_HOME/bin && nohup $JAVA" | awk '{print $1}'`
		
		
		if [ $pid ] ; then
			echo $pid > $PIDFILE
			success ; echo
		else
			failure ; 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:"
	
	# Set the jboss shutdown classpath:
	if [ "$STOP_CLASSPATH" == "" ] ; then
		STOP_CLASSPATH="$JBOSS_HOME/bin/shutdown.jar"
	fi
	
	# Compose the command and shutdown jboss server:
	COMMAND="$JAVA -classpath $STOP_CLASSPATH org.jboss.Shutdown"
	su -l jboss -s /bin/sh -c\
		"cd $JBOSS_HOME/bin && $COMMAND > $CONSOLE 2>&1" < /dev/null
	
	ret=$?
	
	if [ $ret -eq 0 ] ; then
		success
	else
		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 "$JBOSS_HOME/bin && nohup $JAVA"`
			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
}

init() {
	if [ "$2" != "" ] ; then
		JBOSS_HOME="$2"
	fi
	
	if [ -d "$JBOSS_HOME" ] ; then
		# Add system group 'jboss' 
		echo -n "JBoss: "
		gid=`grep ^jboss /etc/group | awk -F: '{print $3}'`
		if [ "$gid" == "" ] ; then
			groupadd -r -f jboss
			
			ret=$?
			if [ $ret -eq 0 ] ; then
				gid=`grep ^jboss /etc/group | awk -F: '{print $3}'`
				echo -n "Added system group jboss (gid $gid)"
				success
			else
				echo -n "Adding system group jboss"
				failure ; echo ; exit 1
			fi
		else
			echo -n "Group jboss (gid $gid) exists"
			passed
		fi
		echo
		
		# Add system user 'jboss':
		echo -n "JBoss: "
		uid=`grep ^jboss /etc/passwd | awk -F: '{print $3}'`
		if [ "$uid" == "" ] ; then
			adduser -c "JBoss EJB Container" -d $JBOSS_HOME -g jboss -n -r\
				-p jboss jboss
			
			ret=$?
			if [ $ret -eq 0 ] ; then
				uid=`grep ^jboss /etc/passwd | awk -F: '{print $3}'`
				echo -n "Added system user jboss (uid $uid)"
				success
			else
				echo -n "Adding system user jboss"
				failure ; echo ; exit 1
			fi
		else
			echo -n "User jboss (uid $uid) exists"
			passed
			
			# Check if given JBOSS_HOME matches actual; fail if it does not:
			home=`echo ~jboss`
			if [ "$home" != "$JBOSS_HOME" ] ; then
				echo
				echo -n "JBoss: Invalid home (actual: $home)"
				failure ; echo ; exit 1
			fi
		fi
		echo
		
		# Setup JBOSS_HOME ownership:
		echo -n "JBoss: Setup JBOSS_HOME ($JBOSS_HOME) ownership"
		chown -R jboss.jboss $JBOSS_HOME
		ret=$?
		
		# See if $JBOSS_HOME is a symlink; cd there and set ownership if it is:
		if [ $ret -eq 0 ] && [ -L "$JBOSS_HOME" ] ; then
			pushd $JBOSS_HOME > /dev/null 2>&1 && chown -R jboss.jboss . *
			ret=$?
			popd > /dev/null 2>&1
		fi
		
		if [ $ret -eq 0 ] ; then
			success
		else
			failure ; echo ; exit 1
		fi
		
	else
		echo -n " $2 is not a directory"
		failure ; echo ; exit 1
	fi
	
	echo
}

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

exit 0

