#!/data/data/com.termux/files/usr/bin/env bash
# -----------------------------------------------------------------------------
# jython.sh - start script for Jython (adapted from jruby.sh)
#
# Environment variables (optional)
#
#   JAVA_HOME      Java installation directory
#
#   JYTHON_HOME    Jython installation directory
#
#   JYTHON_OPTS    Default Jython command line arguments
#
# -----------------------------------------------------------------------------

cygwin=false
jdb_requested=false

# ----- Identify OS we are running under --------------------------------------
case "`uname`" in
  CYGWIN*) cygwin=true;;
  Darwin) darwin=true;;
esac

# ----- Verify and set required environment variables -------------------------

## resolve links - $0 may be a link to home
PRG=$0

while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    if expr "$link" : '/' > /dev/null; then
      PRG="$link"
    else
      PRG="`dirname ${PRG}`/${link}"
    fi
  else
    PRG="`dirname $PRG`/$link"
  fi
done

if [ -z "$JAVA_HOME" ] ; then
  JAVA_CMD=(java)
else
  if $cygwin; then
    JAVA_HOME=`cygpath -u "$JAVA_HOME"`
  fi
  JAVA_CMD=("$JAVA_HOME/bin/java")
fi

# try to dynamically determine jython home
# (this script typically resides in jython home, or in the /bin subdirectory)
if [ -z "$JYTHON_HOME" ] ; then
  if [ "$PRG" = "./jython" ] ; then
    # current dir is the script dir
    JYTHON_HOME_1=`pwd`
  else
    # current dir is not the script dir
    JYTHON_HOME_1=`dirname "$PRG"`
  fi
  if [ -f "$JYTHON_HOME_1"/jython.jar -o -f "$JYTHON_HOME_1"/jython-dev.jar ] ; then
    JYTHON_HOME="$JYTHON_HOME_1"
  else
    JYTHON_HOME=`dirname "$JYTHON_HOME_1"`
  fi
  if [ ! -f "$JYTHON_HOME"/jython.jar -a ! -f "$JYTHON_HOME"/jython-dev.jar ] ; then
    JYTHON_HOME="$JYTHON_HOME_FALLBACK"
  fi
fi

if [ -z "$JYTHON_OPTS" ] ; then
  JYTHON_OPTS=""
fi

CP_DELIMITER=":"
EXTRA_CP=""

build_jython_classpath() {
  CP=$1/jython-dev.jar

  if [ -f "$CP" ] ; then
    # We are running in the development environment.
    # Add -test JAR to class path.
    CP="$CP$CP_DELIMITER$1/jython-test.jar"
    # Add necessary jar dir for command-line execution
    CP="$CP$CP_DELIMITER$1/javalib/*"
  elif [ ! -f "$1"/jython.jar ] ; then
    echo "$0: $1 contains neither jython-dev.jar nor jython.jar." >&2
    echo "Try running this script from the 'bin' directory of an installed Jython or " >&2
    echo 'setting $JYTHON_HOME.' >&2
    exit 1
  else
    # We are running in the deployment environment.
    # Class path is just the main JAR (not tests).
    CP=$1/jython.jar
  fi
}

build_jython_classpath "$JYTHON_HOME"

if $cygwin; then
  # switch delimiter only after building a Unix style $CP
  CP_DELIMITER=";"
  CP=`cygpath -p -w "$CP"`
  # ensure sys.executable is the Windows executable wrapper
  PRG=`cygpath -w "$PRG"`.exe
fi

JYTHON_HOME_FOR_JAVA="$JYTHON_HOME"
PRG_FOR_JAVA="$PRG"
JDB_TMPDIR=""

# ----- Execute the requested command -----------------------------------------

if [ -z "$JAVA_MEM" ] ; then
  JAVA_MEM=-Xmx512m
fi

if [ -z "$JAVA_STACK" ]; then
   # test_marshal.py needs a Xss of 2560k to pass
  JAVA_STACK=-Xss2560k
fi

JAVA_ENCODING=""

# Split out any -J argument for passing to the JVM.
# Scanning for args is aborted by '--'.
while [ $# -gt 0 ] ; do
  case "$1" in
    # Stuff after '-J' in this argument goes to JVM
    -J*)
      val=${1:2}
      if [ "${val:0:4}" = "-Xmx" ]; then
        JAVA_MEM=$val
      elif [ "${val:0:4}" = "-Xss" ]; then
        JAVA_STACK=$val
      elif [ "${val}" = "" ]; then
          $JAVA_CMD -help
          echo "(Prepend -J in front of these options when using 'jython' command)" 
          exit
      elif [ "${val}" = "-X" ]; then
          $JAVA_CMD -X
          echo "(Prepend -J in front of these options when using 'jython' command)" 
          exit
        elif [ "${val}" = "-classpath" ]; then
            EXTRA_CP="$EXTRA_CP$CP_DELIMITER$2"
            shift
        elif [ "${val}" = "-cp" ]; then
            EXTRA_CP="$EXTRA_CP$CP_DELIMITER$2"
            shift
      else
          if [ "${val:0:16}" = "-Dfile.encoding=" ]; then
              JAVA_ENCODING=$val
          fi
	  java_args=("${java_args[@]}" "${1:2}")
      fi
      ;;
    # Match switches that take an argument
    -c|-C|-m|-jar|-Q|-W)
      if [ $# = 1 ]; then
          python_args=("${python_args[@]}" "$1")
      else
          python_args=("${python_args[@]}" "$1" "$2")
          shift
      fi;
      ;;
    # Match -Dprop=val type args
    -D*)
      python_args=("${python_args[@]}" "$1")
      ;;
    # Print the command, don't execute it
    --print)
      print_requested=true
      ;;
    # Run with the instrumented profiler: http://jiprof.sourceforge.net/
    --profile)
      rm -f profile.txt # XXX do this?
      profile_requested=true
      agent_path="$JYTHON_HOME/javalib/profile.jar"
      props_path="$JYTHON_HOME/javalib/profile.properties"
      if $cygwin; then
	agent_path=`cygpath -w "$agent_path"`
	props_path=`cygpath -w "$props_path"`
      fi
      java_args=("${java_args[@]}" -javaagent:"$agent_path"
	-Dprofile.properties="$props_path")
      ;;
    # Put Jython on the boot classpath (disables the verifier)
    --boot)
      boot_requested=true
      ;;
    # Run under JDB
    --jdb)
      jdb_requested=true
      if [ -z "$JAVA_HOME" ] ; then
	JAVA_CMD=(jdb)
      else
	if $cygwin; then
	  JAVA_HOME=`cygpath -u "$JAVA_HOME"`
	fi
	JAVA_CMD=("$JAVA_HOME/bin/jdb")
      fi
      ;;
    -h|--help)
      help_requested=true
      python_args=("${python_args[@]}" "$1")
      ;;
    # Abort processing on a double dash
    --)
      break
      ;;
    # Other opts go to Jython
    -*)
      python_args=("${python_args[@]}" "$1")
      ;;
    # Abort processing on first non-opt arg
    *)
      break
      ;;
  esac
  shift
done

# Force file.encoding to UTF-8 when on Mac, since Apple JDK defaults to MacRoman (JRUBY-3576)
if [[ $darwin && -z "$JAVA_ENCODING" ]]; then
  java_args=("${java_args[@]}" "-Dfile.encoding=UTF-8")
fi

# Append the rest of the arguments
python_args=("${python_args[@]}" "$@")

# Put the python_args back into the position arguments $1, $2 etc
set -- "${python_args[@]}"

setup_jdb_path_alias() {
  if ! $jdb_requested || $cygwin; then
    return
  fi

  # Some jdb implementations, notably OpenJDK/Temurin 8, split the debuggee
  # command line on spaces after receiving it from this launcher. Use a
  # temporary no-space symlink for Jython paths so the classpath and
  # -Dpython.* values survive jdb's second-stage JVM launch.
  case "$JYTHON_HOME$PRG" in
    *" "*)
      JDB_TMPDIR=`mktemp -dt jython-jdb.XXXXXX` || exit 1
      JDB_HOME_TARGET=`cd "$JYTHON_HOME" && pwd` || exit 1
      ln -s "$JDB_HOME_TARGET" "$JDB_TMPDIR/jython" || exit 1
      JYTHON_HOME_FOR_JAVA="$JDB_TMPDIR/jython"
      build_jython_classpath "$JYTHON_HOME_FOR_JAVA"

      PRG_FOR_JAVA="$JYTHON_HOME_FOR_JAVA/bin/`basename "$PRG"`"
      if [ ! -e "$PRG_FOR_JAVA" ]; then
        mkdir "$JDB_TMPDIR/bin" || exit 1
        case "$PRG" in
          /*) JDB_PRG_TARGET="$PRG" ;;
          *) JDB_PRG_TARGET="`pwd`/$PRG" ;;
        esac
        ln -s "$JDB_PRG_TARGET" "$JDB_TMPDIR/bin/`basename "$PRG"`" || exit 1
        PRG_FOR_JAVA="$JDB_TMPDIR/bin/`basename "$PRG"`"
      fi
      trap 'rm -rf "$JDB_TMPDIR"' EXIT
      ;;
  esac
}

java_major_version() {
  local output version major
  output=$("${JAVA_CMD[@]}" -version 2>&1)
  version=`expr "$output" : '.*version "\([^"]*\)"'`
  case "$version" in
    1.*)
      major=${version#1.}
      major=${major%%.*}
      ;;
    *)
      major=${version%%[!0-9]*}
      ;;
  esac
  echo "$major"
}

append_java_arg() {
  if $jdb_requested; then
    java_args=("${java_args[@]}" "-R$1")
  else
    java_args=("${java_args[@]}" "$1")
  fi
}

if [ -z "$help_requested" ]; then
  java_major=`java_major_version`
  if [ -n "$java_major" ]; then
    if [ "$java_major" -ge 9 ] 2>/dev/null; then
      case " $JAVA_OPTS ${java_args[*]} " in
        *--add-opens=java.base/java.io*) ;;
        *) append_java_arg --add-opens=java.base/java.io=ALL-UNNAMED ;;
      esac
      case " $JAVA_OPTS ${java_args[*]} " in
        *--add-opens=java.base/sun.nio.ch*) ;;
        *) append_java_arg --add-opens=java.base/sun.nio.ch=ALL-UNNAMED ;;
      esac
    fi
    if [ "$java_major" -ge 25 ] 2>/dev/null; then
      case " $JAVA_OPTS ${java_args[*]} " in
        *--enable-native-access*) ;;
        *) append_java_arg --enable-native-access=ALL-UNNAMED ;;
      esac
      case " $JAVA_OPTS ${java_args[*]} " in
        *--sun-misc-unsafe-memory-access*) ;;
        *) append_java_arg --sun-misc-unsafe-memory-access=allow ;;
      esac
    fi
  fi
fi

JAVA_OPTS="$JAVA_OPTS $JAVA_MEM $JAVA_STACK"

if [ -n "$profile_requested" ]; then
  java_major=`java_major_version`
  if [ -n "$java_major" ] && [ "$java_major" -ge 13 ] 2>/dev/null; then
    echo "--profile is not supported on Java $java_major; the bundled Java Interactive Profiler is only compatible with Java 12 and earlier." >&2
    exit 2
  fi
fi

setup_jdb_path_alias

if $cygwin; then
  JAVA_HOME=`cygpath --mixed "$JAVA_HOME"`
  JYTHON_HOME=`cygpath --mixed "$JYTHON_HOME"`
  
  if [[ ( "${1:0:1}" = "/" ) && ( ( -f "$1" ) || ( -d "$1" )) ]]; then
    win_arg=`cygpath -w "$1"`
    shift
    win_args=("$win_arg" "$@")
    set -- "${win_args[@]}"
  fi

  # fix JLine to use UnixTerminal
  stty -icanon min 1 -echo > /dev/null 2>&1
  if [ $? = 0 ]; then
    JAVA_OPTS="$JAVA_OPTS -Djline.terminal=jline.UnixTerminal"
  fi
fi

if [ -n "$profile_requested" -o -z "$boot_requested" ] ; then
  [ -n "$profile_requested" ] && echo "Running with instrumented profiler"
  if [ -n "$profile_requested" ]; then
    java_major=`java_major_version`
    if [ -n "$java_major" ] && [ "$java_major" -lt 13 ] 2>/dev/null; then
      java_args=("${java_args[@]}" -Xverify:none)
    fi
  fi
  java_args=("${java_args[@]}" -classpath "$CP$EXTRA_CP$CP_DELIMITER$CLASSPATH")
else
  if [ -z "$help_requested" -a -z "$print_requested" ] && ! $jdb_requested ; then
    JAVA_CMD=(exec "${JAVA_CMD[@]}")
  fi
  java_args=("${java_args[@]}" -Xbootclasspath/a:"$CP$EXTRA_CP")
  [ -n "$CLASSPATH" ] && java_args=("${java_args[@]}" -classpath "$CLASSPATH")
fi

if [ -n "$print_requested" ] ; then
  JAVA_CMD=(echo $JAVA_CMD)
fi

"${JAVA_CMD[@]}" $JAVA_OPTS "${java_args[@]}" -Dpython.home="$JYTHON_HOME_FOR_JAVA" \
  -Dpython.executable="$PRG_FOR_JAVA" org.python.util.jython $JYTHON_OPTS "$@"
JYTHON_STATUS=$?

if [ -n "$profile_requested" ] ; then
  echo "Profiling results:"
  cat profile.txt
elif [ -n "$help_requested" ] ; then
  echo "" >&2
  echo "Jython launcher options:" >&2
  echo "-Jarg    : pass argument through to Java VM (e.g. -J-Xmx512m)" >&2
  echo "--jdb    : run under JDB" >&2
  echo "--print  : print the Java command instead of executing it" >&2
  echo "--profile: run with the Java Interactive Profiler (http://jiprof.sf.net)" >&2
  echo "--boot   : put jython on the boot classpath (disables the bytecode verifier)" >&2
  echo "--       : pass remaining arguments through to Jython" >&2
  echo "Jython launcher environment variables:" >&2
  echo "JAVA_HOME  : Java installation directory" >&2
  echo "JYTHON_HOME: Jython installation directory" >&2
  echo "JYTHON_OPTS: default command line arguments" >&2
fi

if $cygwin; then
  stty icanon echo > /dev/null 2>&1
fi

exit $JYTHON_STATUS
