#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

%:
	dh $@ --buildsystem=maven

override_dh_auto_clean:
	dh_auto_clean
	@rm -f ./lib/jruby.jar ./lib/jruby-truffle.jar
	@rm -f ./bin/jruby ./core/src/main/java/org/jruby/runtime/Constants.java
	# restore POM files to a pre-patched state if there are backups
	-test -d ./bak && ( cd ./bak \
				&& cp ./pom.xml      ..      \
				&& cp ./core/pom.xml ../core \
				&& cp ./lib/pom.xml  ../lib  )
	@rm -rf ./bak
	-cd ./lib/ruby/stdlib/ && rm -r psych* *openssl* org/ jar* ./rubygems_plugin.rb
	-rm ./core/dependency-reduced-pom.xml ./link* test/jruby/testapp/testapp
	-find . -name pom.xml.save -delete
	-rm -r ./tmp/
	mh_clean

override_dh_auto_configure:
	mkdir -p ./bak/lib ./bak/core
	# save POM files before being patched, otherwise sometimes they are not cleanly
	# restored this prevent twice in a row builds
	cp ./pom.xml ./bak && cp ./core/pom.xml ./bak/core && cp ./lib/pom.xml ./bak/lib
	dh_auto_configure

override_dh_auto_build:
	dh_auto_build
	dh_auto_build -- -Pcomplete
	dh_auto_build -- -Pdist package
	dh_auto_build -- -Pall
	# bundle ruby/psych jar with jruby until jruby-psych is packaged
	mkdir -p tmp/java/psych
	javac -extdirs "/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext"   \
		-target 1.7 -source 1.7 -Xlint:unchecked                 \
		-cp "`./debian/psych-java-ext-build-classpath`"          \
		-d tmp/java/psych `./debian/psych-java-ext-source-files`
	jar cf tmp/java/psych/psych.jar `./debian/psych-java-ext-class-entries`

DIST_TARBALL=./maven/jruby-dist/target/jruby-dist-$(DEB_VERSION_UPSTREAM)-bin.tar.gz
DIST_TARGET_DIR=./target/package/
override_dh_prep:
	dh_prep
	mkdir -p $(DIST_TARGET_DIR)
	tar zxf $(DIST_TARBALL) -C $(DIST_TARGET_DIR) --strip-components=1
	cd $(DIST_TARGET_DIR) && (                                                     \
		mv ./bin/jruby.bash ./bin/jruby                                        \
		&& cp ./bin/jruby ../../bin                                            \
		&& chmod 0755 ./bin/*                                                  \
		&& mkdir -p ./lib/jni/                                                 \
		&& find ./lib/ruby/truffle -name LICENSE -delete                       \
	        && cp -a /usr/lib/ruby/2.3.0/psych* ./lib/ruby/stdlib/                 \
	        && cp -a ../../tmp/java/psych/psych.jar ./lib/ruby/stdlib/             \
	        && sed 's/1\.[0-9]\{1,2\}/1.x/' -i ./lib/ruby/stdlib/psych/versions.rb \
		&& sed '1d' -i ./lib/ruby/truffle/jruby+truffle/bin/jruby+truffle      \
		&& rm -r ./tool/nailgun/*                                              )

override_dh_auto_install:
	dh_auto_install
	mh_installpom -pjruby ./maven/pom.xml
	mh_installpom -pjruby ./maven/jruby-complete/pom.xml
	mh_installjar -pjruby ./maven/jruby-complete/pom.xml \
		./maven/jruby-complete/target/jruby-complete-$(DEB_VERSION_UPSTREAM).jar
	mh_installpom -pjruby ./maven/jruby/pom.xml
	mh_installjar -pjruby ./maven/jruby/pom.xml \
		./maven/jruby/target/jruby-$(DEB_VERSION_UPSTREAM).jar

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	cp -a /usr/lib/ruby/2.3.0/psych* ./lib/ruby/stdlib/
	# to-do: package psych java extension
	-dh_auto_build -- package -B -Dinvoker.skip=false -Prake -Dtask=test:mri
	# these tests will be enabled once the following gems are packaged:
        # jar-dependencies and recent jruby-openssl (>= 0.9.17)
#	-dh_auto_build -- package -B -Dinvoker.skip=false -Prake -Dtask=test:jruby
	dh_auto_build -- package -B -Dinvoker.skip=false -Prake -Dtask=spec:jruby
endif

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