SUMMARY="Open-source implementation of the Java Platform, SE"
DESCRIPTION="
Open-source implementation of the Java Platform, Standard Edition.
"
HOMEPAGE="http://openjdk.java.net/"
COPYRIGHT="2005, 2006, Oracle and/or its affiliates"
LICENSE="GNU GPL v2"

#SRC_URI="http://dl.dropbox.com/u/61946213/j2sdk-august.tar.xz"
#CHECKSUM_MD5="65b348918f92eb357ed7b455dadcded8"
# The August j2sdk image is apparently incomplete.
SRC_URI="http://ports-space.haiku-files.org/source/openjdk/j2sdk-april-2013.tar"
CHECKSUM_MD5="e38b939c34b04427336245397fa43c9c"
SOURCE_DIR="j2sdk-image"

SRC_URI_2="http://archive.apache.org/dist/ant/source/apache-ant-1.8.4-src.tar.gz"
CHECKSUM_MD5_2="4e05e1cc4ea4b56d85bbbc4fe29d1c29"
SOURCE_DIR_2="apache-ant-1.8.4"

SRC_URI_3="hg+http://hg.openjdk.java.net/haiku/haiku#ee67ee3bd597"

SRC_URI_4="hg+http://hg.openjdk.java.net/haiku/haiku/corba#84b78407a004"
PATCHES_4="openjdk-1.7_2013_11_08-source4.patchset"

SRC_URI_5="hg+http://hg.openjdk.java.net/haiku/haiku/hotspot#03a6a75222a8"
PATCHES_5="openjdk-1.7_2013_11_08-source5.patchset"

SRC_URI_6="hg+http://hg.openjdk.java.net/haiku/haiku/jaxp#6c9ac74190a0"
SRC_URI_7="hg+http://hg.openjdk.java.net/haiku/haiku/jaxws#84f654f41f6b"

SRC_URI_8="hg+http://hg.openjdk.java.net/haiku/haiku/jdk#df0cce7b0238"
PATCHES_8="openjdk-1.7_2013_11_08-source8.patchset"

SRC_URI_9="hg+http://hg.openjdk.java.net/haiku/haiku/langtools#0dabb2f5fad0"

SRC_URI_10="http://ports-space.haiku-files.org/source/openjdk/jaxp145_01.zip#noarchive"
CHECKSUM_MD5_10="32394c780c8fb5e29775f623525993c0"

SRC_URI_11="http://ports-space.haiku-files.org/source/openjdk/jdk7-jaf-2010_08_19.zip#noarchive"
CHECKSUM_MD5_11="18d15dfd71117daadb332af003d08212"

SRC_URI_12="http://ports-space.haiku-files.org/source/openjdk/jdk7-jaxws2_2_4-b03-2011_05_27.zip#noarchive"
CHECKSUM_MD5_12="2f5b829ade70f67fe272d0b322e3e702"

REVISION="2"
ARCHITECTURES="x86 !x86_gcc2"

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

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

REQUIRES="
	haiku >= $haikuVersion
	lib:libfreetype
	lib:libiconv
	lib:libz
	"

BUILD_REQUIRES="
	haiku_devel >= $haikuVersion
	devel:libfreetype
	devel:libiconv
	devel:libz
	"

BUILD_PREREQUIRES="
	cmd:cpio >= 2.10
	cmd:make
	cmd:gcc
	cmd:ld
	cmd:sed
	cmd:tar
	cmd:zip
	"

PATCH()
{
	# patch Ant
	cd $sourceDir2
	sed -i 's/depends=\"jars,test-jar\"/depends=\"jars\"/' build.xml

	# small patch allowing to finish build without errors
	# this modification already exists in another dev branch
	# if someone fix it in current branch, using sed rather than patch should
	# not brake the whole build
	cd $sourceDir3
	sed -i s/'$(PRINTF) "--'/'$(PRINTF) -- "--'/ make/Defs-internal.gmk
}

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

	# build Apache Ant
	export LIBRARY_PATH=${OPENJDK_DIR}/j2sdk-image/jre/lib/i386/:$LIBRARY_PATH
	export JAVA_HOME=${OPENJDK_DIR}/j2sdk-image
	export ANT_HOME=${OPENJDK_DIR}/ant
	cd $sourceDir2
	sh build.sh install-lite
	unset JAVA_HOME

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

	export ALT_DROPS_DIR=${OPENJDK_DIR}/drop
	mkdir -p ${ALT_DROPS_DIR}

	cp $sourceDir10/jaxp145_01.zip $ALT_DROPS_DIR
	cp $sourceDir11/jdk7-jaf-2010_08_19.zip $ALT_DROPS_DIR
	cp $sourceDir12/jdk7-jaxws2_2_4-b03-2011_05_27.zip $ALT_DROPS_DIR

	# set up environment
	export ALT_BOOTDIR=${OPENJDK_DIR}/j2sdk-image
	export ALT_JDK_IMPORT_PATH=$ALT_BOOTDIR
	export ANT=${OPENJDK_DIR}/ant
	export ALT_OUTPUTDIR=${OPENJDK_DIR}/output
	export LANG=C
	export PATH=$PATH:$ANT_HOME/bin

	# use PATH to search commands rather than explicitly set path for each
	# commands so that grep, egrep & readelf are automatically found
	# see Defs-haiku.gmk
	export ALT_UNIXCOMMAND_PATH=

	mkdir -p ${ALT_OUTPUTDIR}

	export ALLOW_DOWNLOADS=false

	export USE_EXTERNAL_ZLIB=true

	# Build.
	cd jdk
	make

	# The build system is broken in that it doesn't pick up all auto-generated
	# java sources (it uses "find ... -name '*.java'" too early). Unfortunately
	# that isn't easy to fix, so we just run a second make, which will pick up
	# the then already existing files.
	make
}

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

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

	# create a java-config script in binDir
	javaConfig=$binDir/java-config
	sed -e "s,%JAVA_HOME%,$jdkDir," $portDir/sources/java-config.in \
		> $javaConfig
	chmod a+x $javaConfig

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