# Copyright 1999-2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

EAPI="6"

inherit eutils java-pkg-2

B_PV="0.13.17"
B_PN="sbt"
B_P="${B_PN}-${B_PV}"

SB_PV="1.2.0-RC1"
SB_PN="sbt"
SB_P="${SB_PN}-${SB_PV}"

IO_PV="1.2.0-M2"
IO_PN="io"
IO_P="${P}-${IO_PN}-${IO_PV}"

UTIL_PV="1.2.0-M2"
UTIL_PN="util"
UTIL_P="${P}-${UTIL_PN}-${UTIL_PV}"

LM_PV="1.2.0-M3"
LM_PN="librarymanagement"
LM_P="${P}-${LM_PN}-${LM_PV}"

Z_PV="1.2.0-M1"
Z_PN="zinc"
Z_P="${P}-${Z_PN}-${Z_PV}"

SV="2.12"

DESCRIPTION="sbt is a build tool for Scala and Java projects that aims to do the basics well"
HOMEPAGE="http://www.scala-sbt.org/"
# SRC_URI="
# 	!binary? (
# 		https://dev.gentoo.org/~gienah/snapshots/${P}-src.tar.xz
# 		https://dev.gentoo.org/~gienah/snapshots/${P}-ivy2-deps.tar.xz
# 		https://dev.gentoo.org/~gienah/snapshots/${P}-sbt-deps.tar.xz
# 	)
# 	binary? (
# 		https://dev.gentoo.org/~gienah/files/dist/${P}-gentoo-binary.tar.xz
# 	)"

SRC_URI="
	!binary? (
		https://github.com/sbt/${B_PN}/releases/download/v${B_PV}/${B_P}.tgz
		https://github.com/sbt/${SB_PN}/archive/v${SB_PV}.zip -> ${SB_P}.zip
		https://github.com/sbt/${IO_PN}/archive/v${IO_PV}.zip -> ${IO_P}.zip
		https://github.com/sbt/${UTIL_PN}/archive/v${UTIL_PV}.zip -> ${UTIL_P}.zip
		https://github.com/sbt/${LM_PN}/archive/v${LM_PV}.zip -> ${LM_P}.zip
		https://github.com/sbt/${Z_PN}/archive/v${Z_PV}.zip -> ${Z_P}.zip
	)
"

LICENSE="BSD"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="binary"

DEPEND=">=virtual/jdk-1.8
	>=dev-lang/scala-2.12.4:${SV}"
RDEPEND=">=virtual/jre-1.8
	dev-java/jline:2
	>=dev-lang/scala-2.12.4:${SV}"

# test does not seem to do anything except instantly fail
RESTRICT="test"

JAVA_GENTOO_CLASSPATH="scala-${SV},jline-2"

S=${WORKDIR}/${SB_P}

# Note: to bump sbt, some things to try are:
# 1. remove the https://dev.gentoo.org/~gienah/snapshots/${P}-ivy2-deps.tar.xz
# https://dev.gentoo.org/~gienah/snapshots/${P}-sbt-deps.tar.xz and
# binary? ( https://dev.gentoo.org/~gienah/files/dist/${P}-gentoo-binary.tar.xz )
# from SRC_URI
# 2. Comment the sbt publishLocal line in src_compile.
# 3. try:
# FEATURES='noclean -test' emerge -v -1 dev-java/sbt
# It should fail in src_install since the sbt publishLocal is not done.
# Check if it downloads more stuff in
# src_compile to ${WORKDIR}/.ivy2 and ${WORKDIR}/.sbt.
# 4. If some of the downloads fail, it might be necessary to run the sbt compile
# again manually to obtain all the dependencies, if so:
# cd to ${S}
# export EROOT=/
# export WORKDIR='/var/tmp/portage/dev-java/${P}/work'
# export SV="2.11"
# export SB_P=${P}
# export PATH="/usr/share/scala-${SV}/bin:${WORKDIR}/${SB_P}:${PATH}"
# sbt compile
# cd ${WORKDIR}
# find .ivy2 .sbt -uid 0 -exec chown portage:portage {} \;
# 5. cd ${WORKDIR}
# XZ_OPT=-9 tar --owner=portage --group=portage \
# -cJf /usr/portage/distfiles/${P}-ivy2-deps.tar.xz .ivy2/cache
# XZ_OPT=-9 tar --owner=portage --group=portage \
# -cJf /usr/portage/distfiles/${P}-sbt-deps.tar.xz .sbt
# Uncomment the sbt publishLocal line in src_compile.
# 6. It *might* download more dependencies for src_test, however the presence
# of some of these may cause the src_compile to fail.  So download them
# seperately as root so we can identify the
# additional files.  As root:
# cd ${S}
# ${S}/${P} test
# cd ${WORKDIR}
# XZ_OPT=-9 tar --owner=portage --group=portage \
# -cJf /usr/portage/distfiles/${P}-test-deps.tar.xz \
# $(find .ivy2/cache .sbt -uid 0 -type f -print)
# Note: It might not download anything in src_test, in which case
# ${P}-test-deps.tar.xz is not required.
# 7. Create the binary
# cd $WORDKIR
# XZ_OPT=-9 tar --owner=portage --group=portage \
# -cJf /usr/portage/distfiles/${P}-gentoo-binary.tar.xz ${P} .ivy2/local
# 9. Undo the earlier temporary edits to the ebuild.

src_unpack() {
	# Unpack tar and zip files only.
	for f in ${A}; do
		[[ ${f} == *".tgz" ]] || [[ ${f} == *".tar."* ]] || [[ ${f} == *".zip" ]] && unpack ${f}
	done
}

src_prepare() {
	default
	if ! use binary; then
		sed -e "s@scalaVersion := scala210,@scalaVersion := scala${SV/./},\n  scalaHome := Some(file(\"${EROOT}usr/share/scala-${SV}\")),@" \
			-i "${S}/build.sbt" || die

		# suppress this warning in build.log:
		# [warn] Credentials file /var/tmp/portage/dev-java/${P}/work/.bintray/.credentials does not exist
		mkdir -p "${WORKDIR}/.bintray" || die
		cat <<- EOF > "${WORKDIR}/.bintray/.credentials"
			realm = Bintray API Realm
			host = api.bintray.com
			user =
			password =
		EOF

		cat <<- EOF > "${S}"/.jvmopts || die
			-Djava.io.tmpdir="${T}" -Duser.home="${WORKDIR}"
		EOF

		cat <<-EOF >>"${S}"/.sbtopts || die
			-Dsbt.server.autostart=false
		EOF

		for v in "1.0" "1.1" "1.2"; do
			if [ ! -d "${WORKDIR}"/.sbt/${v} ]; then
				mkdir -p "${WORKDIR}"/.sbt/${v} || die
			fi

			cat <<-EOF >"${WORKDIR}"/.sbt/${v}/global.sbt || die
				SettingKey[Boolean]("autoStartServer", "") := false
			EOF
		done
	fi
}

sbt_env() {
	export PATH="${EROOT}usr/share/scala-${SV}/bin:${WORKDIR}/${SB_P}:${PATH}"
}

src_compile() {
	if ! use binary; then
		einfo "=== sbt compile ..."
		sbt_env
		# "${WORKDIR}/${SB_P}/bin/sbt" \
		# strace -o sbt.out -f bash -c "${WORKDIR}/${SB_P}/bin/sbt" \
		declare -a cc=(
			"${WORKDIR}/${B_PN}/bin/sbt"
			"-Dsbt.log.noformat=true"
			"-Dsbt.server.autostart=false"
			"-Dsbtio.path=../${IO_PN}-${IO_PV}"
			"-Dsbtutil.path=../${UTIL_PN}-${UTIL_PV}"
			"-Dsbtlm.path=../${LM_PN}-${LM_PV}"
			"-Dsbtzinc.path=../${Z_PN}-${Z_PV}"
		)
		einfo "${cc[@]} publishLocalAllModule"
		# cat <<- EOF | bash -c "${WORKDIR}/${SB_P}/bin/sbt" \
		# 	-Dsbt.log.noformat=true \
		# 	-Dsbt.server.autostart=false \
		# 	-Dsbtio.path=../${IO_PN}-${IO_PV} \
		# 	-Dsbtutil.path=../${UTIL_PN}-${UTIL_PV} \
		# 	-Dsbtlm.path=../${LM_PN}-${LM_PV} \
		# 	-Dsbtzinc.path=../${Z_PN}-${Z_PV} || die
		# 	compile
		# 	exit
		# EOF
		# cat <<- EOF | java -XX:ReservedCodeCacheSize=128m \
		# 				   -XX:MaxMetaspaceSize=256m \
		# 				   -Djava.io.tmpdir="${T}" \
		# 				   -Duser.home="${WORKDIR}" \
		# 				   -Xms2048M \
		# 				   -Xmx2048M \
		# 				   -Xss2M \
		# 				   -jar "${WORKDIR}/${SB_PN}/bin/sbt-launch.jar" \
		# 				   -Dsbt.log.noformat=true \
		# 				   -Dsbt.server.autostart=false \
		# 				   -Dsbtio.path=../${IO_PN}-${IO_PV} \
		# 				   -Dsbtutil.path=../${UTIL_PN}-${UTIL_PV} \
		# 				   -Dsbtlm.path=../${LM_PN}-${LM_PV} \
		# 				   -Dsbtzinc.path=../${Z_PN}-${Z_PV} || die
		# 	publishLocalAllModule
		# 	exit
		# EOF

		# java -XX:ReservedCodeCacheSize=128m \
		# 	 -XX:MaxMetaspaceSize=256m \
		# 	 -Djava.io.tmpdir="${T}" \
		# 	 -Duser.home="${WORKDIR}" \
		# 	 -Xms2048M \
		# 	 -Xmx2048M \
		# 	 -Xss2M \
		# 	 -jar "${WORKDIR}/${B_PN}/bin/sbt-launch.jar" \
		# 	 -Dsbt.log.noformat=true \
		# 	 -Dsbtio.path=../${IO_PN}-${IO_PV} \
		# 	 -Dsbtutil.path=../${UTIL_PN}-${UTIL_PV} \
		# 	 -Dsbtlm.path=../${LM_PN}-${LM_PV} \
		# 	 -Dsbtzinc.path=../${Z_PN}-${Z_PV} \
		# 	 publishLocalAllModule || die

		# "${WORKDIR}/${B_PN}"/bin/sbt -d \
		# 	 -Dsbt.log.noformat=true \
		# 	 -Dsbt.server.autostart=false \
		# 	 -Dsbtio.path=../${IO_PN}-${IO_PV} \
		# 	 -Dsbtutil.path=../${UTIL_PN}-${UTIL_PV} \
		# 	 -Dsbtlm.path=../${LM_PN}-${LM_PV} \
		# 	 -Dsbtzinc.path=../${Z_PN}-${Z_PV} \
		# 	 publishLocalAllModule || die

		cat <<- EOF | "${WORKDIR}/${B_PN}"/bin/sbt -d -v \
			 -Dsbt.log.noformat=true \
			 -Dsbt.server.autostart=false \
			 -Dsbtio.path=../${IO_PN}-${IO_PV} \
			 -Dsbtutil.path=../${UTIL_PN}-${UTIL_PV} \
			 -Dsbtlm.path=../${LM_PN}-${LM_PV} \
			 -Dsbtzinc.path=../${Z_PN}-${Z_PV} || die
			 publishLocalAllModule
			 exit
		EOF

		# einfo "=== sbt publishLocal with jdk $(java-pkg_get-vm-version) ..."
			# "${WORKDIR}/${SB_P}/sbt" -Dsbt.log.noformat=true publishLocal || die
		einfo "sbt publishLocalAllModule finished"
	fi
}

src_test() {
	sbt_env
	"${S}/${P}" -Dsbt.log.noformat=true test || die
}

src_install() {
	# Place sbt-launch.jar at the end of the CLASSPATH
	java-pkg_dojar $(find "${WORKDIR}"/.ivy2/local -name \*.jar -print | \
						 grep -v sbt-launch.jar) \
				   $(find "${WORKDIR}"/.ivy2/local -name sbt-launch.jar -print)
	local ja="-Dsbt.version=${PV}"
	# disable memory mapping fixes this error when starting sbt:
	# mmap failed for CEN and END part of zip file
	ja+=" -Dsun.zip.disableMemoryMapping=true"
	# disable auto starting the sbt server fixes this error when starting sbt:
	# Exception in thread "sbt-socket-server" java.lang.UnsatisfiedLinkError:
	# /tmp/jna-1469034162/jna4237250668830949412.tmp: /tmp/jna-1469
	ja+=" -Dsbt.server.autostart=false"
	ja+=" -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled"
	java-pkg_dolauncher sbt --jar sbt-launch.jar --java_args "${ja}"
}