#!/bin/sh
#set -xv
# ---------------------------------------------------------------------------
# JOnAS: Java(TM) Open Application Server
# Copyright (C) 2006 Bull S.A.S.
# 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
#
# ---------------------------------------------------------------------------
# $Id$
# ---------------------------------------------------------------------------

# Debug mode
# JONASCTL_DEBUG="false"

# Parse command line
# -------------------------------

# Discover the profile to use
if [ "$JONAS_BASE" = "" ]; then
    JONAS_PROFILE=$JONAS_ROOT;
else
    JONAS_PROFILE=$JONAS_BASE;
fi

# start/stop, ...
# FIXME : Usually the command is not always the first parameter of the command line
# ex: 'jonas -n j1 start' == 'jonas start -n j1'
COMMAND=$1
shift

# -------------------------------
# End Parsing

# Load the environment
# Old style (deprecated)
if [ -f $JONAS_PROFILE/env.sh ]; then
    # source the env.sh :
    #  - retrieve JONAS_ROOT associated with the JOnAS base
    #  - retrieve the right Java and Ant homes
    #  - retrieve the server name if any
    . $JONAS_PROFILE/env.sh;
fi
# Modern style (new locations)
if [ -f $JONAS_PROFILE/conf/env.sh.include ]; then
    # Check first if JONAS_ROOT value has been set in the script
    grep "^JONAS_ROOT=@JONAS_ROOT@$" $JONAS_PROFILE/conf/env.sh.include > /dev/null
    if [ $? = 0 ]; then
        printf "Please set JONAS_ROOT value in your "
        printf "$JONAS_PROFILE/conf/env.sh.include before using $0.\n"
        exit 1
    else
        # source the env.sh :
        #  - retrieve JONAS_ROOT associated with the JOnAS base
        #  - retrieve the right Java and Ant homes
        #  - retrieve the server name if any
        . $JONAS_PROFILE/conf/env.sh.include;
    fi
fi

# Overload with the command environment
# Old style (deprecated)
if [ -f $JONAS_PROFILE/env.$COMMAND.sh ]; then
    . $JONAS_PROFILE/env.$COMMAND.sh;
fi
# Modern style
if [ -f $JONAS_PROFILE/conf/env.$COMMAND.sh.include ]; then
    . $JONAS_PROFILE/conf/env.$COMMAND.sh.include;
fi

# Launch the command
# --------------------------------
# Ensure JONAS BASE is exported if it was set
if [ -d $JONAS_BASE ]; then
    export JONAS_BASE
fi

if [ ! -z "$NAME_SERVER" ]; then
    NAME="-n $NAME_SERVER"
fi

CMD="$JONAS_ROOT/bin/unix/jonas $COMMAND $NAME $*"

# Help for debug
if [ "$JONASCTL_DEBUG" = "yes" ]; then

    S="\033[1;32m"
    E="\033[0m"
    printf "* ---------------------------------------------------------------\n"
    printf "* JOnAS Environment :\n"
    printf "*\tJONAS_ROOT  : $S$JONAS_ROOT$E\n"
    printf "*\tJONAS_BASE  : $S$JONAS_BASE$E\n"
    printf "*\tSERVER_NAME : $S$SERVER_NAME$E\n"
    printf "*\tJAVA_HOME   : $S$JAVA_HOME$E\n"
    printf "*\tJAVA_OPTS   : $S$JAVA_OPTS$E\n"
    printf "*\tANT_HOME    : $S$ANT_HOME$E\n"
    printf "*\tCommand     : $S$CMD$E\n"
    printf "* ---------------------------------------------------------------\n"
fi

# Command Execution
$CMD
