#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

#JAVA_HOME := /usr/lib/jvm/default-java
# upstream code directly from git repo builds with openjdk-7-jdk
# I have to find out how/why
JAVA_HOME := /usr/lib/jvm/java-8-openjdk-$(DEB_BUILD_ARCH)
export LC_ALL=en_US.UTF-8

include $(CURDIR)/debian/maven3.mk

%:
	dh $@

override_dh_auto_clean:
	-$(DEB_MAVEN_INVOKE) -Pdist clean
	dh_clean
	mh_unpatchpoms -pjruby
	mh_clean
	rm -f lib/jruby.jar bin/jruby
	for subp in stdlib dist complete; do \
		rm -rf ./maven/jruby-$$subp/target ; \
	done
	rm -rf ./test/target/
	rm -rf -- ./rubyspec_temp/
	rm -rf -- "$(DEB_MAVEN_REPO)"

	# this ugly hack is due to mh_unpatchpoms not restoring some POMs
	# if you want to build jruby twice in a row, you need to restore
	# everything correctly
	-cd maven.bak && for pom in `find ./jruby-* -type f`; do \
	  cp $$pom $(CURDIR)/maven/$$pom ; \
	done
	-cd maven.bak && cp ./test/pom.xml $(CURDIR)/test/pom.xml
	rm -rf maven.bak

override_dh_auto_configure:
	# this ugly hack is due to mh_unpatchpoms not restoring some POMs
	# if you want to build jruby twice in a row, you need to restore
	# everything correctly
	mkdir -p maven.bak
	cd maven.bak && mkdir -p jruby-complete jruby-stdlib test
	cp maven/jruby-complete/pom.xml maven.bak/jruby-complete
	cp maven/jruby-stdlib/pom.xml maven.bak/jruby-stdlib
	cp test/pom.xml maven.bak/test

	/usr/share/maven-debian-helper/copy-repo.sh "$(CURDIR)/debian"
	mh_patchpoms -pjruby --debian-build --keep-pom-version \
	  --maven-repo="$(DEB_MAVEN_REPO)" --build-no-docs

override_dh_auto_build:
	mkdir -p lib/jni
	mkdir -p $(CURDIR)/maven/jruby-complete/target/classes
	$(DEB_MAVEN_INVOKE) -Pcomplete
	$(DEB_MAVEN_INVOKE) -Pdist package

# this got ugly fast
# there must be a better way to do this
override_dh_prep:
	dh_prep
	mkdir -p target/package
	tar zxf ./maven/jruby-dist/target/jruby-dist-$(DEB_VERSION_UPSTREAM)-bin.tar.gz \
	  -C target/package --strip-components=1
	cd target/package/bin && mv jruby.bash jruby
	cd target/package/bin && chmod 0755 *
	rm target/package/lib/ruby/2.0/rdoc/generator/template/darkfish/js/jquery.js
	mkdir -p target/package/lib/ruby/gems/shared/
	cd target/package/lib/ruby/gems/shared/ && mkdir -p cache gems specifications
	rm -rf target/package/tool/nailgun/*
	for platform in i386-Linux x86_64-Linux arm-Linux; do \
		mkdir -p target/package/lib/jni/$$platform ; \
	done

override_dh_auto_install:
	mh_installpom -pjruby --no-parent core/pom.xml
	mh_installjar -pjruby --java-lib  core/pom.xml core/target/jruby-core-$(DEB_VERSION_UPSTREAM).jar
	for subp in noasm stdlib complete; do \
		mh_installpom -pjruby --no-parent maven/jruby-$$subp/pom.xml ; \
	done
	mh_installjar -pjruby maven/jruby-stdlib/pom.xml maven/jruby-stdlib/target/jruby-stdlib-$(DEB_VERSION_UPSTREAM).jar
	mh_installjar -pjruby maven/jruby-complete/pom.xml maven/jruby-complete/target/jruby-complete-$(DEB_VERSION_UPSTREAM).jar

override_dh_install:
	dh_install
	dh_install core/target/jruby-core-$(DEB_VERSION_UPSTREAM)-noasm.jar \
		usr/share/maven-repo/org/jruby/jruby-core/$(DEB_VERSION_UPSTREAM)

override_dh_link:
	dh_link
	dh_link usr/share/maven-repo/org/jruby/jruby-core/$(DEB_VERSION_UPSTREAM)/jruby-core-$(DEB_VERSION_UPSTREAM)-noasm.jar \
		usr/share/maven-repo/org/jruby/jruby-core/debian/jruby-core-debian-noasm.jar

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	# setup links to jruby-openssl library, so tests relying on that can work
	for file in /usr/share/jruby/lib/ruby/shared/*ssl* \
	/usr/share/jruby/lib/ruby/shared/*org*; do \
		ln -s $$file ./lib/ruby/shared ; \
	done

	$(DEB_MAVEN_INVOKE) -Ptest test
	./bin/jruby spec/mspec/bin/mspec ci
	# enable it when minitest-excludes is available in the archive
	#./bin/jruby -S ./bin/rake test:mri19
	#./bin/jruby -S ./bin/rake test:extended

	# remove links to jruby-openssl library
	for file in ./lib/ruby/shared/*ssl* ./lib/ruby/shared/*org*; do \
		unlink $$file ; \
	done
endif

get-orig-source:
	uscan --verbose --download-current-version
