#!/bin/sh
#########################################################################
# (c) 2005-2014 Copyright, Real-Time Innovations. All rights reserved.  #
# No duplications, whole or partial, manual or electronic, may be made  #
# without express written permission.  Any such copies, or              #
# revisions thereof, must display this notice unaltered.                #
#This code contains trade secrets of Real - Time Innovations, Inc.#
#########################################################################

filename=$0
collect_telemetry="false"
script_dir=`cd "\`dirname "$filename"\`"; pwd`
application_name=rtiddsgen
script_version=7.7.0.0
needs_shared_libraries="false"
needs_platform_name="false"

JVMOPTS=""
if [ ! -z "$RTIDDSGEN_JVM_OPTIMIZATION" ]
then
JVMOPTS=" -XX:+TieredCompilation"
JVMOPTS=" -XX:TieredStopAtLevel=1"
JVMOPTS=" -XX:CICompilerCount=4"
JVMOPTS=" -Xverify:none"
JVMOPTS=" -XX:+UseParallelGC"
JVMOPTS=" -XX:+OptimizeStringConcat"
JVMOPTS=" -XX:CompileThreshold=5000"
fi
if [ -f "$script_dir/../resource/scripts/rticommon.sh" ]
then
    rticommon_script_dir=$script_dir/../resource/scripts
    . "$rticommon_script_dir/rticommon.sh"
    rtiddsgen2_jar=$app_lib_java_dir/rtiddsgen2.jar
else
    rticommon_script_dir=$script_dir/../../ndds.4.1/resource/scripts
    . "$rticommon_script_dir/rticommon.sh"
    app_support_dir=$script_dir/../resource
    rtiddsgen2_jar=$script_dir/../class/rtiddsgen2.jar
fi

ndds_resource_dir=$app_support_dir/$application_name

if [ -f "$app_lib_java_dir/rtiddsgen_dotnet.jar" ]
then
    for arg in "$@"
    do
        if [ "c#" = "$arg" ] || [ "C#" = "$arg" ] || [ "-help" = "$arg" ] || [ "-h" = "$arg" ]
        then

            # With language C# selected, and rtiddsgen_dotnet.jar installed
            # we run the patched rtiddsgen version for the new C# binding;
            # in any other case we run the regular 6.1.0 rtiddsgen.
            rtiddsgen2_jar=$app_lib_java_dir/rtiddsgen_dotnet.jar
            ndds_resource_dir=$app_support_dir/rtiddsgen_dotnet
            break
        fi
    done
fi

# Decide whether it tries to run rtiddsgen1 based on unsupported arguments
rtiddsgen1_arguments="-corba -orb -dataReaderSuffix -dataWriterSuffix -xml"
conditions_for_optional_arguments="-xml"
optional_arguments=

for arg in "$@"
do
    for rtiddsgen1_arg in $rtiddsgen1_arguments
    do
    if [ "x$conditions_for_optional_arguments" = "x$arg" ]
    then
        optional_arguments="-noCodeGeneration"
    fi
	if [ "x$rtiddsgen1_arg" = "x$arg" ]
	then
	    run_rtiddsgen1=true
	    break
	fi
    done
done

if [ "x$JREHOME" = "x" ] || [ ! -x "$JREHOME" ]
then
    echo "Warning: OS $os may not be supported. Be sure JREHOME is set."
    echo $JREHOME
    exit 1
fi

if [ "x$run_rtiddsgen1" = "x" ]
then
    "$JREHOME/bin/java" \
    $JVMOPTS \
	-DNDDS_RESOURCE_DIR="$ndds_resource_dir" -DNDDSHOME="$NDDSHOME" \
    $connext_version_system_property $connext_platform_system_property $connext_workspace_system_property \
	-jar "$rtiddsgen2_jar" \
	"$@"

else
    # Message rtiddsgen1 not supported
    echo "Corba is not supported in this version of RTI Connext. Please contact"
        echo "support@rti.com for more information."
        exit 1
fi
