#!/bin/sh
# ---------------------------------------------------------------------------
# JOnAS: Java(TM) Open Application Server
# Copyright (C) 2005 Bull S.A.
# Contact: jonas-team@objectweb.org
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Initial developer(s): France Telecom
# Contributor(s): Guillaume Sauthier
#
# processname: java
# chkconfig: - 86 14
# description: JOnAS is a J2EE Open Application Server. It's used to serve J2EE application
#
# ---------------------------------------------------------------------------
# $Id$
# ---------------------------------------------------------------------------

# Load functions
. /etc/rc.d/init.d/functions

# JOnAS root
export JONAS_ROOT=@JONAS_ROOT@

# Load JOnAS functions
. $JONAS_ROOT/bin/unix/functions

# Customizable JAVA_HOME Settings ...
# --------------------------------------
export JAVA_HOME=@MUST_SET_JAVA_HOME@
# JAVA_HOME Check
if [ "$JAVA_HOME" = "" ] || [ ! -d $JAVA_HOME ]; then
    error "JAVA_HOME is not set in $0 ..."
    exit 4
fi

# JOnAS Executable
export JONASCTL=$JONAS_ROOT/bin/unix/jonasctl

OPTIONS=""
RUN=""
# Debug mode
#export JONASCTL_DEBUG="yes"
#export ENV_DEBUG="yes"

# Application name
NAME="JOnAS"
# jonas command timeout
TIMEOUT_OPT="-timeout 120"

# function : Check if JOnAS is already started
#  and exit with an error if the instance is up
#  (pre-start)
# ----------------------------------------------
fail_if_already_started()
{
    JONAS_NB=`ps -efwww | grep "Djonas.base=$JONAS_BASE" | grep -v grep | wc -l`
    if [ $JONAS_NB -eq 1 ]; then
	error "JOnAS using base '$JONAS_BASE' is already started."
	exit 1
    fi
}

# function : Check if JOnAS is already started
#  and exit with an error if the instance is down
#  (pre-stop)
# ----------------------------------------------
fail_if_not_started()
{
    JONAS_NB=`ps -efwww | grep "Djonas.base=$JONAS_BASE" | grep -v grep | wc -l`
    debug "Found $JONAS_NB instances"
    if [ $JONAS_NB -eq 0 ]; then
	error "No JOnAS using base '$JONAS_BASE' have been started."
	exit 2
    fi
    
}

# function : Start a JOnAS profile
# ----------------------------------
start()
{
    fail_if_already_started

    LOGGER=">> $JONAS_BASE/logs/jonas.start.log"

    echo -e $"Starting ${NAME}/${SERVER_NAME} ..."
    su jonas --preserve-environment --command="$JONASCTL start $TIMEOUT_OPT $* $LOGGER"

    JONAS_NB=`ps -efwww | grep "Djonas.base=$JONAS_BASE" | grep -v grep | wc -l`
    if [ $JONAS_NB -eq 1 ]; then
        touch /var/lock/subsys/jonas-${SERVER_NAME}.lock
        success $"${NAME}/${SERVER_NAME} startup"
	echo "${NAME}/${SERVER_NAME} started"
    else
        failure $"${NAME}/${SERVER_NAME} startup"
	echo "${NAME}/${SERVER_NAME} cannot be started"
	echo "Log can be found here '$JONAS_BASE/logs/jonas.start.log' and here '${JONAS_BASE}/logs/jonas.su.log'"
	echo "Make sure that '$JONAS_BASE' is readable/writeable for user 'jonas'"
    fi
}

# function : Stop a JOnAS profile
# ----------------------------------
stop()
{
    fail_if_not_started

    LOGGER=">> $JONAS_BASE/logs/jonas.stop.log"

    echo -e $"Stopping ${NAME}/${SERVER_NAME} ..."
    su jonas --preserve-environment --command="$JONASCTL stop $TIMEOUT_OPT $* $LOGGER"

    JONAS_NB=`ps -efwww | grep "Djonas.base=$JONAS_BASE" | grep -v grep | wc -l`
    if [ $JONAS_NB -eq 1 ]; then
	rm -rf /var/lock/subsys/jonas-${SERVER_NAME}.lock
	success $"${NAME}/${SERVER_NAME} shutdown"
	echo "${NAME}/${SERVER_NAME} stopped"
    else
	failure $"${NAME}/${SERVER_NAME} shutdown"
	echo "${NAME}/${SERVER_NAME} cannot be stopped"
	echo "Log can be found '$JONAS_BASE/logs/jonas.stop.log' and '${JONAS_BASE}/logs/jonas.su.log'"
    fi
}

# function : Restart a JOnAS profile
# ----------------------------------
restart()
{
    stop $*
    # Workaround
    # Seems that jonas stop don't stop the JOnAS java process immediately
    # Hand is retuned before the jonas process exit
    sleep 3
    # Restart the instance
    start $*
}

# function : Status of a JOnAS profile
# ----------------------------------
status()
{
    ### status of JONAS_BASE or JONAS_ROOT processes
    nb=`ps -efwww | grep "Djonas.base=$JONAS_BASE" | grep -v grep | grep -v status | wc -l`
    echo "$nb process(es) found"
    # DON'T TOUCH THE FOLLOWING TRACES !!!
    if [ $nb -eq 0 ]; then
	echo "**** jonas webapps stopped ****" ;
    else
	echo "**** jonas webapps running ****" ;
    fi
}

# function : Prints bases of running instances
# ------------------------------------------------
status_all()
{
    RUNNING=`ps -efwww | grep "Dinstall.root=$JONAS_ROOT" | grep -v grep`
    for part in $RUNNING; do
	echo $part | grep "jonas.base" > /dev/null
	if [ $? -eq 0 ]; then
	    # this is a jonas.base line
	    echo `echo $part | grep "jonas.base"`
	fi
    done;
}

# Parse arguments
# --------------------------------
while [ "$#" -gt 0 ]; do
     case "$1" in
	start)
		RUN="start"
		;;
	stop)
		RUN="stop"
		;;
	restart)
		RUN="restart"
		;;
        status-all)
                RUN="status_all"
                ;;
        status)
                RUN="status"
                ;;
	-jb)
		shift
		JONAS_BASE=$1
		debug "jonas.base=$JONAS_BASE"
		;;
	*)
		OPTIONS="$OPTIONS $1"
     esac
     shift
done


# Determine the JOnAS Profile to use
# ----------------------------------
if [ "${JONAS_BASE}" = "" ]; then
    JONAS_BASE=${JONAS_ROOT};
else
    JONAS_BASE=${JONAS_BASE};
fi
export JONAS_BASE

debug "jonas.profile=$JONAS_BASE"

# Source the environment
# ------------------------
if [ -f $JONAS_BASE/env.sh ]; then
    . ${JONAS_BASE}/env.sh;
fi


# Launch the command
# -------------------------
RETVAL=0
if [ "$RUN" != "start" ] && \
   [ "$RUN" != "stop" ] && \
   [ "$RUN" != "restart" ] && \
   [ "$RUN" != "status" ] && \
   [ "$RUN" != "status_all" ]; then
    RETVAL=1
    echo $"Usage: $0 {start|stop|restart|status|status-all|-jb <jonas/base/path>}"
else
    $RUN $OPTIONS
fi

exit 0
