#!/bin/sh
# ---------------------------------------------------------------------------
# JOnAS: Java(TM) Open Application Server
# Copyright (C) 1999-2008 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): Nicolas Tachker - Benoit Pelletier
# ---------------------------------------------------------------------------
# $Id$
# ---------------------------------------------------------------------------

# CYGWIN specific support.
cygwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
esac


# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin; then
  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$JONAS_ROOT" ] && JONAS_ROOT=`cygpath --unix "$JONAS_ROOT"`
  [ -n "$JONAS_BASE" ] && JONAS_BASE=`cygpath --unix "$JONAS_BASE"`
  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi

[ ! -n "$JONAS_BASE" ] && JONAS_BASE="$JONAS_ROOT"

# Verify if JAVA_HOME is well defined
if [ ! -r "$JAVA_HOME"/bin/java ]; then
  echo "The JAVA_HOME environment variable is not defined correctly"
  echo "This environment variable is needed to run this program"
  exit 1
fi

# Verify if JORAM_HOME is well defined
if [ ! -r "$JONAS_ROOT"/bin/unix/joram_raconfig ]; then
  echo "The JONAS_ROOT environment variable is not defined correctly"
  echo "This environment variable is needed to run this program"
  exit 1
fi

JORAM_LIBS=$JONAS_ROOT/lib/commons/jonas/joram

# Building the Classpath
CLASSPATH=$JORAM_LIBS/joram-raconfig.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_api.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_core.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_file.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_helper.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_remote.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_wrp_common.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_wrp_javalog.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_wrp_log4j.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_wrp_p6spy.jar
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/jonas/monolog/ow_util_log_wrp_pw.jar

RARJORAM=$JONAS_BASE/rars/autoload/joram_for_jonas_ra.rar

if [ ! -f $RARJORAM ]
then
	echo "the JORAM RAR file <$RARJORAM> doesn't exist" ;
	exit 1 ;
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  RARJORAM=`cygpath --path --windows "$RARJORAM"`
  JONAS_BASE=`cygpath --path --windows "$JONAS_BASE"`
  JONAS_ROOT=`cygpath --path --windows "$JONAS_ROOT"`
fi

PRM="false";
PORT=16010;
HOST=localhost;
SERVER=0;
export PRM PORT HOST SERVER

while [ "$#" -gt 0 ]
do
  case "$1" in
  -p)
    shift
    PORT=$1;
    PRM="true";
    ;;
  -h)
    shift
    HOST=$1;
    PRM="true";
    ;;
  -s)
    shift
    SERVER=$1;
    PRM="true";
    ;;
  *)
   shift
   ;;
  esac
done

if [ "$PRM" = "false" ]
then
	echo "Usage : $0 -p <port> -h <host> -s <serverid>";
	exit 1;
fi

$JAVA_HOME/bin/java -classpath $CLASSPATH org.objectweb.joram.client.connector.utils.RAConfig -rar $RARJORAM -uhp $HOST $PORT $SERVER -conf $JONAS_BASE/conf
