#!/bin/sh

# Copy this file to /etc/init.d/
# Run the command "chkconfig jboss on" as root from /usr/sbin

## This script has been derived from the following one:
# $Id: jboss_init_redhat.sh 71252 2008-03-25 17:52:00Z dbhole $
# http://ubuntuforums.org/showthread.php?t=652472&page=2

## JBoss Control Script
## To use this script run it as root - it will switch to the specified user

## Major changes from the original one:
# * usage of the shutdown.sh script instead of killing the jboss' JVM process. This simplifies the structure of the script.
# * added support for bind address change (see the JBOSS_HOST variable).

#define the JAVA_HOME
JAVA_HOME=${JAVA_HOME:-"/home/livecycle/jdk1.6.0_16"}

#define where jboss is - this is the directory containing directories log, bin, conf etc
JBOSS_HOME=${JBOSS_HOME:-"/home/livecycle/jboss-4.2.0"}

#define the user under which jboss will run, or use 'RUNASIS' to run as the current user
JBOSS_USER=${JBOSS_USER:-"livecycle"}

#configuration to use, usually one of 'minimal', 'default', 'all'
JBOSS_CONF=${JBOSS_CONF:-"lc_mysql"}

#define JBOSS_HOST so that it listens to all availible IPs
JBOSS_HOST=${JBOSS_HOST:-"0.0.0.0"}

#if JBOSS_HOST specified, use -b to bind jboss services to that address
JBOSS_BIND_ADDR=${JBOSS_BIND_ADDR:-"-b $JBOSS_HOST"}

#define the script used to start jboss
JBOSSSH=${JBOSSSH:-"$JBOSS_HOME/bin/run.sh -c $JBOSS_CONF -b $JBOSS_HOST"}

#define the script used to stop jboss
JBOSSST=${JBOSSST:-"$JBOSS_HOME/bin/shutdown.sh -S -s jnp://$JBOSS_HOST:1099"}

#define what will be done with the console log
#JBOSS_CONSOLE=${JBOSS_CONSOLE:-"/dev/null"}
JBOSS_CONSOLE=${JBOSS_CONSOLE:-"$JBOSS_HOME/server/$JBOSS_CONF/log/console.log"}

if [ "$JBOSS_USER" = "RUNASIS" ]; then
	SUBIT=""
	else
		SUBIT="su - $JBOSS_USER -c "
fi

if [ -n "$JBOSS_CONSOLE" -a ! -d "$JBOSS_CONSOLE" ]; then

	# ensure the file exists
	touch $JBOSS_CONSOLE
	if [ ! -z "$SUBIT" ]; then
		chown $JBOSS_USER $JBOSS_CONSOLE
	fi
fi

if [ -n "$JBOSS_CONSOLE" -a ! -f "$JBOSS_CONSOLE" ]; then
	echo "WARNING: location for saving console log invalid: $JBOSS_CONSOLE"
	echo "WARNING: ignoring it and using /dev/null"JBOSS_CONSOLE="/dev/null"
fi

JBOSS_CMD_START="cd $JBOSS_HOME/bin; $JBOSSSH"
JBOSS_CMD_STOP="cd $JBOSS_HOME/bin; $JBOSSST"

if [ ! -d "$JBOSS_HOME" ]; then
	echo JBOSS_HOME does not exist as a valid directory :
	$JBOSS_HOME
	exit 1
fi

# ---ADDED FROM SKELETON FOR SUSE CHKCONFIG---

### BEGIN INIT INFO
# Provides:          jboss-4.2.1
# Required-Start:    $ALL
# Should-Start:      $ALL
# Required-Stop:     $syslog $remote_fs
# Should-Stop:       ypbind smtp
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: JBoss 4.2.1 Community Edition
# Description:       JBoss 4.2.1 J2EE application server that hosts Adobe LiveCycle ES2 (9.0) - MySQL should already be running
### END INIT INFO

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
JBOSS_BIN=/etc/init.d/jboss
test -x $JBOSS_BIN || { echo "$JBOSS_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Check for existence of needed config file and read it
JBOSS_CONFIG=/etc/sysconfig/jbosscfg
test -r $JBOSS_CONFIG || { echo "$JBOSS_CONFIG not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

# Read config	
. $JBOSS_CONFIG

# Source LSB init functions
# providing start_daemon, killproc, pidofproc, 
# log_success_msg, log_failure_msg and log_warning_msg.
# This is currently not used by UnitedLinux based distributions and
# not needed for init scripts for UnitedLinux only. If it is used,
# the functions from rc.status should not be sourced or used.
#. /lib/lsb/init-functions

# 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     be verbose in local rc status and clear it afterwards
#      rc_status -v -r  ditto and clear both the local and overall rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear both the local and overall rc status
#      rc_exit          exit appropriate to overall rc status
#      rc_active        checks whether a service is activated by symlinks
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0	  - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

# ---END OF ADDED FROM SKELETON FOR SUSE CHKCONFIG---

case "$1" in

	start)
		cd $JBOSS_HOME/bin
		echo -n "Starting JBoss/LiveCycle "
		echo JBOSS_CMD_START = $JBOSS_CMD_START
		if [ -z "$SUBIT" ]; then
			## Start daemon with startproc(8). If this fails
			## the return value is set appropriately by startproc.
			/sbin/startproc $JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &
		else
			$SUBIT "$JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &"
		fi
		# Remember status and be verbose
		rc_status -v
		;;

	stop)
		cd $JBOSS_HOME/bin
		echo -n "Shutting down JBoss/LiveCycle "
		echo JBOSS_CMD_STOP = $JBOSS_CMD_STOP
		if [ -z "$SUBIT" ]; then
			## Stop daemon with killproc(8) and if this fails
			## killproc sets the return value according to LSB.
			/sbin/killproc -TERM $JBOSS_CMD_STOP >${JBOSS_CONSOLE} 2>&1 &
		else
			$SUBIT "$JBOSS_CMD_STOP >${JBOSS_CONSOLE} 2>&1 &"
		fi
		# Remember status and be verbose
		rc_status -v
		;;

	restart)
		$0 stop
		# there must be a time between the "stop" and the "start" because the mentioned two are asynchronous operations. 
		# this depends on how fast your machine is.
		sleep 3
		$0 start
		# Remember status and be quiet
		rc_status
		;;
		
	*)

	echo "usage: $0 (start|stop|restart)"

esac
rc_exit
