#! /bin/bash
. acsstartupConstants

###
### ----------- Command Line Parsing ---------------------

if [ "`uname`" = "Linux" ]; then enable -n echo; fi

THIS=`basename $0`

#
# These will contain the parsing results (CL_XXX, CL = command line)
#
CL_MANAGERREF=

#
# These options can be recognized (longopts comma-separated. colon means argument is required)
#
LONGOPTS=managerReference:
SHORTOPTS=m:

# Run getopt (posixly_correct needed). 
export POSIXLY_CORRECT=1

set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`

# skip over the script name (command line arg 0)
#--------------------------------------------------------------------------------
shift
CONTAINER_NAME=$1


# skip over the container name (command line arg 1), but save the remainder of the command line for future use
shift;
ORIGINAL_COMMAND_LINE=$@

# skip over the java class name (command line arg 2)
shift

#
# Iterate over getopt's output and set CL_XXX variables accordingly
#
while : 
do
	case "$1" in
	--managerReference) CL_MANAGERREF=$2 ; break ;;
	-m)                 CL_MANAGERREF=$2 ; break ;;
	--) break ;;
	esac
	shift
done
shift

if [ "X$CL_MANAGERREF" != "X" ] 
then
   export MANAGER_REFERENCE=$CL_MANAGERREF
fi

# restore 
export POSIXLY_CORRECT=
unset POSIXLY_CORRECT

### ---------- End of Command Line Parsing -------------

#--------------------------------------------------------------------------------
#figure out the remote debuggable port
if [ "X$ACS_LOG_STDOUT" != "X" ] && [ $ACS_LOG_STDOUT -lt $ACS_DEBUG_PRIORITY ]
then

    temp="$ORIGINAL_COMMAND_LINE"
    RDB_PORT=${temp##*OAport }
    RDB_PORT=${RDB_PORT%%' '*}
    RDB_PORT=$(( $RDB_PORT + 1 ))

    #set the remote debuggable port
    export JAVA_OPTIONS="$JAVA_OPTIONS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$RDB_PORT"
    echo "Container will accept remote debuggers on port $RDB_PORT"
fi

#--------------------------------------------------------------------------------
# Install an ORB profiler. 
# For special profiling needs, another class implementing interface "org.jacorb.orb.acs.AcsORBProfiler" can be set in env var JAVA_OPTIONS_ORB_PROFILER;
# it must provide a constructor with a java.util.Logger parameter. 
if [ X"$JAVA_OPTIONS_ORB_PROFILER" = X ]
then
    JAVA_OPTIONS_ORB_PROFILER="-Dalma.acs.orb.profiler.class=alma.acs.container.corba.ContainerOrbProfiler"
fi
JAVA_OPTIONS="$JAVA_OPTIONS $JAVA_OPTIONS_ORB_PROFILER"


#--------------------------------------------------------------------------------
# Special container checks
if [ "X$ACS_LOG_STDOUT" != "X" ] && [ $ACS_LOG_STDOUT -lt $ACS_DEBUG_PRIORITY ]
then
    # enable corba return value checks by alma.acs.container.ContainerSealant
    export JAVA_OPTIONS="$JAVA_OPTIONS -Dalma.acs.container.check_nulls_corba_out=true"
fi

#--------------------------------------------------------------------------------
## set the classpath for component-impl jars (to support separate classloaders)
component_classpath="`vltMakeJavaClasspath -components`"
export JAVA_OPTIONS="$JAVA_OPTIONS -Dacs.components.classpath.jardirs=\"$component_classpath\""

#--------------------------------------------------------------------------------
#just delegate to acsStartJava
export JVM_GC_LOG_NAME=$CONTAINER_NAME


acsStartJava -endorsed -DORBgiopMaxMsgSize=25000000 -DACS.manager=$CL_MANAGERREF $ORIGINAL_COMMAND_LINE -containerName $CONTAINER_NAME 
