#!/bin/sh
# ---------------------------------------------------------------------------
# Fractal
# Copyright (C) 2006 INRIA
# Contact: fractal@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 Merle
#
# Contributor(s):
#
# ---------------------------------------------------------------------------
# $Id$
# ---------------------------------------------------------------------------

# -----------------------------------------------------------------------
# Check FDF_HOME
# -----------------------------------------------------------------------

if [ -z "${FDF_HOME}" ]
then
  FDF_HOME=${JONAS_ROOT}/lib/fdf
fi
 
# -----------------------------------------------------------------------
# Check JAVA_HOME
# -----------------------------------------------------------------------

if [ -z "${JAVA_HOME}" ]
then
    JAVA=java
else
    JAVA=${JAVA_HOME}/bin/java
fi

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

case  $1 
  in "launcher")
   MODE=$1
   if [ "$2" == "-cp" ]
	then
   	CLASSPATH=$CLASSPATH:$3
 	  ARGS="-fractal $4 r $5"
 	  while [ "$#" -gt 0 ]
 	   do    	
 	     ARGS="$ARGS $1"
 	     shift 	    
 	   done
   else 
     I=`expr index "$2" /`
     
     if [ "$I" -gt 0 ]
     then 
     	J=$I
     	STG=${2:$I}
     	while [ "$I" -gt 0 ]
     	do
     		I=`expr index "$STG" /`	 
     		if [ "$I" -gt 0 ]
     		then
     			J=$J+$I
     			STG=${STG:$I}
     	   fi
     	done
     	CLASSPATH=$CLASSPATH:${2:0:$J}     	
     	ARGS="-fractal $STG r $3"
     	echo $CLASSPATH
     	echo $ARGS     	
     else    
	 	  ARGS="-fractal $2 r $3"
 		  while [ "$#" -gt 0 ]
 		   do    	
 		     ARGS="$ARGS $1"
 		     shift 	    
 		   done
   	fi
   fi
   ;;
  *)
    echo "Usage: fdf launcher [-cp ADL_directory] ADL_name {install|start|stop|uninstall}"
 	 exit 1
 	 ;;
esac

# -----------------------------------------------------------------------
# Set Fractal options
# -----------------------------------------------------------------------

FRACTAL_OPTS="\
    -Djava.security.policy=${JONAS_ROOT}/conf/java.policy \
    -Dfractal.provider=org.objectweb.fractal.julia.Julia \
    -Djulia.config=julia.cfg \
    -Djulia.loader=org.objectweb.fractal.julia.loader.DynamicLoader \
    -Djulia.loader.use-context-class-loader=true "

# -----------------------------------------------------------------------
# Set the CLASSPATH
# -----------------------------------------------------------------------

CLASSPATH=\
${JONAS_ROOT}/examples/fdf:\
${ANT_HOME}/lib/ant.jar:\
${FDF_HOME}/ant-jsch.jar:\
${FDF_HOME}/asm-2.0.jar:\
${JONAS_ROOT}/lib/commons/jonas/jakarta-commons/commons-codec-1.3.jar:\
${FDF_HOME}/commons-httpclient-3.0.jar:\
${FDF_HOME}/commons-logging.jar:\
${FDF_HOME}/commons-net-1.4.1.jar:\
${FDF_HOME}/dom4j-1.6.jar:\
${FDF_HOME}/dtdparser.jar:\
${FDF_HOME}/fdf.jar:\
${FDF_HOME}/fractal-adl.jar:\
${FDF_HOME}/fractal.jar:\
${FDF_HOME}/ganymed-ssh2-build209.jar:\
${FDF_HOME}/jsch-0.1.26.jar:\
${FDF_HOME}/julia-asm-2.0.3.jar:\
${FDF_HOME}/julia-mixins-2.0.3.jar:\
${FDF_HOME}/julia-runtime-2.0.3.jar:\
${FDF_HOME}/ow_deployment_scheduling.jar:\
${FDF_HOME}/ow-monolog-2.1.1.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_api.jar:\
${FDF_HOME}/ow_util_trace.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_file.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_helper.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_remote.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_wrp_common.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_wrp_javalog.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_wrp_log4j.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_wrp_p6spy.jar:\
${JONAS_ROOT}/lib/commons/jonas/monolog/ow_util_log_wrp_pw.jar:\
${JONAS_ROOT}/lib/commons/jonas/jakarta-commons/commons-logging-api.jar:\
${CLASSPATH}

export CLASSPATH

# -----------------------------------------------------------------------
# Set the Fractal main class to run
# -----------------------------------------------------------------------

case "$MODE" in
launcher)
  CLASS_TO_RUN=org.objectweb.fractal.deployment.launcher.Launcher
  ;;
*)
  echo "Usage: fdf launcher [-cp ADL_directory] ADL_name {install|start|stop|uninstall}"
  exit 1
  ;;
esac

# -----------------------------------------------------------------------
# Run the Fractal main class
# -----------------------------------------------------------------------

exec ${JAVA} ${FRACTAL_OPTS} ${JAVA_OPTS} ${CLASS_TO_RUN} ${ARGS}


