#!/bin/sh

# Start/stop/restart jboss-as.

# Copyright 2011 Giorgio Peron, Campodarsego, PD, Italy giorgio.peron@gmail.com
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


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

# make java is on your path
JAVAPTH=${JAVAPTH:-"$JAVA_HOME/bin"}

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

export LAUNCH_JBOSS_IN_BACKGROUND=yes

# define the script to use to start standalone jboss
JBOSS_START_STANDALONE=${JBOSS_START_STANDALONE:-"$JBOSS_HOME/bin/standalone.sh "}

# define the script to use to shutdown jboss
# change host and port as need
JBOSS_STOP_STANDALONE=${JBOSS_STOP_STANDALONE:-"$JBOSS_HOME/bin/jboss-admin.sh --connect controller=127.0.0.1:9999 command=:shutdown"}

# define log file
JBOSS_CONSOLE="/var/log/jboss-as/jboss.log"
JBOSS_DOMAIN_CONSOLE="/var/log/jboss-as/jboss_domain.log"

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

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

if [ -n "$JBOSS_DOMAIN_CONSOLE" -a ! -d "$JBOSS_DOMAIN_CONSOLE" ]; then
  # ensure the file exists
  touch $JBOSS_DOMAIN_CONSOLE
  if [ "$JBOSSUS" != "RUNASIS" ]; then
    chown -R $JBOSSUS.$JBOSSUS $JBOSS_DOMAIN_CONSOLE
  fi
fi

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

# define what will be done with the console log
JBOSS_DOMAIN_CONSOLE=${JBOSS_DOMAIN_CONSOLE:-"/dev/null"}

CMD_START_STANDALONE="cd $JBOSS_HOME/bin; $JBOSS_START_STANDALONE" 
CMD_STOP_STANDALONE="cd $JBOSS_HOME/bin; $JBOSS_STOP_STANDALONE"

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 "Starting JBoss Application Server"
    cd $JBOSS_HOME/bin
    if [ -z "$SUBIT" ]; then
        eval $CMD_START_STANDALONE >${JBOSS_CONSOLE} 2>&1 &
    else
        $SUBIT "$CMD_START_STANDALONE >${JBOSS_CONSOLE} 2>&1 &" 
    fi
    ;;
stop)
	echo "Stopping JBoss Application Server"
    if [ -z "$SUBIT" ]; then
        $CMD_STOP_STANDALONE >/dev/null 2>&1
    else
        $SUBIT "$CMD_STOP_STANDALONE >/dev/null 2>&1"
    fi 
    ;;
restart)
    $0 stop
    $0 start
    ;;
domain-start)
	echo "Starting Domain JBoss Application Server"
    cd $JBOSS_HOME/bin
    if [ -z "$SUBIT" ]; then
        eval $CMD_START_DOMAIN >${JBOSS_DOMAIN_CONSOLE} 2>&1 &
    else
        $SUBIT "$CMD_START_DOMAIN >${JBOSS_DOMAIN_CONSOLE} 2>&1 &" 
    fi
    ;;
domain-stop)
	echo "Stopping Domain JBoss Application Server"
    if [ -z "$SUBIT" ]; then
        $CMD_STOP_DOMAIN >/dev/null 2>&1
    else
        $SUBIT "$CMD_STOP_DOMAIN >/dev/null 2>&1"
    fi 
    ;;
domain-restart)
    $0 domain-stop
    $0 domain-start
    ;;
*)
    echo "usage: $0 (start|stop|restart|help|domain-start|domain-stop|domain-restart)"
esac