#!/bin/sh

#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
#   teku start up script for POSIX generated by Gradle.
#
#   Important for running:
#
#   (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
#       noncompliant, but you have some other compliant shell such as ksh or
#       bash, then to run this script, type that shell name before the whole
#       command line, like:
#
#           ksh teku
#
#       Busybox and similar reduced shells will NOT work, because this script
#       requires all of these POSIX shell features:
#         * functions;
#         * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
#           «${var#prefix}», «${var%suffix}», and «$( cmd )»;
#         * compound commands having a testable exit status, especially «case»;
#         * various built-in commands including «command», «set», and «ulimit».
#
#   Important for patching:
#
#   (2) This script targets any POSIX shell, so it avoids extensions provided
#       by Bash, Ksh, etc; in particular arrays are avoided.
#
#       The "traditional" practice of packing multiple parameters into a
#       space-separated string is a well documented source of bugs and security
#       problems, so this is (mostly) avoided, by progressively accumulating
#       options in "$@", and eventually passing that to Java.
#
#       Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
#       and TEKU_OPTS) rely on word-splitting, this is performed explicitly;
#       see the in-line comments for details.
#
#       There are tweaks for specific operating systems such as AIX, CygWin,
#       Darwin, MinGW, and NonStop.
#
#   (3) This script is generated from the Groovy template
#       https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
#       within the Gradle project.
#
#       You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################

# Attempt to set APP_HOME

# Resolve links: $0 may be a link
app_path=$0

# Need this for daisy-chained symlinks.
while
    APP_HOME=${app_path%"${app_path##*/}"}  # leaves a trailing /; empty if no leading path
    [ -h "$app_path" ]
do
    ls=$( ls -ld "$app_path" )
    link=${ls#*' -> '}
    case $link in             #(
      /*)   app_path=$link ;; #(
      *)    app_path=$APP_HOME$link ;;
    esac
done

# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}.." > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum

warn () {
    echo "$*"
} >&2

die () {
    echo
    echo "$*"
    echo
    exit 1
} >&2

# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in                #(
  CYGWIN* )         cygwin=true  ;; #(
  Darwin* )         darwin=true  ;; #(
  MSYS* | MINGW* )  msys=true    ;; #(
  NONSTOP* )        nonstop=true ;;
esac

CLASSPATH=$APP_HOME/lib/teku-develop.jar:$APP_HOME/lib/teku-develop.jar:$APP_HOME/lib/teku-services-beaconchain-develop.jar:$APP_HOME/lib/teku-data-beaconrestapi-develop.jar:$APP_HOME/lib/teku-beacon-validator-develop.jar:$APP_HOME/lib/teku-services-bootnode-develop.jar:$APP_HOME/lib/teku-services-executionlayer-develop.jar:$APP_HOME/lib/teku-services-powchain-develop.jar:$APP_HOME/lib/teku-validator-client-develop.jar:$APP_HOME/lib/teku-validator-remote-develop.jar:$APP_HOME/lib/teku-data-provider-develop.jar:$APP_HOME/lib/teku-beacon-sync-develop.jar:$APP_HOME/lib/teku-networking-eth2-develop.jar:$APP_HOME/lib/teku-networking-p2p-develop.jar:$APP_HOME/lib/teku-validator-eventadapter-develop.jar:$APP_HOME/lib/teku-ethereum-statetransition-develop.jar:$APP_HOME/lib/teku-ethereum-weaksubjectivity-develop.jar:$APP_HOME/lib/teku-services-chainstorage-develop.jar:$APP_HOME/lib/teku-storage-develop.jar:$APP_HOME/lib/teku-beacon-pow-develop.jar:$APP_HOME/lib/teku-data-publisher-develop.jar:$APP_HOME/lib/teku-data-dataexchange-develop.jar:$APP_HOME/lib/teku-ethereum-dataproviders-develop.jar:$APP_HOME/lib/teku-ethereum-executionlayer-develop.jar:$APP_HOME/lib/teku-ethereum-executionclient-develop.jar:$APP_HOME/lib/teku-validator-beaconnode-develop.jar:$APP_HOME/lib/teku-validator-api-develop.jar:$APP_HOME/lib/teku-ethereum-json-types-develop.jar:$APP_HOME/lib/teku-ethereum-networks-develop.jar:$APP_HOME/lib/teku-storage-api-develop.jar:$APP_HOME/lib/teku-ethereum-pow-merkletree-develop.jar:$APP_HOME/lib/teku-data-serializer-develop.jar:$APP_HOME/lib/teku-ethereum-spec-develop.jar:$APP_HOME/lib/teku-ethereum-signingrecord-develop.jar:$APP_HOME/lib/teku-ethereum-pow-api-develop.jar:$APP_HOME/lib/teku-infrastructure-bls-develop.jar:$APP_HOME/lib/teku-infrastructure-restapi-develop.jar:$APP_HOME/lib/teku-networking-nat-develop.jar:$APP_HOME/lib/teku-services-timer-develop.jar:$APP_HOME/lib/teku-infrastructure-serviceutils-develop.jar:$APP_HOME/lib/teku-ethereum-events-develop.jar:$APP_HOME/lib/teku-infrastructure-events-develop.jar:$APP_HOME/lib/teku-infrastructure-async-develop.jar:$APP_HOME/lib/teku-ethereum-performance-trackers-develop.jar:$APP_HOME/lib/teku-infrastructure-metrics-develop.jar:$APP_HOME/lib/teku-infrastructure-ssz-develop.jar:$APP_HOME/lib/teku-ethereum-execution-types-develop.jar:$APP_HOME/lib/teku-infrastructure-crypto-develop.jar:$APP_HOME/lib/teku-infrastructure-kzg-develop.jar:$APP_HOME/lib/teku-infrastructure-json-develop.jar:$APP_HOME/lib/teku-infrastructure-http-develop.jar:$APP_HOME/lib/teku-infrastructure-io-develop.jar:$APP_HOME/lib/teku-infrastructure-logging-develop.jar:$APP_HOME/lib/teku-infrastructure-version-develop.jar:$APP_HOME/lib/teku-infrastructure-yaml-develop.jar:$APP_HOME/lib/teku-infrastructure-bls-keystore-develop.jar:$APP_HOME/lib/teku-infrastructure-time-develop.jar:$APP_HOME/lib/teku-infrastructure-unsigned-develop.jar:$APP_HOME/lib/teku-infrastructure-exceptions-develop.jar:$APP_HOME/lib/jvm-libp2p-1.2.2-RELEASE.jar:$APP_HOME/lib/plugin-api-25.4.1.jar:$APP_HOME/lib/evm-25.4.1.jar:$APP_HOME/lib/besu-datatypes-25.4.1.jar:$APP_HOME/lib/algorithms-25.4.1.jar:$APP_HOME/lib/rlp-25.4.1.jar:$APP_HOME/lib/metrics-core-25.4.1.jar:$APP_HOME/lib/teku-infrastructure-subscribers-develop.jar:$APP_HOME/lib/jblst-0.3.12.jar:$APP_HOME/lib/teku-infrastructure-bytes-develop.jar:$APP_HOME/lib/teku-infrastructure-collections-develop.jar:$APP_HOME/lib/discovery-25.4.0.jar:$APP_HOME/lib/grpc-netty-1.70.0.jar:$APP_HOME/lib/grpc-util-1.70.0.jar:$APP_HOME/lib/grpc-core-1.70.0.jar:$APP_HOME/lib/tuweni-ssz-2.7.0.jar:$APP_HOME/lib/tuweni-crypto-2.7.0.jar:$APP_HOME/lib/tuweni-io-2.7.0.jar:$APP_HOME/lib/grpc-context-1.70.0.jar:$APP_HOME/lib/grpc-api-1.70.0.jar:$APP_HOME/lib/guava-33.1.0-jre.jar:$APP_HOME/lib/swagger-core-2.2.30.jar:$APP_HOME/lib/commons-lang3-3.17.0.jar:$APP_HOME/lib/log4j-slf4j-impl-2.24.3.jar:$APP_HOME/lib/log4j-slf4j2-impl-2.24.3.jar:$APP_HOME/lib/log4j-core-2.24.3.jar:$APP_HOME/lib/log4j-api-2.24.3.jar:$APP_HOME/lib/commons-io-2.19.0.jar:$APP_HOME/lib/core-4.12.2.jar:$APP_HOME/lib/okhttp-eventsource-4.1.1.jar:$APP_HOME/lib/logging-interceptor-4.12.0.jar:$APP_HOME/lib/opentelemetry-exporter-otlp-1.47.0.jar:$APP_HOME/lib/opentelemetry-exporter-sender-okhttp-1.47.0.jar:$APP_HOME/lib/okhttp-4.12.0.jar:$APP_HOME/lib/picocli-4.7.7.jar:$APP_HOME/lib/tuweni-units-2.7.0.jar:$APP_HOME/lib/tuweni-rlp-2.7.0.jar:$APP_HOME/lib/tuweni-bytes-2.7.0.jar:$APP_HOME/lib/vertx-web-4.5.14.jar:$APP_HOME/lib/vertx-web-common-4.5.14.jar:$APP_HOME/lib/vertx-auth-common-4.5.14.jar:$APP_HOME/lib/vertx-bridge-common-4.5.14.jar:$APP_HOME/lib/vertx-core-4.5.14.jar:$APP_HOME/lib/jackson-module-kotlin-2.19.0.jar:$APP_HOME/lib/jackson-datatype-jsr310-2.19.0.jar:$APP_HOME/lib/jackson-core-2.19.0.jar:$APP_HOME/lib/swagger-models-2.2.30.jar:$APP_HOME/lib/jackson-annotations-2.19.0.jar:$APP_HOME/lib/jackson-dataformat-yaml-2.19.0.jar:$APP_HOME/lib/jjwt-jackson-0.12.6.jar:$APP_HOME/lib/crypto-4.12.2.jar:$APP_HOME/lib/jackson-databind-2.19.0.jar:$APP_HOME/lib/snappy-java-1.1.10.7.jar:$APP_HOME/lib/oshi-core-6.8.1.jar:$APP_HOME/lib/rocksdbjni-10.0.1.jar:$APP_HOME/lib/failureaccess-1.0.2.jar:$APP_HOME/lib/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:$APP_HOME/lib/jsr305-3.0.2.jar:$APP_HOME/lib/checker-qual-3.42.0.jar:$APP_HOME/lib/gson-2.11.0.jar:$APP_HOME/lib/caffeine-3.2.0.jar:$APP_HOME/lib/error_prone_annotations-2.36.0.jar:$APP_HOME/lib/fastutil-8.5.15.jar:$APP_HOME/lib/swagger-annotations-2.2.30.jar:$APP_HOME/lib/classgraph-4.8.179.jar:$APP_HOME/lib/javalin-6.6.0.jar:$APP_HOME/lib/swagger-ui-5.21.0.jar:$APP_HOME/lib/abi-4.12.2.jar:$APP_HOME/lib/rlp-4.12.2.jar:$APP_HOME/lib/utils-4.12.2.jar:$APP_HOME/lib/bctls-jdk18on-1.78.1.jar:$APP_HOME/lib/bcpkix-jdk18on-1.80.jar:$APP_HOME/lib/bcutil-jdk18on-1.80.jar:$APP_HOME/lib/bcprov-jdk18on-1.80.jar:$APP_HOME/lib/HdrHistogram-2.2.2.jar:$APP_HOME/lib/jjwt-impl-0.12.6.jar:$APP_HOME/lib/jjwt-api-0.12.6.jar:$APP_HOME/lib/snakeyaml-2.4.jar:$APP_HOME/lib/mimeparse-0.1.3.3.jar:$APP_HOME/lib/jna-platform-5.17.0.jar:$APP_HOME/lib/arithmetic-1.3.0.jar:$APP_HOME/lib/gnark-1.3.0.jar:$APP_HOME/lib/secp256k1-1.3.0.jar:$APP_HOME/lib/secp256r1-1.3.0.jar:$APP_HOME/lib/blake2bf-1.3.0.jar:$APP_HOME/lib/besu-native-common-1.3.0.jar:$APP_HOME/lib/jna-5.17.0.jar:$APP_HOME/lib/javalin-rendering-6.6.0.jar:$APP_HOME/lib/thymeleaf-3.1.3.RELEASE.jar:$APP_HOME/lib/netty-all-4.1.118.Final.jar:$APP_HOME/lib/netty-handler-proxy-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-http2-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-http-4.2.0.Final.jar:$APP_HOME/lib/netty-resolver-dns-native-macos-4.1.118.Final-osx-x86_64.jar:$APP_HOME/lib/netty-resolver-dns-native-macos-4.1.118.Final-osx-aarch_64.jar:$APP_HOME/lib/netty-resolver-dns-classes-macos-4.1.118.Final.jar:$APP_HOME/lib/netty-resolver-dns-4.1.118.Final.jar:$APP_HOME/lib/netty-handler-4.2.0.Final.jar:$APP_HOME/lib/reactor-core-3.7.5.jar:$APP_HOME/lib/org.jupnp.support-3.0.3.jar:$APP_HOME/lib/org.jupnp-3.0.3.jar:$APP_HOME/lib/quartz-2.5.0.jar:$APP_HOME/lib/leveldbjni-win64-1.8.jar:$APP_HOME/lib/leveldbjni-win32-1.8.jar:$APP_HOME/lib/leveldb-native-0.3.1.jar:$APP_HOME/lib/kotlin-reflect-1.9.25.jar:$APP_HOME/lib/kotlinx-coroutines-core-jvm-1.6.4.jar:$APP_HOME/lib/okio-jvm-3.6.0.jar:$APP_HOME/lib/kotlin-stdlib-jdk7-1.9.25.jar:$APP_HOME/lib/kotlin-stdlib-2.1.20.jar:$APP_HOME/lib/tuweni-units-2.4.2.jar:$APP_HOME/lib/tuweni-bytes-2.4.2.jar:$APP_HOME/lib/kotlin-stdlib-jdk8-1.9.25.jar:$APP_HOME/lib/Java-WebSocket-1.5.6.jar:$APP_HOME/lib/websocket-jetty-server-11.0.25.jar:$APP_HOME/lib/jetty-webapp-11.0.25.jar:$APP_HOME/lib/websocket-servlet-11.0.25.jar:$APP_HOME/lib/jetty-servlet-11.0.25.jar:$APP_HOME/lib/jetty-security-11.0.25.jar:$APP_HOME/lib/websocket-core-server-11.0.25.jar:$APP_HOME/lib/jetty-server-11.0.25.jar:$APP_HOME/lib/websocket-jetty-common-11.0.25.jar:$APP_HOME/lib/websocket-core-common-11.0.25.jar:$APP_HOME/lib/jetty-http-11.0.25.jar:$APP_HOME/lib/jetty-io-11.0.25.jar:$APP_HOME/lib/jetty-xml-11.0.25.jar:$APP_HOME/lib/jetty-util-11.0.25.jar:$APP_HOME/lib/slf4j-api-2.0.17.jar:$APP_HOME/lib/java-multibase-v1.1.1.jar:$APP_HOME/lib/noise-java-22.1.0.jar:$APP_HOME/lib/netty-transport-native-epoll-4.1.118.Final.jar:$APP_HOME/lib/netty-transport-native-epoll-4.1.118.Final-linux-x86_64.jar:$APP_HOME/lib/netty-transport-native-epoll-4.1.118.Final-linux-aarch_64.jar:$APP_HOME/lib/netty-transport-native-epoll-4.1.118.Final-linux-riscv64.jar:$APP_HOME/lib/netty-transport-classes-epoll-4.1.118.Final.jar:$APP_HOME/lib/netty-transport-native-kqueue-4.1.118.Final-osx-x86_64.jar:$APP_HOME/lib/netty-transport-native-kqueue-4.1.118.Final-osx-aarch_64.jar:$APP_HOME/lib/netty-transport-classes-kqueue-4.1.118.Final.jar:$APP_HOME/lib/netty-transport-native-unix-common-4.2.0.Final.jar:$APP_HOME/lib/netty-codec-socks-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-dns-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-4.2.0.Final.jar:$APP_HOME/lib/netty-codec-compression-4.2.0.Final.jar:$APP_HOME/lib/netty-codec-protobuf-4.2.0.Final.jar:$APP_HOME/lib/netty-codec-marshalling-4.2.0.Final.jar:$APP_HOME/lib/netty-codec-base-4.2.0.Final.jar:$APP_HOME/lib/netty-transport-4.2.0.Final.jar:$APP_HOME/lib/netty-resolver-4.2.0.Final.jar:$APP_HOME/lib/netty-buffer-4.2.0.Final.jar:$APP_HOME/lib/netty-common-4.2.0.Final.jar:$APP_HOME/lib/protobuf-java-3.25.5.jar:$APP_HOME/lib/dagger-2.55.jar:$APP_HOME/lib/netty-tcnative-boringssl-static-2.0.70.Final.jar:$APP_HOME/lib/opentelemetry-sdk-extension-autoconfigure-1.47.0.jar:$APP_HOME/lib/opentelemetry-exporter-otlp-common-1.47.0.jar:$APP_HOME/lib/opentelemetry-exporter-common-1.47.0.jar:$APP_HOME/lib/opentelemetry-sdk-extension-autoconfigure-spi-1.47.0.jar:$APP_HOME/lib/opentelemetry-sdk-1.47.0.jar:$APP_HOME/lib/opentelemetry-sdk-trace-1.47.0.jar:$APP_HOME/lib/opentelemetry-sdk-metrics-1.47.0.jar:$APP_HOME/lib/opentelemetry-sdk-logs-1.47.0.jar:$APP_HOME/lib/opentelemetry-sdk-common-1.47.0.jar:$APP_HOME/lib/opentelemetry-api-1.47.0.jar:$APP_HOME/lib/opentelemetry-semconv-incubating-1.30.0-alpha.jar:$APP_HOME/lib/opentelemetry-semconv-1.30.0.jar:$APP_HOME/lib/prometheus-metrics-instrumentation-guava-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-instrumentation-jvm-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-core-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-exporter-httpserver-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-exporter-pushgateway-1.3.5.jar:$APP_HOME/lib/tuples-4.12.2.jar:$APP_HOME/lib/jnr-unixsocket-0.38.22.jar:$APP_HOME/lib/rxjava-2.2.21.jar:$APP_HOME/lib/ens-normalize-0.2.0.jar:$APP_HOME/lib/framework-1.3.2.jar:$APP_HOME/lib/framework-internal-1.3.2.jar:$APP_HOME/lib/jakarta.xml.bind-api-4.0.2.jar:$APP_HOME/lib/jakarta.validation-api-2.0.2.jar:$APP_HOME/lib/jc-kzg-4844-2.1.1.jar:$APP_HOME/lib/java-eth-kzg-0.5.4.jar:$APP_HOME/lib/commons-lang-2.4.jar:$APP_HOME/lib/ognl-3.3.4.jar:$APP_HOME/lib/attoparser-2.0.7.RELEASE.jar:$APP_HOME/lib/unbescape-1.1.6.RELEASE.jar:$APP_HOME/lib/reactive-streams-1.0.4.jar:$APP_HOME/lib/jnr-enxio-0.32.17.jar:$APP_HOME/lib/jnr-posix-3.1.19.jar:$APP_HOME/lib/jnr-ffi-2.2.16.jar:$APP_HOME/lib/leveldbjni-1.8.jar:$APP_HOME/lib/launchdarkly-logging-1.1.1.jar:$APP_HOME/lib/annotations-13.0.jar:$APP_HOME/lib/javax.inject-1.jar:$APP_HOME/lib/jakarta.inject-api-2.0.1.jar:$APP_HOME/lib/jspecify-1.0.0.jar:$APP_HOME/lib/animal-sniffer-annotations-1.24.jar:$APP_HOME/lib/perfmark-api-0.27.0.jar:$APP_HOME/lib/annotations-4.1.1.4.jar:$APP_HOME/lib/netty-tcnative-classes-2.0.70.Final.jar:$APP_HOME/lib/netty-codec-haproxy-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-memcache-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-mqtt-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-redis-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-smtp-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-stomp-4.1.118.Final.jar:$APP_HOME/lib/netty-codec-xml-4.1.118.Final.jar:$APP_HOME/lib/netty-handler-ssl-ocsp-4.1.118.Final.jar:$APP_HOME/lib/netty-transport-rxtx-4.1.118.Final.jar:$APP_HOME/lib/netty-transport-sctp-4.1.118.Final.jar:$APP_HOME/lib/netty-transport-udt-4.1.118.Final.jar:$APP_HOME/lib/opentelemetry-context-1.47.0.jar:$APP_HOME/lib/prometheus-metrics-exporter-common-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-exposition-formats-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-exposition-textformats-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-model-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-config-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-tracer-initializer-1.3.5.jar:$APP_HOME/lib/jnr-constants-0.10.4.jar:$APP_HOME/lib/jetty-jakarta-servlet-api-5.0.2.jar:$APP_HOME/lib/websocket-jetty-api-11.0.25.jar:$APP_HOME/lib/javassist-3.29.0-GA.jar:$APP_HOME/lib/commons-codec-1.16.0.jar:$APP_HOME/lib/jakarta.activation-api-2.1.3.jar:$APP_HOME/lib/hawtjni-runtime-1.9.jar:$APP_HOME/lib/leveldb-api-0.6.jar:$APP_HOME/lib/prometheus-metrics-tracer-otel-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-tracer-otel-agent-1.3.5.jar:$APP_HOME/lib/prometheus-metrics-tracer-common-1.3.5.jar:$APP_HOME/lib/jffi-1.3.13.jar:$APP_HOME/lib/jffi-1.3.13-native.jar:$APP_HOME/lib/asm-commons-9.2.jar:$APP_HOME/lib/asm-util-9.2.jar:$APP_HOME/lib/asm-analysis-9.2.jar:$APP_HOME/lib/asm-tree-9.2.jar:$APP_HOME/lib/asm-9.2.jar:$APP_HOME/lib/jnr-a64asm-1.0.0.jar:$APP_HOME/lib/jnr-x86asm-1.0.2.jar


# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
        # IBM's JDK on AIX uses strange locations for the executables
        JAVACMD=$JAVA_HOME/jre/sh/java
    else
        JAVACMD=$JAVA_HOME/bin/java
    fi
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD=java
    if ! command -v java >/dev/null 2>&1
    then
        die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
    case $MAX_FD in #(
      max*)
        # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
        # shellcheck disable=SC2039,SC3045
        MAX_FD=$( ulimit -H -n ) ||
            warn "Could not query maximum file descriptor limit"
    esac
    case $MAX_FD in  #(
      '' | soft) :;; #(
      *)
        # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
        # shellcheck disable=SC2039,SC3045
        ulimit -n "$MAX_FD" ||
            warn "Could not set maximum file descriptor limit to $MAX_FD"
    esac
fi

# Collect all arguments for the java command, stacking in reverse order:
#   * args from the command line
#   * the main class name
#   * -classpath
#   * -D...appname settings
#   * --module-path (only if needed)
#   * DEFAULT_JVM_OPTS, JAVA_OPTS, and TEKU_OPTS environment variables.

# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
    APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
    CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )

    JAVACMD=$( cygpath --unix "$JAVACMD" )

    # Now convert the arguments - kludge to limit ourselves to /bin/sh
    for arg do
        if
            case $arg in                                #(
              -*)   false ;;                            # don't mess with options #(
              /?*)  t=${arg#/} t=/${t%%/*}              # looks like a POSIX filepath
                    [ -e "$t" ] ;;                      #(
              *)    false ;;
            esac
        then
            arg=$( cygpath --path --ignore --mixed "$arg" )
        fi
        # Roll the args list around exactly as many times as the number of
        # args, so each arg winds up back in the position where it started, but
        # possibly modified.
        #
        # NB: a `for` loop captures its iteration list before it begins, so
        # changing the positional parameters here affects neither the number of
        # iterations, nor the values presented in `arg`.
        shift                   # remove old arg
        set -- "$@" "$arg"      # push replacement arg
    done
fi


# Add default JVM options here. You can also use JAVA_OPTS and TEKU_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Dvertx.disableFileCPResolving=true" "-Dteku.home=$APP_HOME" "-Dlog4j.shutdownHookEnabled=false" "-Dlog4j2.formatMsgNoLookups=true" "-XX:NativeMemoryTracking=summary" "-Dio.netty.maxDirectMemory=33554432" "--enable-native-access=ALL-UNNAMED"'

# Collect all arguments for the java command:
#   * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
#     and any embedded shellness will be escaped.
#   * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
#     treated as '${Hostname}' itself on the command line.

set -- \
        -classpath "$CLASSPATH" \
        tech.pegasys.teku.Teku \
        "$@"

# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
    die "xargs is not available"
fi

# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
#   readarray ARGS < <( xargs -n1 <<<"$var" ) &&
#   set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#

eval "set -- $(
        printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $TEKU_OPTS" |
        xargs -n1 |
        sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
        tr '\n' ' '
    )" '"$@"'

unset TEKU_USING_JEMALLOC
if [ "$darwin" = "false" -a "$msys" = "false" ]; then
  # check if jemalloc is available
  TEST_JEMALLOC=$(LD_PRELOAD=libjemalloc.so sh -c true 2>&1)

  # if jemalloc is available the output is empty, otherwise the output has an error line
  if [ -z "$TEST_JEMALLOC" ]; then
    export LD_PRELOAD=libjemalloc.so
    export TEKU_USING_JEMALLOC=true
  fi
fi

exec "$JAVACMD" "$@"