# Tanım: Java Development Kit (JDK) adı verilen bir dizi program.
# URL: http://openjdk.java.net/
# Paketçi: milisarge@gmail.com
# Gerekler: openjdk alsa-lib cups xorg-fontconfig freetype giflib gtk2 krb5 lcms2 libjpeg-turbo libpng libxslt nss unzip xorg-libxt xorg-libxtst xorg-libxinerama zip

isim=openjdk8
openjdk_devir=8
openjdk_update=u144
openjdk_build=b01
openjdk_surum=$openjdk_release$openjdk_update$openjdk_build
surum=8u144-b01
icedtea_surum=3.5.1
certdata_rev=e5e2984f44c5
devir=1
kaynak=(http://icedtea.wildebeest.org/download/source/icedtea-$icedtea_version.tar.xz
	http://stygian.me/crux/distfiles/$isim/corba-$openjdk_version.tar.xz
	http://stygian.me/crux/distfiles/$isim/hotspot-$openjdk_version.tar.xz
	http://stygian.me/crux/distfiles/$isim/jaxp-$openjdk_version.tar.xz
	http://stygian.me/crux/distfiles/$isim/jaxws-$openjdk_version.tar.xz
	http://stygian.me/crux/distfiles/$isim/jdk-$openjdk_version.tar.xz
	http://stygian.me/crux/distfiles/$isim/langtools-$openjdk_version.tar.xz
	http://stygian.me/crux/distfiles/$isim/nashorn-$openjdk_version.tar.xz
	http://stygian.me/crux/distfiles/$isim/openjdk-$openjdk_version.tar.xz
	http://stygian.me/crux/distfiles/$isim-boot.pkg.tar.xz
	http://ftp.netbsd.org/pub/pkgsrc/pkgsrc-2017Q2/pkgsrc/security/mozilla-rootcerts/files/mozilla-rootcerts.sh
	http://hg.mozilla.org/releases/mozilla-release/raw-file/$certdata_rev/security/nss/lib/ckfw/builtins/certdata.txt)

unpack_source() {
	for file in ${source[@]}; do
		case ${file##*/} in
			icedtea-$icedtea_version.tar.xz|\
			$isim-boot.pkg.tar.xz)
				bsdtar -p -o -C $SRC -xf $(get_filename $file) ;;
			*)
				cp $(get_filename $file) $SRC ;;
		esac
	done
}

derle() {
	JAVA_DIR=/opt/OpenJDK-8u60-bin

	mkdir $SRC/build
	cd $SRC/build

	LC_ALL=C \
	CFLAGS="$CFLAGS -fno-delete-null-pointer-checks -fno-omit-frame-pointer -fno-lifetime-dse" \
	CXXFLAGS="$CXXFLAGS -fno-delete-null-pointer-checks -fno-omit-frame-pointer -fno-lifetime-dse" \
	JAVA_HOME=$JAVA_DIR \
	PATH=$JAVA_DIR/bin:$PATH \
	CONFIG_SHELL=/bin/bash \
	../icedtea-$icedtea_version/configure \
		--prefix=/usr/lib/java/$isim \
		--disable-docs \
		--disable-downloading \
		--disable-precompiled-headers \
		--disable-tests \
		--disable-system-pcsc \
		--disable-system-sctp \
		--enable-system-cups \
		--enable-system-fontconfig \
		--enable-system-gconf \
		--enable-system-gio \
		--enable-system-gtk \
		--enable-system-jpeg \
		--enable-system-kerberos \
		--enable-system-lcms \
		--enable-system-png \
		--enable-system-zlib \
		--enable-nss \
		--enable-sunec \
		--enable-non-nss-curves \
		--enable-improved-font-rendering \
		--with-openjdk-src-zip=$SRC/openjdk-$openjdk_version.tar.xz \
		--with-hotspot-src-zip=$SRC/hotspot-$openjdk_version.tar.xz \
		--with-corba-src-zip=$SRC/corba-$openjdk_version.tar.xz \
		--with-jaxp-src-zip=$SRC/jaxp-$openjdk_version.tar.xz \
		--with-jaxws-src-zip=$SRC/jaxws-$openjdk_version.tar.xz \
		--with-jdk-src-zip=$SRC/jdk-$openjdk_version.tar.xz \
		--with-langtools-src-zip=$SRC/langtools-$openjdk_version.tar.xz \
		--with-nashorn-src-zip=$SRC/nashorn-$openjdk_version.tar.xz \
		--with-pkgsurum=MILIS \
		--with-jdk-home=$JAVA_DIR

	LC_ALL=C \
	CFLAGS="$CFLAGS -fno-delete-null-pointer-checks -fno-omit-frame-pointer -fno-lifetime-dse" \
	CXXFLAGS="$CXXFLAGS -fno-delete-null-pointer-checks -fno-omit-frame-pointer -fno-lifetime-dse" \
	JAVA_HOME=$JAVA_DIR \
	PATH=$JAVA_DIR/bin:$PATH \
	make

	mkdir -p $PKG/usr/lib/java/$isim
	cp -r openjdk.build/images/j2sdk-image/* $PKG/usr/lib/java/$isim

	# remove unneeded files
	rm -f $PKG/usr/lib/java/$isim/src.zip
	rm -rf $PKG/usr/lib/java/$isim/{demo,sample}
	rm -f $PKG/usr/lib/java/$isim/man/ja
	rm -rf $PKG/usr/lib/java/$isim/man/ja_JP.UTF-8
	find $PKG/usr/lib/java/$isim -name '*.ja' -delete

	# deduplicate
	rm $PKG/usr/lib/java/$isim/jre/lib/amd64/libjawt.so
	ln -s ../../../../lib/amd64/libjawt.so \
		$PKG/usr/lib/java/$isim/jre/lib/amd64/libjawt.so
	rm $PKG/usr/lib/java/$isim/jre/lib/amd64/jli/libjli.so
	ln -s ../../../../lib/amd64/jli/libjli.so \
		$PKG/usr/lib/java/$isim/jre/lib/amd64/jli/libjli.so
	for f in $PKG/usr/lib/java/$isim/jre/bin/* ; do
		if [ -f $PKG/usr/lib/java/$isim/bin/${f##*/} ]; then
			rm $f
			ln -s ../../bin/${f##*/} $f
		fi
	done

	sed -e "/# cd \/etc\/openssl\/certs/s//# cd \/usr\/share\/$isim\/certs/" \
		-e '/@AWK@/s//\/usr\/bin\/awk/' \
		-e '/@ECHO@/s//\/bin\/echo/' \
		-e '/@EXPR@/s//\/usr\/bin\/expr/' \
		-e '/@LN@/s//\/bin\/ln/' \
		-e '/@LS@/s//\/bin\/ls/' \
		-e '/@MKDIR@/s//\/bin\/mkdir/' \
		-e '/@OPENSSL@/s//\/bin\/openssl/' \
		-e "/@SSLDIR@/s//\/usr\/share\/$isim/" \
		-e '/@PREFIX@/s//\/usr/' \
		-e '/@RM@/s//\/bin\/rm/' \
		-e '/@LOCALBASE@/s//\/usr/' \
		-e "/@DATADIR@/s//\/usr\/share\/$isim/" \
		-e 's/self extract/self -f $certfile extract/' \
		-e 's/self rehash/self -f $certfile rehash/' \
		$SRC/mozilla-rootcerts.sh > $SRC/mozilla-rootcerts

	# generate java's cacerts
	# big thanks to pkgsrc's mozilla-rootcerts and openjdk pkg's for this part
	rm -f $PKG/usr/lib/java/$isim/jre/lib/security/cacerts
	mkdir $SRC/cacerts
	cd $SRC/cacerts
	sh $SRC/mozilla-rootcerts -f $SRC/certdata.txt extract
	for cert in *.pem; do
		JAVA_HOME=$PKG/usr/lib/java/$isim \
		PATH=$JAVA_HOME/bin:$PATH \
		keytool \
			-noprompt \
			-importcert \
			-keystore $PKG/usr/lib/java/$isim/jre/lib/security/cacerts \
			-alias $(echo $cert | sed 's,.*/\([^/]*\)\.pem,\1,') \
			-file $cert \
			-storepass changeit
	done
}
