#!/usr/bin/make -f

export SBTHOME=$(CURDIR)/debian/sbt_home
export HOME=$(SBTHOME)
export SBTCMD=$(CURDIR)/bootstrapsbt/bin/sbt -verbose -debug -sbt-dir $(SBTHOME)/.sbt -ivy $(SBTHOME)/.ivy2

PKD    = $(abspath $(dir $(MAKEFILE_LIST)))
SRCPKG = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
BINPKG = lib$(SRCPKG)-java
PKGVER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog -SVersion | cut -d- -f1)
SBTVER ?= $(shell grep sbt.version= $(CURDIR)/project/build.properties | sed 's/.*=//')
URL    = https://dl.bintray.com/sbt/native-packages/sbt/$(SBTVER)/sbt-$(SBTVER).tgz

PKG_ORIG = $(CURDIR)/../$(SRCPKG)_$(PKGVER).orig.tar.gz
BOOTSTRAP_SBT = $(CURDIR)/../$(SRCPKG)_$(PKGVER).orig-bootstrapsbt.tar.xz
BOOTSTRAP_DEPS = $(CURDIR)/../$(SRCPKG)_$(PKGVER).orig-bootstrapdeps.tar.xz
BOOTSTRAP_DEPS_SOURCES = $(CURDIR)/../$(SRCPKG)_$(PKGVER).orig-bootstrapdeps-sources.tar.xz

%:
	dh $@ --with javahelper --with maven_repo_helper

override_dh_auto_build:
	git init
	mkdir -p $(SBTHOME)/.ivy2/cache
	cp -fr $(CURDIR)/bootstrapdeps/* $(SBTHOME)/.ivy2/cache/
	mkdir $(SBTHOME)/.sbt
	echo "[repositories]" > $(SBTHOME)/.sbt/repositories
	echo "local" >> $(SBTHOME)/.sbt/repositories
	cp -frv $(CURDIR)/bootstrapdeps-sources/. $(SBTHOME)/.ivy2/cache/
	$(SBTCMD) 'set offline := true' -Dsbt.override.build.repos=true packageBin make-pom
	find ./ -name '*.pom'|grep target|sed 's/\(.*\).pom/\1.pom --package=$(BINPKG) --has-package-version --java-lib --artifact=\1.jar/' > $(CURDIR)/debian/$(BINPKG).poms

override_dh_clean:
	rm -fr .git $(SBTHOME) project/project $(shell find ./ -name target -type d) debian/$(BINPKG).poms
	dh_clean

.PHONY: get-orig-source

get-orig-source: $(PKG_ORIG) $(BOOTSTRAP_DEPS) $(BOOTSTRAP_DEPS_SOURCES)

$(PKG_ORIG):
	@echo
	@echo "Downloading latest $(SRCPKG)-$(PKGVER)"
	uscan --noconf --verbose --rename --check-dirname-level=0 --download-version $(PKGVER) $(PKD)

$(BOOTSTRAP_SBT):
	@echo
	@echo "Fetching sbt binary for bootstrap"
	wget -q $(URL)
	rm -fr bootstrapsbt
	mkdir bootstrapsbt
	tar -C bootstrapsbt --strip-components=1 -zxf sbt-$(SBTVER).tgz
	tar -JcC bootstrapsbt -f $(BOOTSTRAP_SBT) .
	rm -fr sbt-$(SBTVER).tgz

$(BOOTSTRAP_DEPS) $(BOOTSTRAP_DEPS_SOURCES): $(BOOTSTRAP_SBT)
	@echo
	@echo "Fetching online sbt project build dependancies"
	rm -fr bootstrapsbt bootstrapdeps-sources
	mkdir bootstrapsbt
	tar -Jx -C bootstrapsbt -f $(BOOTSTRAP_SBT)
	mkdir -p $(SBTHOME)/.ivy2/cache
	mkdir $(SBTHOME)/.sbt
	git init
	$(SBTCMD) update
	#$(SBTCMD) updateClassifiers
	# "update" compiles compiler-interface jar with the scala defined by
	# the project, so we remove it and store all jar srcs into a dedicated
	# debian source component that will be copied before compiling the
	# whole project, including the compiler-interface jar
	# After bootstrap, we intend to provide this compiler-interface, built
	# with debian's scala
	rm -fr $(SBTHOME)/.ivy2/cache/org.scala-sbt/org.scala-sbt-compiler-interface-*
	# .properties and .xml.original will be regenerated and that will avoid unnecessary debian/copyright work
	find $(SBTHOME)/.ivy2/cache \( -name '*.properties' -o -name '*.xml.original' \) -delete
	# remove path with no jar file
	find $(SBTHOME)/.ivy2/cache/ -name '*.xml' |while read DIR; do \
		if [ ! -d $${DIR%/*}/jars -a ! -d $${DIR%/*}/bundles -a ! -d $${DIR%/*}/orbits ]; then rm -fr $${DIR%/*}; fi done
	cd $(SBTHOME)/.ivy2/cache ; find . -type d -name 'srcs'| while read DIR; do \
		mkdir -p $(CURDIR)/bootstrapdeps-sources/$$DIR ; \
		mv $$DIR/* $(CURDIR)/bootstrapdeps-sources/$$DIR ; \
		rm -fr $$DIR ; \
		done
	tar -JcC $(SBTHOME)/.ivy2/cache/ -f $(BOOTSTRAP_DEPS) .
	tar -Jc -f $(BOOTSTRAP_DEPS_SOURCES) bootstrapdeps-sources
