DESCRIPTION="openjdk"
HOMEPAGE="http://openjdk.java.net/"
SRC_URI="http://ports-space.haiku-files.org/source/openjdk/j2sdk-april-2013.tar"
REVISION="3"
STATUS_HAIKU="stable"
DEPEND="app-arch/cpio 	= 2.10
	media-libs/freetype 	>= 2.4.9"

CHECKSUM_MD5="e38b939c34b04427336245397fa43c9c"

BUILD()
{
	OPENJDK_DIR=`haikuporter -t`/dev-lang/openjdk/work

	# Get and build Apache Ant.
	# Making a separate .bep file for it wasn't such a good idea.

	wget -nc http://archive.apache.org/dist/ant/source/apache-ant-1.8.4-src.tar.gz
	tar xvfz apache-ant-1.8.4-src.tar.gz
	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 apache-ant-1.8.4
	sed -i 's/depends=\"jars,test-jar\"/depends=\"jars\"/' build.xml
	sh build.sh install-lite
	unset JAVA_HOME
	cd ..

	# Clone latest JDK repositories.
	if [ "$(ls -A jdk)" ]; then
		echo "jdk directory is not empty"
	else
		hg clone http://hg.openjdk.java.net/haiku/haiku jdk --debug
	fi

	if [ "$(ls -A jdk/corba)" ]; then
		echo "jdk/corba directory is not empty"
	else
		hg clone http://hg.openjdk.java.net/haiku/haiku/corba jdk/corba --debug
	fi

	if [ "$(ls -A jdk/hotspot)" ]; then
		echo "jdk/hotspot directory is not empty"
	else
		hg clone http://hg.openjdk.java.net/haiku/haiku/hotspot jdk/hotspot --debug
	fi

	if [ "$(ls -A jdk/jaxp)" ]; then
		echo "jdk/jaxp directory is not empty"
	else
		hg clone http://hg.openjdk.java.net/haiku/haiku/jaxp jdk/jaxp --debug
	fi

	if [ "$(ls -A jdk/jaxws)" ]; then
		echo "jdk/jaxws directory is not empty"
	else
		hg clone http://hg.openjdk.java.net/haiku/haiku/jaxws jdk/jaxws --debug
	fi

	if [ "$(ls -A jdk/jdk)" ]; then
		echo "jdk/jdk directory is not empty"
	else
		hg clone http://hg.openjdk.java.net/haiku/haiku/jdk jdk/jdk --debug
	fi

	if [ "$(ls -A jdk/langtools)" ]; then
		echo "jdk/langtools directory is not empty"
	else
		hg clone http://hg.openjdk.java.net/haiku/haiku/langtools jdk/langtools --debug
	fi

	export ALT_BOOTDIR=${OPENJDK_DIR}/j2sdk-image

	# don't set ALT_JDK_IMPORT for a full build
	# set jdk/README-build.html
	# export ALT_JDK_IMPORT_PATH=${OPENJDK_DIR}/j2sdk-image

	export ANT=${OPENJDK_DIR}/ant
	export ALT_OUTPUTDIR=${OPENJDK_DIR}/output

	# set jdk/README-build.html
	export LANG=C

	# add $ANT_HOME/bin to PATH
	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 automaticly found
	# see Defs-haiku.gmk
	export ALT_UNIXCOMMAND_PATH=

	# mandatory GCC4 [requires at least 4.5.3]
	setgcc gcc4

	mkdir -p ${ALT_OUTPUTDIR}

	# some sources downloaded while building have moved
	# so we have to provide a directory where they have to reside to
	export ALT_DROPS_DIR=${OPENJDK_DIR}/drop
	mkdir -p ${ALT_DROPS_DIR}
	export ALLOW_DOWNLOADS=false

	# grab relocated sources
	cd ${ALT_DROPS_DIR}
	wget -nc http://ports-space.haiku-files.org/source/openjdk/jaxp145_01.zip
	wget -nc http://ports-space.haiku-files.org/source/openjdk/jdk7-jaf-2010_08_19.zip
	wget -nc http://ports-space.haiku-files.org/source/openjdk/jdk7-jaxws2_2_4-b03-2011_05_27.zip
	cd ..

	# Deploy certain headers, if they are missing.

	HEADERS_DIR=`finddir B_SYSTEM_HEADERS_DIRECTORY`
	MEDIA_DIR=${HEADERS_DIR}/os/media

	if [ -e ${MEDIA_DIR}/SoundConsumer.h ] ;
	then
		echo SoundConsumer.h exists.
	else
		cp SoundConsumer.h ${MEDIA_DIR}/SoundConsumer.h
		SOUND_CONSUMER_H=1
		echo SoundConsumer.h was not found. Deployed private file instead.
	fi

	if [ -e ${MEDIA_DIR}/SoundUtils.h ] ;
	then
		echo SoundUtils.h exists.
	else
		cp SoundUtils.h ${MEDIA_DIR}/SoundUtils.h
		declare SOUND_UTILS_H=1
		echo SoundUtils.h was not found. Deployed private file instead.
	fi

	# 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 rathan than patch should not brake
	# the whole build
	if [ -e jdk/make/Defs-internal.gmk.org ];
	then
		echo Defs-internal exists.
	else
		mv jdk/make/Defs-internal.gmk jdk/make/Defs-internal.gmk.org
	fi
	cat jdk/make/Defs-internal.gmk.org | sed s/'$(PRINTF) "--'/'$(PRINTF) -- "--'/ >jdk/make/Defs-internal.gmk

	# Build.
	cd jdk
	make

	# Cleanup private files, if any were deployed.
	# I don't really think it would harm anyone if we don't clean them up after compiling.

#	if [ ${SOUND_CONSUMER_H} == 1 ] ;
#	then
#		rm -f ${MEDIA_DIR}/SoundConsumer.h
#		echo Removed private SoundConsumer.h
#	fi
#
#	if [ ${SOUND_UTILS_H} == 1 ] ;
#	then
#		rm -f ${MEDIA_DIR}/SoundUtils.h
#		echo Removed private SoundUtils.h
#	fi
}

INSTALL()
{
	# install
	mkdir -p `finddir B_COMMON_DIRECTORY`/jdk
	cp -r -f -v output/j2sdk-image/* `finddir B_COMMON_DIRECTORY`/jdk
	# setup env
	$(haikuporter -t)/dev-lang/openjdk/openjdk_setup_env.sh

	# build distro
	mkdir -p ${DESTDIR}`finddir B_COMMON_DIRECTORY`/jdk
	cp -r -f -v output/j2sdk-image/* ${DESTDIR}`finddir B_COMMON_DIRECTORY`/jdk
	mkdir -p ${DESTDIR}/boot/common/boot/post_install
	cp -f $(haikuporter -t)/dev-lang/openjdk/openjdk_setup_env.sh ${DESTDIR}/boot/common/boot/post_install
}

TEST()
{
	echo "OpenJDK tests are not yet ported to Haiku."
}

LICENSE="GNU GPL v2"
COPYRIGHT="2005, 2006, Oracle and/or its affiliates"
