#! /bin/bash
. acsstartupConstants

#
# The container is started by acsStartJava.
#
# The original command line is passed to acsStartJava but it is manipulated.
# This script does the followings:
#    * Get the manager reference to be set in the  ACS.manager java property
#    * The name of the container (first param in cmd line) is prepended by 
#      -containerName to be recognized by acsStartJava
#    * -endorsed is added by default
#    * optionally set debug port
#    * optionally set an ORB profiler
#    * set the classpath for component-impl jars

###
### ----------- 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 -------------


#--------------------------------------------------------------------------------
# For historical reasons we enable remote debugging if stdout debug logs are enabled.
# If we clean this up (e.g. by using a dedicated flag), then we could also  
# pass the -debugport flag to acsStartJava instead of setting up the JVM debug options directly.
# The container will listen for debuggers one port above its ORB 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 -DACS.manager=$CL_MANAGERREF $ORIGINAL_COMMAND_LINE -containerName $CONTAINER_NAME 