#!/bin/sh
#########################################################################
# (c) 2025 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
script_dir=`cd "\`dirname "$filename"\`"; pwd`
executable_name=protoc-gen-connext-cpp
script_version=7.7.0.0
protobuf_version=33.0
needs_shared_libraries=false

# Check if the binary
# Depending on the build we are shipping the dynamic or the static executable
# Use find since platform_name is not yet defined at this point
bin_search_dir="$script_dir/../resource/app/bin"
if [ -z "$(find "$bin_search_dir" -name "$executable_name" -type f 2>/dev/null)" ]
then
    if [ -n "$(find "$bin_search_dir" -name "protoc-gen-connext-cppz" -type f 2>/dev/null)" ]
    then
        executable_name="protoc-gen-connext-cppz"
    elif [ -n "$(find "$bin_search_dir" -name "protoc-gen-connext-cppdz" -type f 2>/dev/null)" ]
    then
        executable_name="protoc-gen-connext-cppdz"
    elif [ -n "$(find "$bin_search_dir" -name "protoc-gen-connext-cppd" -type f 2>/dev/null)" ]
    then
        executable_name="protoc-gen-connext-cppd"
    fi
fi

rticommon_script_dir=$script_dir/../resource/scripts
rticommon_script_name=rticommon.sh


if [ ! -f "$rticommon_script_dir/$rticommon_script_name" ]
then
    rticommon_script_dir=$script_dir/../../ndds.4.1/resource/scripts
    lib_dir=$script_dir/../../ndds.4.1/lib
    bin_dir=$script_dir/../lib
fi

. "$rticommon_script_dir/$rticommon_script_name"

# Add Protobuf library folder to LD_ and DYLD_LIBRARY_PATH environment variables
LD_LIBRARY_PATH="$NDDSHOME/third_party/protobuf-$protobuf_version/$platform_name/release/lib":$LD_LIBRARY_PATH; export LD_LIBRARY_PATH
DYLD_LIBRARY_PATH="$NDDSHOME/third_party/protobuf-$protobuf_version/$platform_name/release/lib":$DYLD_LIBRARY_PATH; export DYLD_LIBRARY_PATH

"$bin_dir/$platform_name/$executable_name" "$@"
