#!/bin/bash
#########################################################################
# (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_location=`cd "\`dirname "$0"\`"; pwd`
script_version=7.7.0.0
needs_shared_libraries=true

# The following four strings will be replace when executing the scripts/build.xml
STARTUP_JAR=org.eclipse.equinox.launcher_1.7.0.v20250519-0528.jar
LAUNCHER_X64LINUX_LIBDIR=org.eclipse.equinox.launcher.gtk.linux.x86_64_1.2.1400.v20250331-1702
LAUNCHER_DARWIN_LIBDIR=org.eclipse.equinox.launcher.cocoa.macosx.x86_64_1.2.1300.v20250331-1702
LAUNCHER_ARM64DARWIN_LIBDIR_STRING=org.eclipse.equinox.launcher.cocoa.macosx.aarch64_1.2.1300.v20250331-1702

app_name="admin_console"
executable_name="RTI_AdminConsole"
os=`uname -s`

# Running on Linux
if [ "$os" = "Linux" ]
then
    script_dir=$script_location
    rticommon_script_dir="$script_dir/../resource/scripts"
    . "$rticommon_script_dir/rticommon.sh"

    if [ "$jre_platform" = "x64Linux" ]
    then
        launcher_lib_dir=$LAUNCHER_X64LINUX_LIBDIR
    else
        echo "Unsupported architecture, only 64 bit architectures are supported. Please, contact support@rti.com."
        exit
    fi

    startup_jar="$eclipse_dir/plugins/$STARTUP_JAR"
    launcher_lib="$eclipse_dir/plugins/$launcher_lib_dir"

    if [ "x$launcher_lib_dir" = "x" ] ||
	[ ! -f "$startup_jar" ] ||
	[ ! -d "$launcher_lib" ]; then
        echo "Required file or folder cannot be found. Please, contact support@rti.com."
        exit
    fi

    path_exe="$bin_dir/$platform_name/$executable_name"
    shared_config="$app_support_dir/$app_name/$jre_platform/configuration"

    # Wayland UI is not supported with the current OpenJDK version
    if [ -n "WAYLAND_DISPLAY" ] ; then
        echo "Wayland display not supported. Admin Console will be executed using x11"
        export GDK_BACKEND=x11
    fi

# Running from within RTI_AdminConsole.app on Darwin
elif [ "$os" = "Darwin" ] && [ -d "$script_location/../MacOS" ]
then
    script_dir=$script_location/../../../../../..
    rticommon_script_dir="$script_dir/../resource/scripts"
    . "$rticommon_script_dir/rticommon.sh"

    startup_jar="$eclipse_dir/plugins/$STARTUP_JAR"

    if [ "$jre_platform" = "arm64Darwin" ];
    then
        launcher_lib="$eclipse_dir/plugins/$LAUNCHER_ARM64DARWIN_LIBDIR_STRING"
    else
        launcher_lib="$eclipse_dir/plugins/$LAUNCHER_DARWIN_LIBDIR"
    fi

    if [ ! -d "$app_lib_dir/$platform_name" ] ||
        [ ! -f "$startup_jar" ] ||
        [ ! -d "$launcher_lib" ]; then
        echo "Required file or folder cannot be found. Please, contact support@rti.com."
        exit
    fi

    path_exe="$script_location/../../RTI_AdminConsole/Contents/MacOS/RTI_AdminConsole"
    shared_config="$app_support_dir/$app_name/$jre_platform/configuration"
    arch_specific_options="-noSplash"

# Running from bin/rtiadminconsole on Darwin
elif [ "$os" = "Darwin" ]
then
    script_dir=$script_location
    run_open=true

    rticommon_script_dir="$script_dir/../resource/scripts"
    . "$rticommon_script_dir/rticommon.sh"
else
    echo "Platform $os not supported. Please, contact support@rti.com."
    exit
fi

JEP_LOCATION="$app_support_dir/$app_name/$jre_platform/jep/"

if [ "$os" = "Darwin" ] 
then
    # check if Admin Console is already running
    ac_pids=`pgrep $executable_name`
    if [ "x$ac_pids" != "x" ]
    then
        # admin console is already running, activate the existing one
        touch $home_user_config_dir/$app_name/.activate

        # Wait up to 5 seconds to check if the application is being restarted
        acStarted=false
        for i in {1..5}
        do
            sleep 1
            ac_pids=`pgrep $executable_name`
            if [ "x$ac_pids" != "x" ]
            then
                acStarted=false
            else 
                rm -f $home_user_config_dir/$app_name/.lock
                acStarted=true
                break
            fi
        done

        if [ acStarted == false ]
        then
            exit
        fi

    else
        # remove the lock file if it exists (to avoid a situation where the
        # application won't start because it had been killed the last time it
        # was run)
        rm -f $home_user_config_dir/$app_name/.lock
    fi
fi

# Validate the JRE version to use when running Admin Console
. "$script_dir/../resource/app/app_support/scripts/validate_jre_version.sh"

# Prepare additional information about the Python configuration if possible
. "$app_support_dir/$app_name/$jre_platform/configure_python_lib.sh"

# do not use the SWT GTK3 version as our tree tables do not (currently) display
# correctly with it
export SWT_GTK3=0
# Set the JFX path if not manually assigned
if [ -z "$PATH_TO_FX" ]
then
    PATH_TO_FX="$app_support_dir/$app_name/$jre_platform/javafx/lib"
    # And then append its value to the (DY)LD_LIBRARY_PATH
    LD_LIBRARY_PATH="$PATH_TO_FX":$LD_LIBRARY_PATH; export LD_LIBRARY_PATH
    DYLD_LIBRARY_PATH="$PATH_TO_FX":$DYLD_LIBRARY_PATH; export DYLD_LIBRARY_PATH
fi

# Disable Monitoring Library 2.0 if not set by default
if [ -z "$RTI_MONITORING2_ENABLE" ]
then
    export RTI_MONITORING2_ENABLE="off"
fi

# Run RTI Admin Console
if [ "x$run_open" = "xtrue" ]
then
    open -g "$bin_dir/$platform_name/$executable_name.app"
else
    "$path_exe" -startup "$startup_jar" \
    --launcher.library "$launcher_lib" \
    -install "$eclipse_dir" \
    -configuration "$home_user_config_dir/$app_name/$jreArch/configuration" \
    -data "$home_user_config_dir/$app_name/$jreArch" \
    -vm "$JREHOME/bin/java" $arch_specific_options \
    -vmargs -Dosgi.configuration.cascaded=true \
    $connext_version_system_property $connext_platform_system_property $connext_workspace_system_property \
    -Defxclipse.java-modules.dir="$PATH_TO_FX" \
    -DjepPackage="$JEP_LOCATION" \
    -DacArch="$jreArch" \
    -Dosgi.sharedConfiguration.area="$shared_config" &
fi
