#!/usr/bin/make -f

export LC_ALL=C.UTF-8

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

JAVA_HOME            := /usr/lib/jvm/default-java/
ENABLE_BUILD_HELP_ARCHS := amd64 i386
DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -n -e 's/+dfsg.*$$//p')

# Providing the hardening CPPFLAGS to the ant cc task.
ARGS_FOR_COMPILER=$(shell COMPILERARGS=""; \
    for arg in $(CPPFLAGS) $(CFLAGS); do\
        COMPILERARGS="$${COMPILERARGS}<compilerarg value=\\\"$$arg\\\" \/> " ;\
    done; \
    echo "$${COMPILERARGS}")

# Providing the hardening LDFLAGS to the ant cc task.
ARGS_FOR_LINKER=$(shell LINKERARGS=""; \
    for arg in $(LDFLAGS); do\
        LINKERARGS="$${LINKERARGS}<linkerarg value=\\\"$$arg\\\" \/> " ;\
    done; \
    echo "$${LINKERARGS}")


%:
	dh $@ --with maven_repo_helper

override_dh_auto_configure:
	# Customizing headers provided by the JDK, to make them fit Gluegen
	debian/customizeJdkHeaders
	mv src/nativewindow/classes/com/jogamp/nativewindow/javafx debian/
	dh_auto_configure
	# Providing the hardening CPPFLAGS and LDFLAGS to the ant cc tasks.
	for sub in jogl nativewindow newt; do\
	    mv make/build-$$sub.xml make/build-$$sub.xml.old;\
	    sed 's!#COMPILER_ARGS#!$(ARGS_FOR_COMPILER)!; s!#LINKER_ARGS#!$(ARGS_FOR_LINKER)!' make/build-$$sub.xml.old > make/build-$$sub.xml;\
	done

# Disable the build of the help on slow archs. It takes forever (a few hours)
execute_before_dh_auto_build-indep:
ifneq (,$(findstring $(DEB_HOST_ARCH),$(ENABLE_BUILD_HELP_ARCHS)))
	ant -Duser.name debian -propertyfile debian/ant.properties -buildfile make/build.xml javadoc.all
else
# Exclude binary doc package from the build. See debhelper manpage.
export DH_OPTIONS += -Nlibjogl2-java-doc
endif

override_dh_auto_build:
	ant -Duser.name debian -propertyfile debian/ant.properties -buildfile make/build.xml

override_mh_install:
	mh_install -plibjogl2-java

override_dh_clean:
	$(RM) -r build build-temp gluegen-build jdkHeaders
	# Restoring the make/build-*.xml files
	for sub in jogl nativewindow newt; do \
	    if [ -e "make/build-$$sub.xml.old" ]; then \
	        mv make/build-$$sub.xml.old make/build-$$sub.xml; \
	    fi; \
	done
	-mv debian/javafx src/nativewindow/classes/com/jogamp/nativewindow/
	dh_clean

get-orig-pom:
	wget https://repo1.maven.org/maven2/org/jogamp/jogl/jogl-all/$(DEB_STRIPPED_UPSTREAM_VERSION)/jogl-all-$(DEB_STRIPPED_UPSTREAM_VERSION).pom -O debian/pom.xml
