#!/usr/bin/make -f
#
# Debian Makefile for AUnit
# Copyright (c) 2009      Stephen Leake <stephen_leake@stephe-leake.org>
# Copyright (c) 2004-2006 Ludovic Brenta <ludovic@ludovic-brenta.org>
# Copyright (c) 2013-2015 Nicolas Boulenguez <nicolas@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk

DEB_DATE_RFC_2822 := $(shell dpkg-parsechangelog -S date)
DEB_DATE_RFC_3339 := $(shell date "+%F %T" -d "$(DEB_DATE_RFC_2822)")

ADAFLAGS += -gnatfno -gnatwa -gnatVa

LIB_NAME := $(patsubst lib%,%,$(DEB_SOURCE))

SOVERSION := $(shell sed debian/control -ne \
  '/^Package: $(DEB_SOURCE)\([0-9.]\+\)$$/ { s//\1/; p; q}')
ifndef SOVERSION
  $(warning Could not guess SOVERSION from debian/control)
  # Not error. Policy defines targets that may be run from outside build dir.
endif

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

# Ignore upstream Makefile/configure.
.PHONY: $(addprefix override_dh_auto_,configure build-arch build-indep test install clean)

PROJECT := lib/gnat/aunit_build.gpr
override_dh_auto_build-arch:
 # Touch generated sources for deterministic ALI files and tar headers.
	find . -depth -newermt '$(DEB_DATE_RFC_3339)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE_RFC_3339)'
	$(MAKE) INSTALL=false \
	  GPRBUILD='gnatmake $(BUILDER_OPTIONS) -XLIBRARY_KIND=static $(foreach var,ADAFLAGS,"-X$(var)=$($(var))")'
	$(MAKE) INSTALL=false \
	  GPRBUILD='gnatmake $(BUILDER_OPTIONS) -XLIBRARY_KIND=dynamic $(foreach var,ADAFLAGS LDFLAGS SOVERSION,"-X$(var)=$($(var))")'

override_dh_auto_build-indep: version_information.backup
 # Freeze the clock for deterministic PDF timestamps.
	faketime -f "$(DEB_DATE_RFC_3339)" \
	$(MAKE) -C docs
version_information.backup: %.backup: %
	mv $* $@
	echo MAJOR_VERSION=$(DEB_VERSION_UPSTREAM) \
	  | sed 's/\./.\nMINOR_VERSION=/' > $*

# Currently fails.
# override_dh_auto_test:
# 	$(MAKE) -C test 'GPRBUILD=gnatmake $(BUILDER_OPTIONS) $(foreach var,ADAFLAGS LDFLAGS,"-X$(var)=$($(var))")'

override_dh_auto_clean::
	dh_auto_clean
	if test -e version_information.backup; then \
	  mv version_information.backup version_information; \
	fi
	$(MAKE) -C test GPRCLEAN=gnatclean clean

# The order in this list matters for rdfind.
EXAMPLES := simple_test test_caller test_fixture calculator failures liskov
INSTALLED_EXAMPLES := $(addprefix debian/libaunit-doc/usr/share/doc/libaunit-doc/examples/,$(EXAMPLES))
.PHONY: override_dh_installexamples-indep
override_dh_installexamples-indep:
	dh_installexamples --indep
  # Gnatmake will always be available, not gprbuild.
  # -eS allows testing these examples in run-time tests.
	sed -i 's/gprbuild/gnatmake -eS/;s/gprclean/gnatclean/' $(addsuffix /Makefile,$(INSTALLED_EXAMPLES))
  # Replace duplicate files with symlinks.
	rdfind -outputname /dev/null -makesymlinks true $(INSTALLED_EXAMPLES)
  # symlinks to make them relative (only if -indep build).
	symlinks -r -s -c $(INSTALLED_EXAMPLES)

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=libaunit-doc --exclude=usr/share/doc/libaunit-doc/examples/
# Check calculator/tests/stack-test.adb.gz
	dh_compress --remaining-packages
