#!/usr/bin/env bash
#
# JBoss AS7* Control Script
#
# ** Modification for AS7 server made by ArtCom GmbH 2011/10/22 **
#
# 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 Ubuntu systems. It assumes 
# that JBoss-AS7 lives in /opt/jboss-as7, it's run by user 
# 'jboss' and JDK binaries are in /usr/java/jdk/bin.
# All this can be changed in the script itself. 
# --------------------------------------------------------------------------
# See also the specification here:
# http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
# Summary: Required subcommands:
#    start, stop, restart, status, force-reload
# The Subcommand status must provide the following exit codes:
# 0	program is running or service is OK
# 1	program is dead and /var/run pid file exists
# 2	program is dead and /var/lock lock file exists
# 3	program is not running
# 4	program or service status is unknown
# --------------------------------------------------------------------------

# 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
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 2 6
# Should-Start:      $syslog
# Should-Stop:
# Description: Start the JBoss application server.
### END INIT INFO

####set -x

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

#make java is on your path
##JAVAPTH=${JAVAPTH:-"/usr/java/j2sdk1.4.1/bin"}

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

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

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:-"$JBOSS_HOME/standalone/log/server.log"}

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

CMD_START="cd $JBOSS_HOME/bin; $JBOSSSH" 
##CMD_STOP="java -classpath $JBOSSCP org.jboss.Shutdown --shutdown"
CMD_STOP="$JBOSS_HOME/bin/jboss-admin.sh --connect command=:shutdown"
CMD_RELOAD="$JBOSS_HOME/bin/jboss-admin.sh --connect command=:reload"

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

if [ -z "`echo $PATH | grep -q {$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

##echo CMD_START = $CMD_START

case "$1" in
start)
    cd $JBOSS_HOME/bin
    if [ -z "$SUBIT" ]; then
        eval $CMD_START >${JBOSS_CONSOLE} 2>&1 &
    else
        $SUBIT "$CMD_START >${JBOSS_CONSOLE} 2>&1 &" 
    fi
    ;;
stop)
    if [ -z "$SUBIT" ]; then
        $CMD_STOP
    else
        $SUBIT "$CMD_STOP"
    fi 
    ;;
force-reload)
    if [ -z "$SUBIT" ]; then
        $CMD_RELOAD
    else
        $SUBIT "$CMD_RELOAD"
    fi
    ;;
restart)
    $0 stop
    echo "waiting 10 sec for shutdown completion..."
    sleep 10
    $0 start
    ;;
status)
    # The Subcommand status must provide the following exit codes:
    # 0 program is running or service is OK
    # 1 program is dead and /var/run pid file exists
    # 2 program is dead and /var/lock lock file exists
    # 3 program is not running
    # 4 program or service status is unknown

    if [ ! -d "$JBOSS_HOME" ]
    then echo "The artcom jboss-as7 is not installed"
         exit 4
    elif sudo $JBOSS_HOME/bin/jboss-admin.sh --connect quit 2>&1 | grep -q "controller at" 
    then echo "The artcom jboss-as7 is running"
         exit 0
    else echo "The artcom jboss-as7 is stopped"
         exit 3
    fi
    ;;
help)
    echo "usage: $0 (start|stop|restart|force-reload|status|help)"
    ;;
*)
    echo "$0 $1 failed"
    echo "usage: $0 (start|stop|restart|force-reload|status|help)"
esac
