#!/bin/sh
#
# Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code 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
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

# Usage:
#    jtreg ...args....
#       Run the application via the regression test-suite front end
#       with the given arguments.
#       The Java runtime used to run JavaTest is found as follows:
#       -   $JT_JAVA is used, if it is set
#       -   Otherwise, $JAVA_HOME/bin/java is used if $JAVA_HOME is set
#           (cf JDK.)
#       -   Otherwise, "java" is used
#
# jtreg requires a version of Java equivalent to JDK 1.7.0 or higher.

# $JT_HOME can be used to specify the jtreg installation directory
#   (e.g. /usr/local/jtreg/4.2)
#
# $JT_JAVA is used to specify the version of java to use when running JavaTest
#   (e.g. /usr/local/java/jdk1.7.0/solaris-sparc/bin/java)
#
# You can also run the jar file directly, as in
#   java -jar <path>/lib/jtreg.jar ...args...
#
# jtreg also provides Ant tasks; see the documentation for details.

# Implementation notes for Windows:
# Cygwin:
#   Detected with `uname -s` (CYGWIN*)
#   Windows drives are mounted with /cygdrive/LETTER
# Windows Subsystem for Linux (WSL):
#   Detected with `uname -s` (Linux) and /proc/version contains "Microsoft"
#   Windows drives are mounted with /mnt/LETTER
#   Windows binaries need an explicit .exe suffix.
#
# Values are evaluated according to whether the are used in the context of the
# shell, or in the context of the JDK under test.
# JTJAVA is evaluated for use in the shell, to run java
# JTHOME is evaluated as a JDK arg, for use in -classpath or -jar args
# Other command line are updated to be JDK args for jtreg.

case "`uname -s`" in
    CYGWIN* ) cygwin=1 ;;
    Linux ) if grep -q Microsoft /proc/version ; then wsl=1 ; fi ;;
esac


# Look for -jdk option as possible default to run jtreg
# Unset IFS and use newline as arg separator to preserve spaces in args
DUALCASE=1  # for MKS: make case statement case-sensitive (6709498)
saveIFS="$IFS"
nl='
'
for i in "$@" ; do
    IFS=
    case $i in
    -jdk:* )    jdk="`echo $i | sed -e 's/^-jdk://'`" ;;
    esac
    IFS="$saveIFS"
done
unset DUALCASE

# Java home directory
if [ -z "${JT_JAVA}" ]; then
    JT_JAVA="/usr/lib/jvm/default-java"
fi

# Fixup JT_JAVA, JTHOME as needed, if using Cygwin or WSL
if [ -n "$cygwin" ]; then
    JT_HOME=`cygpath -a -m "$JT_HOME"` ;
elif [ -n "$wsl" -a -x "$JT_JAVA".exe ]; then
    JT_JAVA="$JT_JAVA".exe
    JT_HOME=`wslpath -a -m "$JT_HOME"`
fi

if [ -z "${JT_HOME}" ] || [ ! -f "${JT_HOME}/jtreg.jar" ]; then
    JT_HOME="/usr/share/java"
fi

# Separate out -J* options for the JVM
# Unset IFS and use newline as arg separator to preserve spaces in arg
DUALCASE=1  # for MKS: make case statement case-sensitive (6709498)
saveIFS="$IFS"
nl='
'
if [ -n "$cygwin" ]; then
  driveDir=cygdrive ;
elif [ -n "$wsl" -a "${JT_JAVA##*.}" = "exe" ]; then
  driveDir=mnt ;
fi
for i in "$@" ; do
    IFS=
    if [ -n "$driveDir" ]; then i=`echo $i | sed -e 's|/'$driveDir'/\([A-Za-z]\)/|\1:/|'` ; fi
    case $i in
    -J* )       javaOpts=$javaOpts$nl`echo $i | sed -e 's/^-J//'` ;;
    *   )       jtregOpts=$jtregOpts$nl$i ;;
    esac
    IFS="$saveIFS"
done
unset DUALCASE

# And finally ...

JT_CLASSPATH="${JT_HOME}/jtreg.jar:/usr/share/java/javatest.jar:/usr/share/java/jh.jar:/usr/share/java/junit4.jar"

IFS=$nl

"${JT_JAVA}"/bin/java \
    $javaOpts \
    -Dprogram=`basename "$0"` \
    -cp "${JT_CLASSPATH}" \
    com.sun.javatest.regtest.Main \
    $jtregOpts
