#!/usr/bin/make -f
# Debian build script for XML EZ OUT - Copyright (c) 2009-2014 Xavier Grave

DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging*.mk

ADAFLAGS += -gnat05 -gnatf -gnaty -gnatwa -gnatwe

DEB_DATE := $(shell dpkg-parsechangelog -S date)

soversion := $(shell sed -nr '/^Package: libxmlezout([^-]+)$$/{s//\1/;p;q}' debian/control)
soname := libxmlezout.so.$(soversion)

POLICY_TARGETS := binary binary-arch binary-indep build build-arch build-indep install clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh $@ --with ada-library

# Ignore all targets from upstream GNUmakefile.
.PHONY: $(addprefix override_dh_auto_,build install clean)

override_dh_auto_build:
 # Ensure deterministic timestamps in ALI files for reproducible builds.
	find . -depth \( -name "*.ad[bs]" \) \
	  -a -newermt '$(DEB_DATE)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE)'
	gprbuild $(BUILDER_OPTIONS) debian/build_xmlezout.gpr -Xkind=static \
	  $(foreach f,ADAFLAGS,"-X$(f)=$($(f))")
	gprbuild $(BUILDER_OPTIONS) debian/build_xmlezout.gpr -Xkind=dynamic \
	  $(foreach f,ADAFLAGS LDFLAGS soname,"-X$(f)=$($(f))")

override_dh_auto_clean:
	rm -f $(foreach d, lib obj, \
	      $(foreach k, dynamic static, \
	      debian/build_$(d)_$(k)/*))

mtnversion := t:libxmlezout-1.06.1
mtnbase := base_debian.db
branch := com.mckae.xml_ez_out
mtn_uri := mtn://www.ada-france.org?$(branch)
orig_dir := libxmlezout.orig
orig_tgz := libxmlezout.orig.tar.gz
pkg_orig_dir := libxmlezout-$(DEB_VERSION_UPSTREAM).orig
pkg_orig_tgz := libxmlezout_$(DEB_VERSION_UPSTREAM).orig.tar.gz

.PHONY: get-orig-source
get-orig-source:
	TMPDIR=$$(mktemp --tmpdir --directory libxmlezout-get-orig-source-XXXXXXXXXX) && \
	if [ -e ../$(mtnbase) ]; then \
                cp ../$(mtnbase) $$TMPDIR; \
        fi; \
	mtn clone -d $$TMPDIR/$(mtnbase) $(mtn_uri) -k "" $$TMPDIR/$(orig_dir) && \
	(cd $$TMPDIR && tar --create --gzip --exclude="_MTN" $(orig_dir)) > $(orig_tgz) ; \
	rm -rf $$TMPDIR
	@echo "The file $(orig_tgz) contains the latest Debian snapshot of upstream sources."

.PHONY: get-pkg-source
get-pkg-source:
	TMPDIR=$$(mktemp --tmpdir --directory libxmlezout-get-pkg-source-XXXXXXXXXX) && \
	if [ -e ../$(mtnbase) ]; then \
                cp ../$(mtnbase) $$TMPDIR; \
        fi; \
	mtn clone -r $(mtnversion) -d $$TMPDIR/$(mtnbase) $(mtn_uri) -k "" $$TMPDIR/$(pkg_orig_dir) && \
	(cd $$TMPDIR && tar --create --gzip --exclude="_MTN" $(pkg_orig_dir)) > $(pkg_orig_tgz) ; \
	rm -rf $$TMPDIR
	@echo "The file $(pkg_orig_tgz) contains the latest Debian snapshot of upstream sources."
