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

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

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

$(BUILDDIR)/RerunTest.agentvm.ok \
$(BUILDDIR)/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 \
	$(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 \
		$(@:$(BUILDDIR)/RerunTest.%.ok=-%) \
		$(TESTDIR)/rerun  \
			> $(@:%.ok=%/jt.log) 2>&1
	$(GREP) -s 'Test results: passed: 10' $(@:%.ok=%/jt.log)  > /dev/null
	for i in `cd $(TESTDIR)/rerun ; ls */*.sh */*.java` ; do \
	    echo Checking $(@:$(BUILDDIR)/RerunTest.%.ok=%) $$i ; \
	    mkdir -p `dirname $(@:%.ok=%)/out/$$i` ; \
	    $(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 's|$(BUILDDIR)|%BUILD%|g' \
		    -e 's|$(ABSTOPDIR)|%WS%|g' \
		    -e 's|%BUILD%.images.jtreg.lib.guice\.jar.||g' \
		    -e 's|%BUILD%.images.jtreg.lib.hamcrest\.jar.||g' \
		    -e 's|%BUILD%.images.jtreg.lib.jcommander\.jar.||g' \
		> $(@:%.ok=%)/out/$${i%.*}.out ; \
	    diff -c $(TESTDIR)/rerun/$${i%.*}.$(@:$(BUILDDIR)/RerunTest.%.ok=%).out \
		$(@:%.ok=%)/out/$${i%.*}.out || exit 1 ; \
	done
	echo "test passed at `date`" > $@

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