#!/bin/sh
# ---------------------------------------------------------------------------
# JOnAS: Java(TM) Open Application Server
# Copyright  2007 Bull S.A.S.
# Contact: jonas-team@ow2.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
# eyindanga
# ---------------------------------------------------------------------------
# $Id: j5 12397 2007-12-14 13:12:45Z fornacif $
# ---------------------------------------------------------------------------

echo "-------------------------------------------------------------------"
echo "-            JOnAS 5 - OSGi on Unix platform                      -"
echo "-            JOnAS 5: Java(TM) Open Application Server            -"
echo "-            http://jonas.ow2.org                                 -"
echo "-            Contact: jonas-team@ow2.org                          -"
echo "-------------------------------------------------------------------"

# -----------------------------------------------------------------------
# Compute JONAS_ROOT in case it has not been set.
# -----------------------------------------------------------------------
if [ ! -d $JONAS_ROOT/lib ]; then
  savewd=`pwd`
  cd `dirname $0`
  zdir=`pwd`
  cd $savewd
  zfqn=$zdir/`basename "$0"`
  JONAS_ROOT=`dirname $zdir`
  JONAS_ROOT=`dirname $JONAS_ROOT`
  export JONAS_ROOT
fi

# Set CLASSPATH and JONAS_BASE
. $JONAS_ROOT/bin/setenv

# Set path for JOnAS bootstrap libraries
LIB_BOOTSTRAP_PATH=$JONAS_ROOT/lib/bootstrap

# Update JONAS_CLASSPATH
JONAS_CLASSPATH=$JONAS_CLASSPATH$SPS$LIB_BOOTSTRAP_PATH/felix-launcher.jar
JONAS_CLASSPATH=$JONAS_CLASSPATH$SPS$LIB_BOOTSTRAP_PATH/jonas-commands.jar
JONAS_CLASSPATH=$JONAS_CLASSPATH$SPS$LIB_BOOTSTRAP_PATH/carol.jar
JONAS_CLASSPATH=$JONAS_CLASSPATH$SPS$LIB_BOOTSTRAP_PATH/commons-logging.jar
JONAS_CLASSPATH=$JONAS_CLASSPATH$SPS$LIB_BOOTSTRAP_PATH/jonas-version.jar

JONAS_OPTS="\
 -Djonas.root=$JONAS_ROOT \
 -Djonas.base=$JONAS_BASE \
 -Djava.security.manager \
 -Djava.security.policy=$JONAS_BASE/conf/java.policy \
 -Djava.security.auth.login.config=$JONAS_BASE/conf/jaas.config \
 -Djava.endorsed.dirs=$JONAS_ROOT/lib/endorsed \
 -Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB \
 -Dorg.omg.CORBA.ORBSingletonClass=org.jacorb.orb.ORBSingleton \
 -Dorg.omg.PortableInterceptor.ORBInitializerClass.standard_init=org.jacorb.orb.standardInterceptors.IORInterceptorInitializer \
 -Djavax.rmi.CORBA.PortableRemoteObjectClass=org.ow2.carol.rmi.multi.MultiPRODelegate \
 -Djava.naming.factory.initial=org.ow2.carol.jndi.spi.MultiOrbInitialContextFactory \
 -Djavax.rmi.CORBA.UtilClass=org.ow2.carol.util.delegate.UtilDelegateImpl \
 "
# Most of the ORB and naming properties are not usually changed by the user !! Should be in a class.

# ---------------------------------------------
# Get arguments
# ---------------------------------------------
ARGS=
MODE=
JONAS_NAME=
PINGTIMEOUT=
JONAS_NAME="jonas"
HEADLESS="-Djava.awt.headless=true"
START_OPTS=
PINGTIMEOUT_OPT=
WHERE=background
FORCE_FOREGROUND=false
ADMIN_CLASS=org.ow2.jonas.commands.admin.ClientAdmin
while [ "$#" -gt 0 ]
  do case "$1" in
    start)
      MODE=start
      ;;
    stop)
      MODE=stop
      ;;
    halt)
      MODE=halt
      ;;
    admin)
      MODE=admin
      ;;
    version)
      MODE=version
      ;;
    check)
      MODE=check
      ;;
    bootstrap)
      MODE=start
      echo "Start JOnAS Bootstrap only."
      START_OPTS="$START_OPTS -Djonas.bootstrap=true"
      ;;
    -cp)
      shift
      JONAS_CLASSPATH=$JONAS_CLASSPATH$SPS$1
      ;;
    -debug)
      shift
      # $1=-p
      if [ "$1" != "-p" ]; then
        echo "-debug mode need a following \"-p \""
        exit 1
      fi
      shift
      JONAS_DEBUG_PORT=$1
      JONAS_DEBUG_SUSPEND=n
      if [ "$2" = "-s" ]; then
        shift
        shift
        JONAS_DEBUG_SUSPEND=$1
      fi
      echo "JOnAS Debug Info :"
      echo " listening on port : $JONAS_DEBUG_PORT"
      echo " suspend mode : $JONAS_DEBUG_SUSPEND"
      JONAS_DEBUG_OPTS=" -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=$JONAS_DEBUG_PORT,suspend=$JONAS_DEBUG_SUSPEND"
      ;;
    -fg)
      WHERE=foreground
      ;;
    -gui)
      echo "Start Felix GUI with JOnAS."
      START_OPTS="$START_OPTS -Djonas.felix.gui.enabled=true"
      HEADLESS=""
      ;;
    -tui)
      echo "Start Felix TUI with JOnAS."
      START_OPTS="$START_OPTS -Djonas.felix.tui.enabled=true"
      FORCE_FOREGROUND=true
      ;;
    -win)
      WHERE=window
      ;;
    -nojit)
      JONAS_OPTS="$JONAS_OPTS -Djava.compiler=NONE"
      START_OPTS="$START_OPTS -Djava.compiler=NONE"
      ;;
    -dev)
      START_OPTS="$START_OPTS -Djonas.developer=true"
      ;;
    -clean)
      echo "Clean Felix cache before starting JOnAS."
      START_OPTS="$START_OPTS -Djonas.cache.clean=true"
      ;;
    -n)
      shift
      JONAS_NAME=$1
      ;;
    -timeout)
      shift
      PINGTIMEOUT=$1
      ;;
    -D*)
      JONAS_OPTS="$JONAS_OPTS $1"
      START_OPTS="$START_OPTS $1"
      ;;
    -target)
      if [ "$MODE" = "start" ]; then
        ARGS="$ARGS -start"
      fi
# -------------------------------------
# Not supported argument
#     if [ "$MODE" = "stop" ]; then
#       ARGS="$ARGS -stop"
#     fi
# -------------------------------------
      if [ "$MODE" = "halt" ]; then
        ARGS="$ARGS -halt"
      fi
      MODE=admin
      ARGS="$ARGS -target"
      ;;
    *)
      # 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="$ADMIN_CLASS -start"
    ;;
  stop)
    CLASS_TO_RUN="$ADMIN_CLASS -s"
    ;;
  halt)
    CLASS_TO_RUN="$ADMIN_CLASS -halt"
    ;;
  admin)
    CLASS_TO_RUN=$ADMIN_CLASS
    ;;
  version)
    CLASS_TO_RUN=org.ow2.jonas.Version
    ;;
  check)
    CLASS_TO_RUN=org.ow2.jonas.commands.check.CheckEnv
    ;;
  *)
    # Usage
    # Will be displayed if the user type an unrecognized command
    echo "Unrecognized command : '$*'"
    echo "Usage: $0 start | stop | bootstrap | halt | admin | version | check"
    echo "Options:"
    echo "  for 'start|bootstrap': [-fg] [-win] [-debug] [-gui] [-tui] [-dev] [-clean] [-nojit]"
    echo "  -fg                         Launch JOnAS in foreground (Only applicable for 'start')."
    echo "  -win                        Launch JOnAS in a separate window (Only applicable for 'start')."
    echo "  -debug -p <port> [-s <y/n>] Debug the JOnAS instance."
    echo "  -gui                        Launch JOnAS with the Apache Felix GUI (Only applicable for 'start')."
    echo "  -tui                        Launch JOnAS with the Apache Felix Shell"
    echo "                              (allow to perform OSGi operations on the framework)."
    echo "                              (Only applicable for 'start' and 'bootstrap')"
    echo "  -dev                        Use M2 registry for bundles (for JOnAS developers)."
    echo "  -clean                      Clean Felix cache before starting JOnAS."
    echo "  -nojit                      Disable JIT."
    echo " for '*': [-cp] [-n] [-timeout] [-target] [-D*]"
    echo "  -cp <classpath>             Additional CLASSPATH entries (to be used with care)."
    echo "  -n <jonas-name>             Specify the JOnAS instance name."
    echo "  -timeout <timeout>          Specify a new connection timeout."
    echo "  -target [options]           Perform remote administration tasks."
    echo "  -Dname=value                Add JVM properties."
    exit 1
    ;;
esac

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

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

# ---------------------------------------------
# Set jonas server name
# ---------------------------------------------
if [ ! -z "$JONAS_NAME" ]; then
  JONAS_OPTS="$JONAS_OPTS -Djonas.name=$JONAS_NAME "
  START_OPTS="$START_OPTS -Djonas.name=$JONAS_NAME "
fi

# ---------------------------------------------
# Set jonas timeout for ping
# ---------------------------------------------
if [ ! -z "$PINGTIMEOUT" ]; then
  PINGTIMEOUT_OPT="-timeout $PINGTIMEOUT"
fi

# Add Headless if needed
START_OPTS="$START_OPTS $HEADLESS"

# Force foreground mode
if [ "$FORCE_FOREGROUND" = true ]; then
 WHERE=foreground
fi

# ---------------------------------------------
# Run java command
# ---------------------------------------------
if [ "$MODE" = "start" ]; then
 # Launching JOnAS
 case "$WHERE" in
 foreground)
   $JAVA $JAVA_OPTS $START_OPTS $JONAS_OPTS -cp $JONAS_CLASSPATH $JONAS_DEBUG_OPTS $ARGS $CLASS_TO_RUN
   ;;
 background)
   $JAVA $JAVA_OPTS $START_OPTS $JONAS_OPTS -cp $JONAS_CLASSPATH $JONAS_DEBUG_OPTS $ARGS $CLASS_TO_RUN&
   $JAVA $JAVA_OPTS $START_OPTS $JONAS_OPTS -cp $JONAS_CLASSPATH $ADMIN_CLASS -ping $PINGTIMEOUT_OPT
   ;;
 window)
   xterm -title $HOSTNAME:$JONASNAME -geometry 110x16 -sb -e $0 start -fg $JONAS_DEBUG_OPTS $START_OPTS $ARGS&
   ;;
 esac
else
  # Stop / Halt / Check / Admin / ...
  $JAVA $JONAS_OPTS -cp $JONAS_CLASSPATH $CLASS_TO_RUN $ARGS
fi
