#!/bin/sh
# ---------------------------------------------------------------------------
# JOnAS: Java(TM) Open Application Server
# Copyright (C) 1999 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): Philippe Durieux
# Contributor(s): Adriana Danes :
#                    - Change /config en /conf
#                    - Use JONAS_BASE
# Florent Benoit & Ludovic Bert
# ---------------------------------------------------------------------------
# $Id: jonas.sh,v 1.23 2004/03/22 15:47:07 benoitf Exp $
# ---------------------------------------------------------------------------

# -----------------------------------------------------------------------
# Config execution environment
# -----------------------------------------------------------------------
if [ ! -d $JONAS_ROOT/ejbjars ]
then
    echo "JONAS_ROOT must be set"
    exit 1
fi
. $JONAS_ROOT/bin/unix/setenv
. $JONAS_ROOT/bin/unix/config_env

# add ow_jonas_bootstrap.jar to the classpath for the bootstrap class
CLASSPATH=$JONAS_ROOT/lib/common/ow_jonas_bootstrap.jar$SPS$CLASSPATH

JONAS_OPTS="\
    -Dinstall.root=$JONAS_ROOT \
    -Djonas.base=$JONAS_BASE \
    -Djava.security.policy=$JONAS_BASE/conf/java.policy \
    -Djava.security.auth.login.config=$JONAS_BASE/conf/jaas.config \
    -Djonas.classpath=$XTRA_CLASSPATH \
    -Djonas.default.classloader=true \
    -Djavax.rmi.CORBA.PortableRemoteObjectClass=org.objectweb.carol.rmi.multi.MultiPRODelegate \
    -Djava.naming.factory.initial=org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory \
    -Djava.endorsed.dirs=$JONAS_ROOT/lib/endorsed
"


# ---------------------------------------------
# Get args
# ---------------------------------------------
PROF_OPTS=
PROF=
ARGS=
MODE=
JONASNAME=
NAME_OPT=
WHERE=background
while [ "$#" -gt 0 ]
do	case "$1" in
	start)
		MODE=start
		;;
	stop)
		MODE=stop
		;;
	admin)
		MODE=admin
		;;
	version)
		MODE=version
		;;
	check)
		MODE=check
		;;
	-cp)
		shift
		CLASSPATH=$CLASSPATH$SPS$1
		;;
	-fg)
		WHERE=foreground
		;;
	-bg)
		WHERE=background
		;;
	-win)
		WHERE=window
		;;
	-nojit)
		JONAS_OPTS="$JONAS_OPTS -Djava.compiler=NONE"
		;;
	-rmi-annotation)
	    ANNOTATE=true
	    export ANNOTATE
		;;
	-n)
		shift
		JONASNAME=$1
		;;
	-D*)
		JONAS_OPTS="$JONAS_OPTS $1"
		;;

	-prof)
		PROF=yes
        ;;
	*)
		# all other args are passed "as is" to the java program
		ARGS="$ARGS $1"
		;;
	esac
	shift
done

# ---------------------------------------------
# Check args
# ---------------------------------------------
case "$MODE" in
start)
	CLASS_TO_RUN=org.objectweb.jonas.server.Server
	;;
stop)
	CLASS_TO_RUN="org.objectweb.jonas.adm.JonasAdmin -s"
	;;
admin)
	CLASS_TO_RUN=org.objectweb.jonas.adm.JonasAdmin
	;;
version)
	CLASS_TO_RUN=org.objectweb.jonas_lib.version.Version
	;;
check)
	CLASS_TO_RUN=org.objectweb.jonas.tools.CheckEnv
	;;
*)
	echo "Usage: jonas start|stop|admin|version|check [options]"
	exit 1
	;;
esac

# ---------------------------------------------
# PROF_OPT may be used for using profiler tool
# ---------------------------------------------

case "$PROF" in
yes)
	PROF_OPTS="$PROF_OPT"
    ;;
no)
	;;
esac

# ---------------------------------------------
# Print out JONAS_BASE
# ---------------------------------------------
case "$MODE" in
start)
	echo "JONAS_BASE set to" $JONAS_BASE
	;;
check)
	echo "JONAS_BASE set to" $JONAS_BASE
	;;
esac

# ---------------------------------------------
# Add RMI Annotation
# ---------------------------------------------
if [ -z "$ANNOTATE" ]
then
	JONAS_OPTS="$JONAS_OPTS -Djava.rmi.server.RMIClassLoaderSpi=org.objectweb.jonas.server.RemoteClassLoaderSpi"
fi

# ---------------------------------------------
# Set jonas server name
# ---------------------------------------------
if [ ! -z "$JONASNAME" ]
then
    NAME_OPT="-n $JONASNAME"
	JAVA_OPTS="$JAVA_OPTS -Djonas.name=$JONASNAME "
    fi

# ---------------------------------------------
# Set tomcat base directory
# ---------------------------------------------
if [ -n "$CATALINA_HOME" ]
then
   TOMCAT_OPTS="$TOMCAT_OPTS -Dcatalina.home=$CATALINA_HOME"
fi
if [ -n "$CATALINA_BASE" ]
then
   TOMCAT_OPTS="$TOMCAT_OPTS -Dcatalina.base=$CATALINA_BASE"
fi

# ---------------------------------------------
# Set jetty home directory
# ---------------------------------------------
JETTY_OPTS=""
if [ -n "$JETTY_HOME" ]
then
   JETTY_OPTS="-Djetty.home=$JETTY_HOME"
fi

# ---------------------------------------------
# Run java command
# ---------------------------------------------
BOOT=org.objectweb.jonas.server.Bootstrap
if [ "$MODE" = "start" ]
then
    case "$WHERE" in
    foreground)
	$JAVA $JAVA_OPTS $JONAS_OPTS $TOMCAT_OPTS $JETTY_OPTS $PROF_OPT $BOOT $CLASS_TO_RUN
	;;
    background)
	$JAVA $JAVA_OPTS $JONAS_OPTS $TOMCAT_OPTS $JETTY_OPTS $PROF_OPT $BOOT $CLASS_TO_RUN &
	$JAVA $JONAS_OPTS $TOMCAT_OPTS $JETTY_OPTS $BOOT org.objectweb.jonas.adm.JonasAdmin -ping $NAME_OPT
	;;
    window)
	xterm -title $HOSTNAME:$JONASNAME -geometry 110x16 -sb -e jonas start -fg &
	;;
    esac
else
    $JAVA $JONAS_OPTS $TOMCAT_OPTS $JETTY_OPTS $BOOT $CLASS_TO_RUN $NAME_OPT $ARGS
fi
