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="2012 Oracle and/or its affiliates."
LICENSE="GNU GPL v2"

SOURCE_URI="https://dl.dropboxusercontent.com/u/61946213/j2sdk-image-140528.tar.xz"
CHECKSUM_SHA256="fe9ad5156baf62a14213667f18bf88035daf5f24d6370afc9af30d079d8609a3"
SOURCE_DIR="j2sdk-image"

SOURCE_URI_2="http://www.eu.apache.org/dist/ant/binaries/apache-ant-1.9.4-bin.zip"
CHECKSUM_SHA256_2="973d97d656e65561e2753823de6cf6db35570aaf93eeec6e7cb3d68e1ec4d0e8"
SOURCE_DIR_2="apache-ant-1.9.4"

SOURCE_URI_3="hg+http://hg.openjdk.java.net/jdk7u/jdk7u#jdk7u80-b32"
SOURCE_URI_4="hg+http://hg.openjdk.java.net/jdk7u/jdk7u/langtools#jdk7u80-b32"
SOURCE_URI_5="hg+http://bitbucket.org/hamishm/haiku-jdk7u-corba#58510ce"
SOURCE_URI_6="hg+http://hg.openjdk.java.net/jdk7u/jdk7u/jaxp#jdk7u80-b32"
SOURCE_URI_7="hg+http://hg.openjdk.java.net/jdk7u/jdk7u/jaxws#jdk7u80-b32"
SOURCE_URI_8="hg+http://bitbucket.org/hamishm/haiku-jdk7u-hotspot#43c8a75"
SOURCE_URI_9="hg+http://bitbucket.org/hamishm/haiku-jdk7u-jdk#5d173cf"

REVISION="4"
ARCHITECTURES="!x86_gcc2 ?x86"
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.7
	cmd:jexec = $portVersion compat >= 1.7
	cmd:appletviewer = $portVersion compat >= 1.7
	cmd:apt = $portVersion compat >= 1.7
	cmd:extcheck = $portVersion compat >= 1.7
	cmd:idlj = $portVersion compat >= 1.7
	cmd:jar = $portVersion compat >= 1.7
	cmd:jarsigner = $portVersion compat >= 1.7
	cmd:java = $portVersion compat >= 1.7
	cmd:java_config = $portVersion compat >= 1.7
	cmd:javac = $portVersion compat >= 1.7
	cmd:javadoc = $portVersion compat >= 1.7
	cmd:javah = $portVersion compat >= 1.7
	cmd:javap = $portVersion compat >= 1.7
	cmd:jcmd = $portVersion compat >= 1.7
	cmd:jconsole = $portVersion compat >= 1.7
	cmd:jdb = $portVersion compat >= 1.7
	cmd:jhat = $portVersion compat >= 1.7
	cmd:jinfo = $portVersion compat >= 1.7
	cmd:jmap = $portVersion compat >= 1.7
	cmd:jps = $portVersion compat >= 1.7
	cmd:jrunscript = $portVersion compat >= 1.7
	cmd:jsadebugd = $portVersion compat >= 1.7
	cmd:jstack = $portVersion compat >= 1.7
	cmd:jstat = $portVersion compat >= 1.7
	cmd:jstatd = $portVersion compat >= 1.7
	cmd:keytool = $portVersion compat >= 1.7
	cmd:native2ascii = $portVersion compat >= 1.7
	cmd:orbd = $portVersion compat >= 1.7
	cmd:pack200 = $portVersion compat >= 1.7
	cmd:policytool = $portVersion compat >= 1.7
	cmd:rmic = $portVersion compat >= 1.7
	cmd:rmid = $portVersion compat >= 1.7
	cmd:rmiregistry = $portVersion compat >= 1.7
	cmd:schemagen = $portVersion compat >= 1.7
	cmd:serialver = $portVersion compat >= 1.7
	cmd:servertool = $portVersion compat >= 1.7
	cmd:tnameserv = $portVersion compat >= 1.7
	cmd:unpack200 = $portVersion compat >= 1.7
	cmd:wsgen = $portVersion compat >= 1.7
	cmd:wsimport = $portVersion compat >= 1.7
	cmd:xjc = $portVersion compat >= 1.7
	"

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

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	ca_root_certificates
	devel:libfreetype$secondaryArchSuffix
	devel:libiconv$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
	"

PATCHES="openjdk_x86-1.7.u80_b32-source9.patchset"
ADDITIONAL_FILES="jexec.rdef"

BUILD()
{
	OPENJDK_DIR=$(dirname $sourceDir)

	# prepare the directory structure
	cd $OPENJDK_DIR
	ln -sfn $sourceDir3 jdk
	ln -sfn $sourceDir4 jdk/langtools
	ln -sfn $sourceDir5 jdk/corba
	ln -sfn $sourceDir6 jdk/jaxp
	ln -sfn $sourceDir7 jdk/jaxws
	ln -sfn $sourceDir8 jdk/hotspot
	ln -sfn $sourceDir9 jdk/jdk

	chmod u+x ${sourceDir2}/bin/ant

	# set up environment
	export ALT_BOOTDIR=${OPENJDK_DIR}/j2sdk-image
	export ANT_HOME=${sourceDir2}
	export ALT_OUTPUTDIR=${OPENJDK_DIR}/output
	export ALT_UNIXCOMMAND_PATH=

	export LC_ALL=C

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

	if [ -n "$secondaryArchSuffix" ]; then
		export ALT_COMPILER_PATH=`finddir B_SYSTEM_BIN_DIRECTORY`$secondaryArchSubDir
		export ALT_FREETYPE_HEADERS_PATH=`finddir B_SYSTEM_HEADERS_DIRECTORY`$secondaryArchSubDir
	fi

	# Build. (multijob make sometimes fails)
	cd jdk
	make
}

INSTALL()
{
	# install the generated SDK image dir
	cd ..
	jdkDir=$libDir/openjdk
	mkdir -p $(dirname $jdkDir)
	cp -a output/j2sdk-image $jdkDir

	addResourcesToBinaries $portDir/additional-files/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

	packageEntries devel \
		$libDir/openjdk/src.zip \
		$libDir/openjdk/sample \
		$libDir/openjdk/demo
}

SUMMARY_devel="Source files, demos, and examples from the OpenJDK Development Kit"
PROVIDES_devel="
	openjdk${secondaryArchSuffix}_devel = $portVersion compat >= 1.7
	devel:openjdk$secondaryArchSuffix
	"
REQUIRES_devel="
	openjdk$secondaryArchSuffix
	"
