#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1
export JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8 # otherwise build fails in pbuilder

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

# Referencing the Maven coordinates of the dependencies.
NAGA_COORDINATES = <dependency>\n<groupId>com.google.code</groupId>\n<artifactId>naga</artifactId>\n<version>debian</version>\n</dependency>
CLI_COORDINATES = <dependency>\n<groupId>commons-cli</groupId>\n<artifactId>commons-cli</artifactId>\n<version>debian</version>\n</dependency>
INCHI_COORDINATES = <dependency>\n<groupId>net.sf.jni-inchi</groupId>\n<artifactId>jni-inchi</artifactId>\n<version>debian</version>\n</dependency>

%:
	dh $@ --with javahelper,maven_repo_helper

override_dh_auto_clean:
	mkdir --parents build
	quilt push -a || true
	dh_auto_clean
	quilt pop -a || true
	set -e; for i in Jmol/tools; do \
	  test ! -e $$i || find $$i -type l -delete; \
	done
	rm -rf build
	rm -f src/org/jmol/translation/translations.tgz
	if [ -e tools/pom.xml ]; then rm tools/pom.xml; fi

override_dh_install:
	dh_install
	# Using imagemagick to resize the icon
	mkdir -p debian/jmol/usr/share/icons/hicolor/96x96/apps/
	convert src/org/openscience/jmol/app/images/icon.png -resize 96x96\! debian/jmol/usr/share/icons/hicolor/96x96/apps/jmol-icon.png

override_dh_installdocs:
	# Run javadoc target manually
	# don't know why, but in June 2022, the package was built *without*
	# applying debian patches, which results in FTBFS by the debian build
	# farm. So, I must enforce the application and removal of patches.
	quilt push -a || true
	ant -Duser.name debian javadoc
	quilt pop -a || true
	mkdir -p debian/libjmol-java-doc/usr/share/doc/libjmol-java/api
	cp -a build/javadoc/* debian/libjmol-java-doc/usr/share/doc/libjmol-java/api
	dh_installdocs

override_dh_auto_build-indep:
	# don't know why, but in June 2022, the package was built *without*
	# applying debian patches, which results in FTBFS by the debian build
	# farm. So, I must enforce the application and removal of patches.
	quilt push -a || true
	dh_auto_build -i
	quilt pop -a || true

execute_before_mh_install:
	# Putting the Maven coordinates of the dependencies in the upstream template
	# pom file. Also putting the dfsg-stripped upstream version number in the pom.
	sed -e 's,\(<dependencies>\),\1\n$(NAGA_COORDINATES)\n$(CLI_COORDINATES)\n$(INCHI_COORDINATES),' \
	    -e 's/\(@version@\)/\1$(DEB_VERSION_UPSTREAM)/; s/@version@\(.*\)+dfsg[0-9]*/\1/' \
	    tools/pom.xml.template > tools/pom.xml
