#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

# If the user has not explicitly set JAVA_HOME, export sensible value
# to make sure that the correct compiler is used by ant.
# * prefer openjdk-6 then sun-java6;
# * include both multi-arch and non-multi-arch paths for ease of backporting
#   "export JAVA_HOME=/usr/lib/jvm/java-6-openjdk-i386" or similar if you want
#   to build locally using openjdk-7.
JAVA_HOME ?= $(shell echo \
  ` for jre in \
      /usr/lib/jvm/default-java \
      /usr/lib/jvm/java-7-openjdk-\`dpkg-architecture -qDEB_BUILD_ARCH\` \
      /usr/lib/jvm/java-6-openjdk-\`dpkg-architecture -qDEB_BUILD_ARCH\` \
      /usr/lib/jvm/java-6-openjdk \
      ; do \
      test -d $$jre && test -f $$jre/bin/javac && echo $$jre && break ; \
    done ` \
  )
export JAVA_HOME

%:
	dh $@

override_dh_auto_build:
	ant -Dlib.dir=/usr/share/java compile
	ant -Dlib.dir=/usr/share/java jar
	convert -geometry 32x32 res/LaTeXDrawIcon.png out/latexdraw32.xpm
	convert -geometry 16x16 res/LaTeXDrawIcon.png out/latexdraw16.xpm

override_dh_auto_clean:
	rm -f build-stamp
	find . -name \*.jar -exec rm -f {} \;
	find . -name \*.class -exec rm -f {} \;
	rm -rf out/ api/
	dh_clean

# A target for building the API docs with javadoc is provided here
# but is not actually used in the Debian package. To successfully
# build the API documentation, the packages
#         libjiu-java-doc, libjlibeps-java-doc
# must be installed too.
apidocs: build
	ant -Dlib.dir=/usr/share/java doc

override_dh_installchangelogs:
	dh_installchangelogs -k release_note.txt

# Be explicit given that there is plenty of confusion about what this target means
get-orig-source: get-latest-source

get-latest-source:
	cd $(dir $_).. && \
	uscan=$$(uscan --report --dehs --upstream-version 0) && \
	uver=$$(echo "$$uscan" | sed -n 's,<upstream-version>\(.*\)</.*>,\1,p') && \
	uurl=$$(echo "$$uscan" | sed -n 's,<upstream-url>\(.*\)</.*>,\1,p') && \
	echo debian/repackage --upstream-version "$${uver}" "$${uurl}" $(CURDIR) &&\
	sh debian/repackage --upstream-version "$${uver}" "$${uurl}" $(CURDIR)

.PHONY: get-orig-source get-latest-source
