#!/bin/sh
#
# JBoss Control Script
#
# To use this script
# run it as root - it will switch to the specified user
# It loses all console output - use the log.
#
# Here is a little (and extremely primitive)
# startup/shutdown script for SuSE systems. It assumes
# that JBoss lives in /usr/local/tstv/jboss, it's run by user
# 'jboss' and JDK binaries are in /usr/local/tstv/jdk/bin. All
# this can be changed in the script itself.
#
# Either amend this script for your requirements
# or just ensure that the following variables are set correctly
# before calling the script.
#
### BEGIN INIT INFO
# Provides: jboss
# X-UnitedLinux-Should-Start: oracle
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the JBoss application server.
### END INIT INFO

jbossIsRunning()
{
      jboss_ps_log=`mktemp /var/tmp/jboss-ps.log.XXXXXX`
      ps aux --cols 1024 >"$jboss_ps_log"
      jboss_is_running="false"
      if grep org.jboss.Main "$jboss_ps_log" >/dev/null 2>/dev/null ; then
            jboss_is_running="true"
      fi
      rm -f "$jboss_ps_log"
      test "$jboss_is_running" = "true"
}

oracleIsRunning()
{
      oracle_ps_log=`mktemp /var/tmp/oracle-ps.log.XXXXXX`
      ps aux --cols 1024 >"$oracle_ps_log"
      oracle_is_running="false"
      if grep [o]ra_pmon "$oracle_ps_log" >/dev/null 2>/dev/null ; then
            oracle_is_running="true"
      fi
      rm -f "$oracle_ps_log"
      test "$oracle_is_running" = "true"
}

listenerIsRunning()
{
      listener_ps_log=`mktemp /var/tmp/listener-ps.log.XXXXXX`
      ps aux --cols 1024 >"$listener_ps_log"
      listener_is_running="false"
      if grep [t]nslsnr "$listener_ps_log" >/dev/null 2>/dev/null ; then
            listener_is_running="true"
      fi
      rm -f "$listener_ps_log"
      test "$listener_is_running" = "true"
}

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

#make java is on your path
JAVAPTH=${JAVAPTH:-"/usr/local/tstv/jdk/bin"}

#specify the host address to bind to
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_HOST:+"-b $JBOSS_HOST"}

#define the classpath for the shutdown class
JBOSSCP=${JBOSSCP:-"$JBOSS_HOME/bin/shutdown.jar:$JBOSS_HOME/client/jnet.jar"}

#define the classpath for any additional jars to be included at jboss start
ADD_CLASSPATH="export JBOSS_CLASSPATH=\"/usr/local/platform/lib/SignatureVerifier.jar\""

#define the script to use to start jboss
JBOSSSH=${JBOSSSH:-"$JBOSS_HOME/bin/run.sh -c default $JBOSS_BIND_ADDR"}

# 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
#
# 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.

if [ -n "$JBOSS_CONSOLE" -a ! -d "$JBOSS_CONSOLE" ]; then
  # ensure the file exists
  touch $JBOSS_CONSOLE
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

#define what will be done with the console log
JBOSS_CONSOLE=${JBOSS_CONSOLE:-"/usr/local/tstv/jboss/log/jboss.log"}

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

CMD_START="cd $JBOSS_HOME/bin; $JBOSSSH"
CMD_STOP="java -classpath $JBOSSCP org.jboss.Shutdown --shutdown"

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

if [ -z "`echo $PATH | grep $JAVAPTH`" ]; then
  export PATH=$PATH:$JAVAPTH
fi

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

case "$1" in
start)
    echo -n "Checking Oracle server status: "
    if oracleIsRunning ; then
      rc_failed 0
      rc_status -v
    else
      rc_failed 3
      rc_status -v
      exit 1
    fi

    echo -n "Checking Oracle Listener status: "
    if listenerIsRunning ; then
      rc_failed 0
      rc_status -v
    else
      rc_failed 3
      rc_status -v
      exit 1
    fi
	
    echo -n "Starting JBoss application server: "
    if jbossIsRunning ; then
      rc_failed 0
    else
        cd $JBOSS_HOME/bin
        if [ -z "$SUBIT" ]; then
            $ADD_CLASSPATH; eval $CMD_START >${JBOSS_CONSOLE} 2>&1 &
        else
            $SUBIT "$ADD_CLASSPATH; $CMD_START >${JBOSS_CONSOLE} 2>&1 &"
        fi
    fi

    # Remember status and be verbose
    rc_status -v
    ;;
stop)
    echo -n "Shutting down JBoss application server: "
    if [ -z "$SUBIT" ]; then
        $CMD_STOP
    else
        $SUBIT "$CMD_STOP"
    fi

    while jbossIsRunning
    do
        echo "... waiting for shutdown to complete ..."
        sleep 5
    done

    # Remember status and be verbose
    rc_status -v
    ;;
restart)
    $0 stop
    $0 start

    # Remember status and be quiet
    rc_status
    ;;
status)
    echo -n "Checking for service jboss "
    ## Check status with checkproc(8), if process is running
    ## checkproc will return with exit status 0.

    # Return value is slightly different for the status command:
    # 0 - service up and running
    # 1 - service dead, but /var/run/  pid  file exists
    # 2 - service dead, but /var/lock/ lock file exists
    # 3 - service not running (unused)
    # 4 - service status unknown :-(
    # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)

    if jbossIsRunning ; then
      rc_failed 0
    else
      rc_failed 3
    fi

    # NOTE: rc_status knows that we called this init script with
    # "status" option and adapts its messages accordingly.
    rc_status -v
    ;;
*)
    echo "usage: $0 (start|stop|restart|status|help)"
esac
rc_exit