#!/usr/bin/env sh

# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of fzjava command, the Fuzion tool that creates Fuzion
#  features to interface Java code.
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------


# usage: fzjava <args>
#
# env vars that may be set:
#
#   FUZION_HOME            the fuzion installation directory
#   FUZION_JAVA            the java command used to run Fuzion
#   FUZION_JAVA_STACK_SIZE the stack size parameter passed to the JAVA command
#   FUZION_JAVA_OPTIONS    options to be passed to the JAVA command (if set, will
#                          override stack size)

set -eu

FUZION_CMD=$0
FUZION_BIN="$(dirname "$(readlink -f "$0")")"

: "${FUZION_HOME="$FUZION_BIN/.."}"
: "${FUZION_JAVA=java}"
: "${FUZION_JAVA_ADDITIONAL_CLASSPATH=""}"
: "${FUZION_JAVA_CLASSPATH="$FUZION_HOME/classes"}"
# windows (msys2) does not seem to like trailing colons in the class path
if [ -n "$FUZION_JAVA_ADDITIONAL_CLASSPATH" ]
then
  FUZION_JAVA_CLASSPATH="$FUZION_JAVA_CLASSPATH:$FUZION_JAVA_ADDITIONAL_CLASSPATH"
fi
: "${FUZION_JAVA_STACK_SIZE=5m}"
: "${FUZION_JAVA_OPTIONS="-Xss$FUZION_JAVA_STACK_SIZE"}"
: "${FUZION_JAVA_ADDITIONAL_OPTIONS=--enable-preview --enable-native-access=ALL-UNNAMED}"

LINE_SEPARATOR="
"

# shellcheck disable=SC2086
$FUZION_JAVA \
  $FUZION_JAVA_OPTIONS \
  $FUZION_JAVA_ADDITIONAL_OPTIONS \
  -cp "$FUZION_JAVA_CLASSPATH" \
  -Dline.separator="$LINE_SEPARATOR" \
  -Dfile.encoding=UTF-8 \
  -Dfuzion.home="$FUZION_HOME" \
  -Dfuzion.command="$FUZION_CMD" \
  dev.flang.tools.fzjava.FZJava \
  "$@"
