#
# Copyright (c) 2013, 2022, 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.
#

$(BUILDTESTDIR)/RerunTest.agentvm.ok \
$(BUILDTESTDIR)/RerunTest.othervm.ok:  JAVAOPTION =-javaoption:-Dmy.java.option=x

$(BUILDTESTDIR)/RerunTest.agentvm.ok \
$(BUILDTESTDIR)/RerunTest.othervm.ok:  JAVACOPTION=-javacoption:-XDmy.javac.option=x

ifdef HEADLESS
RERUN_JTREG_OPT = -k:!needDisplay
RERUN_EXPECT_PASS = 9
else
RERUN_EXPECT_PASS = 10
endif

$(BUILDTESTDIR)/RerunTest.agentvm.ok \
$(BUILDTESTDIR)/RerunTest.othervm.ok: \
	    $(JTREG_IMAGEDIR)/lib/jtreg.jar \
	    $(JTREG_IMAGEDIR)/bin/jtreg
	$(RM) $(@:%.ok=%) ; $(MKDIR) $(@:%.ok=%)
	unset GNOME_DESKTOP_SESSION_ID ; \
	unset XMODIFIERS ; \
	LANG=en_US.UTF-8 TZ=GMT+0.00 DISPLAY=$${DISPLAY:-dummy} \
	JTREG_JAVA=$(JDKJAVA) $(JTREG_IMAGEDIR)/bin/jtreg $(JTREG_OPTS) \
		-J-Djavatest.regtest.allowTrailingBuild=true \
		-w:$(@:%.ok=%)/work -r:$(@:%.ok=%)/report \
		-jdk:$(JDK8HOME) \
		-verbose:summary \
		-e:MY_ENV_VAR=x -vmoption:-Dmy.vm.option=x $(JAVAOPTION) $(JAVACOPTION) \
		-ignore:run \
		$(RERUN_JTREG_OPT) \
		$(@:$(BUILDTESTDIR)/RerunTest.%.ok=-%) \
		$(TESTDIR)/rerun  \
			> $(@:%.ok=%/jt.log) 2>&1
	$(GREP) -s 'Test results: passed: $(RERUN_EXPECT_PASS)' $(@:%.ok=%/jt.log)  > /dev/null
	for i in `cd $(TESTDIR)/rerun ; ls */*.sh */*.java` ; do \
	    if [ -n "$(HEADLESS)" -a $$i = std/AppletTest.java ]; then continue; fi ; \
	    echo Checking $(@:$(BUILDTESTDIR)/RerunTest.%.ok=%) $$i ; \
	    mkdir -p `dirname $(@:%.ok=%)/out/$$i` ; \
	    JTREG_JAVA=$(JDKJAVA) $(JTREG_IMAGEDIR)/bin/jtreg \
		-jdk:$(JDK8HOME) \
		-w:$(@:%.ok=%)/work \
		-dir:$(TESTDIR)/rerun \
		-show:rerun $$i | \
		sed -e 's|^HOME=.* \\|HOME=%HOME% \\|' \
		    -e 's|^DISPLAY=.* \\|DISPLAY=%DISPLAY% \\|' \
		    -e 's|$(JDK8HOME)|%JDKHOME%|g' \
		    -e "s|`cd $(JDK8HOME); pwd -P`|%JDKHOME%|g" \
		    -e '/JTREG_JAVA=/d' \
		    -e 's|$(BUILDTESTDIR)|%BUILDTEST%|g' \
		    -e 's|$(BUILDDIR)|%BUILD%|g' \
		    -e 's|$(ABSTOPDIR)|%WS%|g' \
		    -e 's|junit-[A-Za-z0-9.-]*\.jar|junit.jar|g' \
		    -e 's|testng-[A-Za-z0-9.-]*\.jar|testng.jar|g' \
		    -e 's|%BUILD%.images.jtreg.lib.hamcrest[A-Za-z0-9.-]*\.jar.||g' \
		    -e 's|%BUILD%.images.jtreg.lib.guice[A-Za-z0-9.-]*\.jar.||g' \
		    -e 's|%BUILD%.images.jtreg.lib.jcommander[A-Za-z0-9.-]*\.jar.||g' \
		> $(@:%.ok=%)/out/$${i%.*}.out ; \
	    diff -c $(TESTDIR)/rerun/$${i%.*}.$(@:$(BUILDTESTDIR)/RerunTest.%.ok=%).out \
		$(@:%.ok=%)/out/$${i%.*}.out || exit 1 ; \
	done
	echo "test passed at `date`" > $@

ifdef JDK8HOME
ifneq ($(OS_NAME), windows)
TESTS.jtreg += \
	$(BUILDTESTDIR)/RerunTest.agentvm.ok \
	$(BUILDTESTDIR)/RerunTest.othervm.ok
endif
endif
