# Contributor: Timo Teras <timo.teras@iki.fi>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Timo Teras <timo.teras@iki.fi>
pkgname=openjdk7
_icedteaver=2.6.11
# pkgver is <JDK version>.<JDK update>
# check icedtea JDK when updating
pkgver=7.151.$_icedteaver
pkgrel=0
pkgdesc="OpenJDK 7 via IcedTea"
url="http://icedtea.classpath.org/"
arch="all"
license="GPL-2.0 with Classpath"
depends="$pkgname-jre java-cacerts"
options="sover-namecheck"
makedepends="bash findutils tar zip file paxmark gawk util-linux libxslt
	autoconf automake linux-headers coreutils
	ca-certificates
	nss-dev cups-dev jpeg-dev giflib-dev libpng-dev libxt-dev
	lcms2-dev libxp-dev libxtst-dev libxinerama-dev zlib-dev
	libxrender-dev alsa-lib-dev freetype-dev fontconfig-dev
	gtk+2.0-dev krb5-dev attr-dev pcsc-lite-dev lksctp-tools-dev
	libxcomposite-dev nss-static"
install=""

# upstream binary versions
RHINO_VER=1.7.7.1
ANT_VER=1.9.7

case $CARCH in
x86)		_jarch=i386;;
x86_64)		_jarch=amd64;;
aarch64)	_jarch=aarch64;;
arm*)		_jarch=arm
		# openjdk has quite a bit of arm assembly producing textrels
		# and it's non trivial to fix. allow it for now.
		options="$options textrels";;
s390x)		_jarch=s390x;;
ppc64le)	_jarch=ppc64le;;
esac

INSTALL_BASE=/usr/lib/jvm/java-1.7-openjdk
_jrelib="$INSTALL_BASE/jre/lib/$_jarch"

# exclude xawt from ldpath to avoid duplicate provides for libmawt.so
# (also in headless). in future this should be a virtual provides.
ldpath="$_jrelib:$_jrelib/native_threads:$_jrelib/headless:$_jrelib/server:$_jrelib/jli"
sonameprefix="$pkgname:"

subpackages="$pkgname-jre-lib:jrelib:noarch $pkgname-jre $pkgname-jre-base:jrebase
	$pkgname-doc:doc"

if [ "$BOOTSTRAP" != "no" ]; then
	makedepends="$makedepends java-gcj-compat"
	BOOTSTRAP_JAVA_HOME=/usr/lib/jvm/java-1.5-gcj/
else
	makedepends="$makedepends $pkgname"
	BOOTSTRAP_JAVA_HOME="$INSTALL_BASE"
fi

_dropsver=$_icedteaver
_dropsurl=http://icedtea.classpath.org/download/drops/icedtea7/$_dropsver

source="http://icedtea.classpath.org/download/source/icedtea-$_icedteaver.tar.gz
	openjdk-$_dropsver.tar.bz2::$_dropsurl/openjdk.tar.bz2
	corba-$_dropsver.tar.bz2::$_dropsurl/corba.tar.bz2
	jaxp-$_dropsver.tar.bz2::$_dropsurl/jaxp.tar.bz2
	jaxws-$_dropsver.tar.bz2::$_dropsurl/jaxws.tar.bz2
	jdk-$_dropsver.tar.bz2::$_dropsurl/jdk.tar.bz2
	langtools-$_dropsver.tar.bz2::$_dropsurl/langtools.tar.bz2
	hotspot-$_dropsver.tar.bz2::$_dropsurl/hotspot.tar.bz2
	http://archive.apache.org/dist/ant/binaries/apache-ant-$ANT_VER-bin.tar.gz
	https://github.com/mozilla/rhino/releases/download/Rhino${RHINO_VER//./_}_RELEASE/rhino-$RHINO_VER.zip

	icedtea-hotspot-musl.patch
	icedtea-hotspot-musl-ppc.patch
	icedtea-hotspot-noagent-musl.patch
	icedtea-hotspot-uclibc-fixes.patch
	icedtea-jdk-fix-build.patch
	icedtea-jdk-execinfo.patch
	icedtea-jdk-fix-ipv6-init.patch
	icedtea-jdk-musl.patch
	icedtea-jdk-no-soname.patch
	icedtea-cpio.patch
	"

# icedtea6-1.9.7-generate_cacerts-1.patch

builddir="$srcdir/icedtea-$_icedteaver"

unpack() {
	if [ -z "$force" ]; then
		verify || return 1
		initdcheck || return 1
	fi
	mkdir -p "$srcdir"
	msg "Unpacking sources..."
	tar -C "$srcdir" -zxf icedtea-$_icedteaver.tar.gz || return 1
	tar -C "$srcdir" -zxf apache-ant-$ANT_VER-bin.tar.gz || return 1
	unzip -o -q "rhino-$RHINO_VER.zip" -d "$srcdir" || return 1
}

prepare() {
	cd "$builddir"

	# Busybox sha256 does not support longopts
	sed -e "s/--check/-c/g" -i Makefile.am

	for patch in $source; do
		case $patch in
		icedtea-*.patch)
			cp ../$patch patches
			;;
		*.patch)
			msg "Applying patch $patch"
			patch -p1 -i "$srcdir"/$patch || return 1
			;;
		esac
	done

	./autogen.sh
}

build() {
	[ -z "$JOBS" ] && export JOBS=$(echo $MAKEFLAGS | sed -n -e 's/.*-j\([0-9]\+\).*/\1/p')
	export ALT_PARALLEL_COMPILE_JOBS="${JOBS:-2}"
	export HOTSPOT_BUILD_JOBS="${JOBS:-2}"

	export JAVA_HOME=$BOOTSTRAP_JAVA_HOME
	export PATH=$JAVA_HOME/bin:$srcdir/apache-ant-$ANT_VER/bin:$PATH
	export DISTRIBUTION_PATCHES=""

	# Explicitly set the C++ standard as the default has changed on GCC 6+
	# and disable optimizations that lead to a broken JVM. These options
	# has been adopted from the Fedora package.
	export EXTRA_CPP_FLAGS="$CXXFLAGS -std=gnu++98 -fno-delete-null-pointer-checks -fno-lifetime-dse"
	# CXXFLAGS doesn't make it to all calls, so we set the C++ standard
	# version for C too.
	export EXTRA_CFLAGS="$CFLAGS -std=gnu++98 -Wno-error -fno-delete-null-pointer-checks -fno-lifetime-dse"

	local patch
	for patch in $source; do
		case $patch in
		icedtea-*.patch)
			export DISTRIBUTION_PATCHES="$DISTRIBUTION_PATCHES patches/$patch"
			;;
		esac
	done
	echo "icedtea patches: $DISTRIBUTION_PATCHES"

	# --disable-arm32-jit: http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=2942
	cd "$builddir"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=$INSTALL_BASE \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-parallel-jobs=${JOBS:-2} \
		--disable-dependency-tracking \
		--disable-downloading \
		--disable-arm32-jit \
		--with-rhino=$srcdir/rhino$RHINO_VER/lib/rhino-$RHINO_VER.jar \
		--with-openjdk-src-zip=$srcdir/openjdk-$_dropsver.tar.bz2 \
		--with-hotspot-src-zip=$srcdir/hotspot-$_dropsver.tar.bz2 \
		--with-corba-src-zip=$srcdir/corba-$_dropsver.tar.bz2 \
		--with-jaxp-src-zip=$srcdir/jaxp-$_dropsver.tar.bz2 \
		--with-jaxws-src-zip=$srcdir/jaxws-$_dropsver.tar.bz2 \
		--with-jdk-src-zip=$srcdir/jdk-$_dropsver.tar.bz2 \
		--with-langtools-src-zip=$srcdir/langtools-$_dropsver.tar.bz2 \
		--with-pax=paxmark \
		--with-jdk-home=$BOOTSTRAP_JAVA_HOME \
		--with-pkgversion="Alpine ${pkgver}-r${pkgrel}" \
		|| return 1

	msg "Icedtea boot"
	make -j1 icedtea-boot || return 1
	msg "Icedtea boot done"
	make || return 1
}

package() {
	mkdir -p "$pkgdir"/$INSTALL_BASE
	cp -a "$builddir"/openjdk.build/j2sdk-image/* "$pkgdir"/$INSTALL_BASE || return 1
	rm "$pkgdir"/$INSTALL_BASE/src.zip

	# pax mark again (due to fakeroot xattr handling bug)
	"$builddir"/pax-mark-vm "$pkgdir"/$INSTALL_BASE

	# symlink to shared java cacerts store
	rm -f "$pkgdir"/$INSTALL_BASE/jre/lib/security/cacerts
	ln -sf /etc/ssl/certs/java/cacerts \
		"$pkgdir"/$INSTALL_BASE/jre/lib/security/cacerts
}

jrelib() {
	pkgdesc="OpenJDK 7 Java Runtime (class libraries)"
	depends=""

	local A; for A in jre/lib/zi jre/lib/images jre/lib/*.jar \
			jre/lib/security jre/lib/ext/*.jar jre/lib/cmm \
			jre/ASSEMBLY_EXCEPTION jre/THIRD_PARTY_README jre/LICENSE; do

		local dirname=${A%/*}
		mkdir -p "$subpkgdir"/$INSTALL_BASE/$dirname
		mv "$pkgdir"/$INSTALL_BASE/$A "$subpkgdir"/$INSTALL_BASE/$dirname || return 1
	done
}

jrebase() {
	pkgdesc="OpenJDK 7 Java Runtime (no GUI support)"
	depends="$pkgname-jre-lib java-common java-cacerts"

	mkdir -p "$subpkgdir"/$INSTALL_BASE/bin

	local A; for A in java orbd rmid servertool unpack200 keytool \
			pack200 rmiregistry tnameserv; do
		mv "$pkgdir"/$INSTALL_BASE/bin/$A "$subpkgdir"/$INSTALL_BASE/bin || return 1
	done

	# rest of the jre subdir (which were not taken by -jre subpkg)
	mv "$pkgdir"/$INSTALL_BASE/jre "$subpkgdir"/$INSTALL_BASE || return 1

	# pax mark again (due to fakeroot xattr handling bug)
	"$builddir"/pax-mark-vm "$subpkgdir"/$INSTALL_BASE
}

jre() {
	pkgdesc="OpenJDK 7 Java Runtime"
	# manually depend to avoid clash with libgcj's libjvm.so
	depends="so:openjdk7:libjvm.so=0"

	mkdir -p "$subpkgdir"
	local A; for A in jre/bin/policytool \
			bin/appletviewer \
			bin/policytool \
			jre/lib/$_jarch/xawt \
			jre/lib/$_jarch/libsplashscreen.so; do

		local dirname=${A%/*}
		mkdir -p "$subpkgdir"/$INSTALL_BASE/$dirname
		mv "$pkgdir"/$INSTALL_BASE/$A "$subpkgdir"/$INSTALL_BASE/$dirname || return 1
	done

	# pax mark again (due to fakeroot xattr handling bug)
	"$builddir"/pax-mark-vm "$subpkgdir"/$INSTALL_BASE
}

doc() {
	default_doc || return 1

	mkdir -p "$subpkgdir"/$INSTALL_BASE/
	mv "$pkgdir"/$INSTALL_BASE/man "$subpkgdir"/$INSTALL_BASE/
}

sha512sums="314fa192f9de58c224b1d40a4fe4a15a7a323f3a1454d27487b126a0822ced3d4305fbc401e892530c618a394d5f22e1b64a65a844fe6b0253dc0df36497bd25  icedtea-2.6.11.tar.gz
95a0168b109266dfe7af97355f34888559f1978201fc7fe9c1b7b15074793d850a68ce031603bb62c4ad6790e8575ebf7e73fca2fdd8f03c9ab0a4e24e3e07b8  openjdk-2.6.11.tar.bz2
87b374d6907dcd72043cb3a86eed08e836861c238282fdd8c9684b6653b5788f1d183038572282f92dd81381765ac41243c6faa52033173157bb8b48b928d071  corba-2.6.11.tar.bz2
a08618aca24f80f67669f62743150eaf7a33a602f9b5d806f3c0213261144a15006c4348800e25d5746500735498bc0c8380ee9adc75b6fce17283d9f4971358  jaxp-2.6.11.tar.bz2
145ae63e81a1c2cce6e462c2b38b05d79d138a89f5f4176151e87a8dcfb27c0b8f47c826ea12d85acdd31733ea253aaf9fbb019e605a4cc24c4b66803ca01f61  jaxws-2.6.11.tar.bz2
7974d9d7d55c4a57b53ee5e29f36ef058d207eee1701eac0e8b32b8f13a9084b08ff9fcde00cb16f4c23fa6bf371361f183b07f418cd6d200b67a67f86fa6785  jdk-2.6.11.tar.bz2
6301ca17725dcf085b4dea74d453bb2ec9e7620b6791bdba6cf70fd4fac3f53679c8281db17c343ae4639a3c5431d9af09d76619825e2a8f3072bf46e09a9ce1  langtools-2.6.11.tar.bz2
8d4bb7a97e2213d21a99261d9f9a32824830f1b178260cb170eeb45ac54414bd26042dc75b2dc719850638571cf8057d7d3fdb65c32fbed8797e6242ba257e7f  hotspot-2.6.11.tar.bz2
85d35441a3ecb1eb5c87b112ee9f3a8accbb16a1b9e249f706f0253566fcda7952f5a9cc08bd8c829b1c93d40ae452e1b1c9e29523a51a3c8a5032fa9c22e135  apache-ant-1.9.7-bin.tar.gz
fba60e83db6e6dd7ef0a834e8aa6858af41b973e01a54392f11ed1d7d152b9641a48e5d473b434e062e8b8d5d19281da1445751e2b0e297ecd25d2680efc435a  rhino-1.7.7.1.zip
32e603be6f4be91e7cb2bca4fe0c6c81e7ca8728333355d15f553d10675c65885d655dd14982d6e8ac3d82229d9688432d61db5f63852a9d4364f7b76862a35e  icedtea-hotspot-musl.patch
a6b12034217bf7adf0880a7e5697722b109793823a7db303fc1363e14d625976c4a3693a8145f9bdfbdc25180a46d64f21f3460cdc65da2dab03ddedb6bd1247  icedtea-hotspot-musl-ppc.patch
e7a2c1771bb582d427041f8d22e48c0daf8f20d7c0926cbce3549d49c4e949359ee25a35682b486e82f3e390535c950c5beee3bd8d06fb5a717b50f2d9b2a6bc  icedtea-hotspot-noagent-musl.patch
822eee0dc4d3ba677a289dfeb3668b536d2d626708390a9d9702fb4144a481fd443a215a0d2041c6026113837aafa4ba0b5e9ead8589d2da6717a238bbc95a5a  icedtea-hotspot-uclibc-fixes.patch
213a537de5f011cb39d608515c3413513ac75fb93593f9a9ef4205f71d72bdd8b097c80db185f7b26021d5bb85045b866f34f3478482dc4189972d8614a13458  icedtea-jdk-fix-build.patch
0391970e6a32946aa3cccf38fdef9c0fe2af26cd0df824b98aa2fcfa1bf661d4a68e339bffcfd16f386c565fc68bb28a29208a67d4bad8a0e847ad02bd8becbb  icedtea-jdk-execinfo.patch
48533f87fc2cf29d26b259be0df51087d2fe5b252e72d00c6ea2f4add7b0fb113141718c116279c5905e03f64a1118082e719393786811367cf4d472b5d36774  icedtea-jdk-fix-ipv6-init.patch
44a35941c80f408d0607e32763b3b6ccee21e1d39886309327d3d74d2900117e4346ef59e77c663fd022fec10ee8f365eeb46c1260014d5765d226ce175ce3c5  icedtea-jdk-musl.patch
bf4b184e170f7b0ff64ab30d2162784fe2bd5460d1fa31973259f7065fd4c511c46f97724fe2bd72bb94e9006cb568d0e0c87d1a9c90819e65880f8f44830bb1  icedtea-jdk-no-soname.patch
a54c79c82afa1bc95265397b274260584c8b8c6be1651ddfb907d9523a809ea4581409e0d3fb0bbb63ef5a204e8ce29b7940e78cd640af1f490ae938c59129b6  icedtea-cpio.patch"
