# TODO: better places for bin and man?
# TODO: make -headless versions
# TODO: config files?
# TODO: find giflib patch for --with-giflib=system

_java_ver=8
_jdk_update=60
_jdk_build=05
_repo_ver="jdk${_java_ver}u${_jdk_update}-b${_jdk_build}"
_openjdk_version="openjdk-1.8.0_${_jdk_update}"

# Template file for 'openjdk'
pkgname=openjdk
version=${_java_ver}u${_jdk_update}
revision=1
nocross=yes
wrksrc=jdk8u-jdk8u${_jdk_update}-b${_jdk_build}/
build_style=gnu-configure
configure_args="
 --prefix=${XBPS_DESTDIR}/${sourcepkg}-${version}/usr/lib
 --with-update-version=${_jdk_update}
 --with-build-number=b${_jdk_build}
 --with-milestone=fcs
 --enable-unlimited-crypto
 --with-zlib=system
 $(vopt_if oracle_bootstrap --with-boot-jdk=/usr/lib/jvm/oracle-jdk \
			    --with-boot-jdk=/usr/lib/jvm/openjdk)"
make_build_args="DEBUG_BINARIES=true docs all"
hostmakedepends="pkg-config cpio unzip zip ca-certificates libressl-openssl"
case "$XBPS_MACHINE" in
	arm*) hostmakedepends+=" $(vopt_if oracle_bootstrap oracle-jdk-arm openjdk)";;
	*) hostmakedepends+=" $(vopt_if oracle_bootstrap oracle-jdk openjdk)";;
esac
makedepends="libXrender-devel libXtst-devel libXt-devel
 libjpeg-turbo-devel cups-devel freetype-devel alsa-lib-devel"
case "$XBPS_TARGET_MACHINE" in
	arm*)
		makedepends+=" libffi-devel"
		configure_args+=" --with-jvm-variants=zero"
esac
depends="openjdk-jre-${version}_${revision}"
provides="java-environment-${version}_1"
replaces="java-environment>=0"
short_desc="OpenJDK Java Development Kit"
maintainer="Christian Neukirchen <chneukirchen@gmail.com>"
license="GPL-3"
homepage="http://openjdk.java.net/"
distfiles="
 http://hg.openjdk.java.net/jdk8u/jdk8u/archive/${_repo_ver}.tar.gz>jdk8u-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/jdk8u/jdk8u/corba/archive/${_repo_ver}.tar.gz>corba-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/archive/${_repo_ver}.tar.gz>hotspot-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/archive/${_repo_ver}.tar.gz>jdk-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/archive/${_repo_ver}.tar.gz>jaxws-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/jdk8u/jdk8u/jaxp/archive/${_repo_ver}.tar.gz>jaxp-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/archive/${_repo_ver}.tar.gz>langtools-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/jdk8u/jdk8u/nashorn/archive/${_repo_ver}.tar.gz>nashorn-${_repo_ver}.tar.gz"

checksum="
 ac109bdcf7b683c8c1151ff334896b805a1e5a77c1a6f2ebfc861a0c9aad9f5b
 05532b78726ea27617b219b40dd8ef061ecb8803b93d795696e5a4f33dd59aed
 e91399a4882ddad4b9cb70cd41081c54915cc84e2553742e2a3de11a4bd9231a
 e0b78561ea300aa9ad803ff08237ea1e89e2a384f3df5d90e2b9d5e3166fa0da
 ca4a0bcf118ab74ae7a7a230f549ba3a583e3864ed6f8b15e197849376349481
 92caf350d5a596bd4bd301e2bf7a7e4b62b7a762cf9c961cb21c70bdebf6802f
 679d9b2ac707b5de62842d3570c63c682f2c4787bf4afc358a1bc50113674521
 b2d562dc45ba1deb38f9bb15cbb7fe73ebb269c3bfd275539901d75d9c2fd4d5"

# Build is still parallel, but don't use -jN.
disable_parallel_build=yes

build_options="oracle_bootstrap"
build_options_default="oracle_bootstrap"
desc_option_oracle_bootstrap="Bootstrap using Oracle JDK"

post_extract() {
	chmod +x configure
	for subrepo in corba hotspot jdk jaxws jaxp langtools nashorn; do
		ln -s ../${subrepo}-${_repo_ver} ${subrepo}
	done
	cd hotspot
	# https://bugs.openjdk.java.net/browse/JDK-8041658
	patch -p1 < ${FILESDIR}/002_gcc.make-4.9.patch
}

post_install() {
	rm -rf ${DESTDIR}/usr/lib/bin
	vinstall ${FILESDIR}/mkcacerts 755 usr/lib/jvm/$_openjdk_version/jre/bin
	vmkdir usr/lib/jvm/$_openjdk_version/jre/lib/security
	sh ${FILESDIR}/mkcacerts \
		-d "/usr/share/ca-certificates/" \
		-s "/usr/bin/openssl"  \
		-k "${DESTDIR}/usr/lib/jvm/$_openjdk_version/jre/bin/keytool" \
		-o "${DESTDIR}/usr/lib/jvm/$_openjdk_version/jre/lib/security/cacerts"
	vlicense ASSEMBLY_EXCEPTION
	vlicense LICENSE
	vlicense THIRD_PARTY_README
}

openjdk-jre_package() {
	shlib_provides="libawt.so libawt_xawt.so libjava.so libjli.so libjvm.so"
	short_desc="OpenJDK Java Runtime Environment"
	provides="java-runtime-${version}_1"
	replaces="java-runtime>=0"
	pkg_install() {
		vmove "usr/lib/jvm/$_openjdk_version/jre"
		ln -s $_openjdk_version ${PKGDESTDIR}/usr/lib/jvm/openjdk
		vmkdir etc/profile.d
		cat >>${PKGDESTDIR}/etc/profile.d/10_openjdk.sh <<EOF
export JAVA_HOME=\${JAVA_HOME=/usr/lib/jvm/$_openjdk_version/jre}
export PATH="\$PATH:/usr/lib/jvm/$_openjdk_version/jre/bin:/usr/lib/jvm/$_openjdk_version/bin"
EOF
	}
}

openjdk-src_package() {
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	short_desc+=" -- sources"
	pkg_install() {
		vmove "usr/lib/jvm/$_openjdk_version/src.zip"
	}
}

openjdk-doc_package() {
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	short_desc+=" -- documentation"
	pkg_install() {
		cd ${wrksrc}
		vmkdir usr/share/doc
		cp -a build/linux-*/docs/* ${PKGDESTDIR}/usr/share/doc
	}
}
