#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

JRUBY_VERSION=$(shell grep -Po "JRUBY_VERSION=\K.*" bin/.jruby.release)

%:
	dh $@ --buildsystem=maven

execute_after_dh_auto_clean:
	rm -f debian/jruby.lintian-overrides
	rm -rf .pc_gems
	# use git and .gitignore to fully clean build tree
	# upstream .gitignore needs a bit of work
	git --git-dir=debian/git-tmp init
	git --git-dir=debian/git-tmp --work-tree=. clean -Xdf \
		-e '!*.classpath' \
		-e '!*.project' \
		-e '!*.settings' \
		-e '!bin/.jruby.release' \
		-e '!bin/ruby.bat' \
		-e '!lib/ruby/stdlib/rubygems/' \
		-e 'lib/ruby/stdlib/rubygems/*' \
		-e '!lib/ruby/stdlib/rubygems/defaults/' \
	    -e '!lib/ruby/stdlib/unicode_normalize/' \
	    -e '!samples/jnlp/build.properties' \
		-e '!test/jruby/testapp/Makefile'
	rm -rf debian/git-tmp
	# restore pom.xml files to original versions
	-$(foreach p,. lib core shaded,test -f ${p}/pom.xml.bak && mv ${p}/pom.xml.bak ${p}/pom.xml;)
	# remove leftover pom
	rm -f maven/jruby/pom.xml

execute_after_dh_auto_configure:
	# move stdlib rubygems into maven repository
	mv rubygems debian/maven-repo
	# backup upstream generated poms
	$(foreach p,. lib core shaded,cp ${p}/pom.xml ${p}/pom.xml.bak;)

override_dh_auto_build:
	# ensure org.jruby.maven in local maven repo is writeable
	# this is needed so that a given version of jruby can build itself
	mkdir debian/maven-repo/org/jruby.tmp
	cp -L -R debian/maven-repo/org/jruby/* debian/maven-repo/org/jruby.tmp/
	rm -rf debian/maven-repo/org/jruby
	mv debian/maven-repo/org/jruby.tmp debian/maven-repo/org/jruby
	# build selected profiles
	dh_auto_build -- -Djruby.revision=$(DEB_VERSION) -Pmain,complete

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	# java tests
	# to debug a single test, add -Dsurefire.useFile=false -Dtest=org/jruby/test/<TestFileName>.java
	dh_auto_test -- -Djruby.revision=$(DEB_VERSION) -Ptest -pl org.jruby:jruby-base
endif

override_dh_auto_install:
	# don't ship jruby-stdlib maven artifact
	dh_auto_install --destdir=debian/jruby/ -O--buildsystem=maven -- -pl "!lib"

execute_after_dh_install:
	# clean and install polyglot-generated poms
	for p in maven/pom.xml maven/jruby/pom.xml; do \
		mh_cleanpom --keep-pom-version -pjruby $$p; \
		mh_installpom -pjruby $$p; \
	done
	# install org.jruby:jruby proxy jar
	mh_installjar -pjruby ./maven/jruby/pom.xml \
	        ./maven/jruby/target/jruby-$(JRUBY_VERSION).jar
	# apply Class-Path attribute to main jar
	jh_classpath

execute_after_dh_installexamples:
	# remove vcs control file
	rm debian/jruby/usr/share/doc/jruby/examples/jnlp/.gitignore

JRUBY_HOME_DIR=lib/target/classes/META-INF/jruby.home
execute_after_dh_prep:
	# apply patches to bundled stdlib gems
	QUILT_PATCHES=debian/patches/gems QUILT_PC=.pc_gems quilt push -a
	# remove superfluous shell scripts
	rm -f $(JRUBY_HOME_DIR)/bin/*.bat
	rm -f $(JRUBY_HOME_DIR)/bin/*.sh
	rm -f $(JRUBY_HOME_DIR)/bin/jirb*
	# remove env from shell scripts
	sed -i '1s|^#!/usr/bin/env\sruby|#!/usr/bin/jruby|' $(JRUBY_HOME_DIR)/bin/*
	# mark all scripts executable
	chmod +x $(JRUBY_HOME_DIR)/bin/*
	# remove bundled libfixposix libraries
	find $(JRUBY_HOME_DIR)/lib/ruby/stdlib/libfixposix/binary -type f -and -not -name version.rb -delete
	find $(JRUBY_HOME_DIR)/lib/ruby/stdlib/libfixposix/binary -type d -and -empty -delete
	# remove bundled font files from stdlib (rdoc templates)
	# only remove the fonts that debian provides in packages
	find $(JRUBY_HOME_DIR)/lib/ruby/stdlib -name Lato-\*.ttf -type f -print -delete
	# remove .jrubydir files (only needed in jars)
	find $(JRUBY_HOME_DIR)/lib -name .jrubydir -type f -delete
	# remove bundled jars available in debian
	rm -rf $(JRUBY_HOME_DIR)/lib/ruby/stdlib/jline
	rm -rf $(JRUBY_HOME_DIR)/lib/ruby/stdlib/org/bouncycastle
	rmdir $(JRUBY_HOME_DIR)/lib/ruby/stdlib/org
	# remove unneeded executable bits from gem.home scripts
	find $(JRUBY_HOME_DIR)/lib/ruby/gems/shared/gems/* -type f -executable \
		| while read script; do \
			sed -i '1,1{/^#\!\/usr\/bin\/env ruby/d}' $$script; \
			chmod -x $$script; \
		done

execute_before_dh_lintian:
	sed 's,@DEB_VERSION_UPSTREAM@,$(JRUBY_VERSION),g' \
		debian/jruby.lintian-overrides.in > debian/jruby.lintian-overrides
