SUMMARY="Open-source implementation of the Java Platform, SE"
DESCRIPTION="OpenJDK (Open Java Development Kit) is a free and open source \
implementation of the Java Platform, Standard Edition (Java SE). It is the \
result of an effort Sun Microsystems began in 2006. The implementation is \
licensed under the GNU General Public License (GNU GPL) with a linking \
exception. Were it not for the GPL linking exception, components that linked \
to the Java class library would be subject to the terms of the GPL license. \
OpenJDK is the official Java SE 7 reference implementation."
HOMEPAGE="http://openjdk.java.net/"
COPYRIGHT="2015 Oracle and/or its affiliates."
LICENSE="GNU GPL v2"

SOURCE_URI="hg+http://bitbucket.org/hamishm/haiku-jdk8u#6e3a7c2"
SOURCE_DIR="jdk"

SOURCE_URI_2="hg+http://bitbucket.org/hamishm/haiku-jdk8u-hotspot#e5ab6f7"
SOURCE_URI_3="hg+http://bitbucket.org/hamishm/haiku-jdk8u-jdk#9b3967c"
SOURCE_URI_4="hg+http://hg.openjdk.java.net/jdk8u/jdk8u/langtools#jdk8u40-b27"
SOURCE_URI_5="hg+http://hg.openjdk.java.net/jdk8u/jdk8u/corba#jdk8u40-b27"
SOURCE_URI_6="hg+http://hg.openjdk.java.net/jdk8u/jdk8u/jaxp#jdk8u40-b27"
SOURCE_URI_7="hg+http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws#jdk8u40-b27"
SOURCE_URI_8="hg+http://hg.openjdk.java.net/jdk8u/jdk8u/nashorn#jdk8u40-b27"


REVISION="1"
ARCHITECTURES="x86"
if [ $effectiveTargetArchitecture != x86_gcc2 ]; then
	# x86_gcc2 is fine as primary target architecture as long as we're building
	# for a different secondary architecture.
	ARCHITECTURES="$ARCHITECTURES x86_gcc2"
else
    ARCHITECTURES="$ARCHITECTURES !x86_gcc2"
fi
SECONDARY_ARCHITECTURES="x86"

DISABLE_SOURCE_PACKAGE=yes
	# at least as long as Ant and a complete SDK image are part of the sources

PROVIDES="
	openjdk$secondaryArchSuffix = $portVersion compat >= 1.8
	cmd:jexec = $portVersion compat >= 1.8
	cmd:appletviewer = $portVersion compat >= 1.8
	cmd:apt = $portVersion compat >= 1.8
	cmd:extcheck = $portVersion compat >= 1.8
	cmd:idlj = $portVersion compat >= 1.8
	cmd:jar = $portVersion compat >= 1.8
	cmd:jarsigner = $portVersion compat >= 1.8
	cmd:java = $portVersion compat >= 1.8
	cmd:java_config = $portVersion compat >= 1.8
	cmd:javac = $portVersion compat >= 1.8
	cmd:javadoc = $portVersion compat >= 1.8
	cmd:javah = $portVersion compat >= 1.8
	cmd:javap = $portVersion compat >= 1.8
	cmd:jcmd = $portVersion compat >= 1.8
	cmd:jconsole = $portVersion compat >= 1.8
	cmd:jdb = $portVersion compat >= 1.8
	cmd:jdeps = $portVersion compat >= 1.8
	cmd:jhat = $portVersion compat >= 1.8
	cmd:jinfo = $portVersion compat >= 1.8
	cmd:jjs = $portVersion compat >= 1.8
	cmd:jmap = $portVersion compat >= 1.8
	cmd:jps = $portVersion compat >= 1.8
	cmd:jrunscript = $portVersion compat >= 1.8
	cmd:jsadebugd = $portVersion compat >= 1.8
	cmd:jstack = $portVersion compat >= 1.8
	cmd:jstat = $portVersion compat >= 1.8
	cmd:jstatd = $portVersion compat >= 1.8
	cmd:keytool = $portVersion compat >= 1.8
	cmd:native2ascii = $portVersion compat >= 1.8
	cmd:orbd = $portVersion compat >= 1.8
	cmd:pack200 = $portVersion compat >= 1.8
	cmd:policytool = $portVersion compat >= 1.8
	cmd:rmic = $portVersion compat >= 1.8
	cmd:rmid = $portVersion compat >= 1.8
	cmd:rmiregistry = $portVersion compat >= 1.8
	cmd:schemagen = $portVersion compat >= 1.8
	cmd:serialver = $portVersion compat >= 1.8
	cmd:servertool = $portVersion compat >= 1.8
	cmd:tnameserv = $portVersion compat >= 1.8
	cmd:unpack200 = $portVersion compat >= 1.8
	cmd:wsgen = $portVersion compat >= 1.8
	cmd:wsimport = $portVersion compat >= 1.8
	cmd:xjc = $portVersion compat >= 1.8
	"

REQUIRES="
	haiku$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	openjdk$secondaryArchSuffix >= 1.7
	ca_root_certificates
	devel:libfreetype$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"

BUILD_PREREQUIRES="
	cmd:cpio
	cmd:make
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:sed
	cmd:tar
	cmd:zip
	cmd:awk
	cmd:hostname
	cmd:find
	cmd:unzip
	cmd:unzipsfx
	cmd:head
	cmd:file
	cmd:which
	cmd:autoconf
	"

BUILD()
{
	# prepare the directory structure
	cd $(dirname $sourceDir)/jdk
	ln -sfn $sourceDir2 hotspot
	ln -sfn $sourceDir3 jdk
	ln -sfn $sourceDir4 langtools
	ln -sfn $sourceDir5 corba
	ln -sfn $sourceDir6 jaxp
	ln -sfn $sourceDir7 jaxws
	ln -sfn $sourceDir8 nashorn

	cp $portDir/sources/elf.h hotspot/src/share/vm/utilities

	# If ASLR is enabled, the JVM can fail to find a large enough area for
	# the heap.
	export DISABLE_ASLR=1

	# Verify that we can allocate a large enough heap before starting.
	maxHeapSize=960M
	java -XX:ThreadStackSize=1536 -XX:MaxPermSize=160m -Xmx${maxHeapSize} -version

	freeTypeHeaders=$(finddir B_SYSTEM_HEADERS_DIRECTORY)$secondaryArchSubDir/freetype2
	freeTypeLib=$(finddir B_SYSTEM_DEVELOP_DIRECTORY)/lib$secondaryArchSubDir

	bash ./common/autoconf/autogen.sh
	bash ./configure --with-boot-jdk-jvmargs="-Xmx${maxHeapSize}" \
	                 --with-freetype-include="${freeTypeHeaders}" \
	                 --with-freetype-lib="${freeTypeLib}"

	make images
}

INSTALL()
{
	# install the generated SDK image dir
	jdkDir=$libDir/openjdk

	mkdir -p $(dirname $jdkDir)
	cp -a build/haiku-*/images/j2sdk-image $jdkDir

	addResourcesToBinaries $portDir/sources/jexec.rdef $jdkDir/lib/jexec

	# set up the cacerts file
	cacerts=$jdkDir/jre/lib/security/cacerts
	certDir=certs

	mkdir -p $certDir
	csplit -f $certDir/ /system/data/ssl/CARootCertificates.pem "/-----BEGIN CERTIFICATE-----/" {*}

	rm $certDir/00

	for cert in $certDir/*; do
		$jdkDir/bin/keytool -import -noprompt -keystore $cacerts \
			-storepass "changeit" -file $cert -alias "$cert"
	done

	# symlink the executables to binDir
	mkdir -p $prefix/bin
	symlinkRelative -s $jdkDir/bin/* $prefix/bin
		# TODO: We should probably link only a subset.

	# create a profile.d file that sets up JAVA_HOME
	jdkProfile=$dataDir/profile.d/openjdk.sh
	mkdir -p $(dirname $jdkProfile)
	echo "JAVA_HOME=$jdkDir" > $jdkProfile
	echo "export JAVA_HOME" >> $jdkProfile
}
