#!/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-2017 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/>.
#

# To debug, replace eval with error.
define new_line :=


endef
$(eval $(addsuffix $(new_line),$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/ \
    GNAT_VERSION:=\1 \
  /p;\
  s/^Package: lib[a-z-]\+\([0-9.]\+\)$$/ \
    SOVERSION:=\1 \
  /p;\
  s/^Package: \(lib[a-z-]\+[0-9.]\+-dev\)$$/ \
    DEV_PKG:=\1 \
  /p;\
  ' debian/control)))

DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging-$(GNAT_VERSION).mk
ADAFLAGS += -gnatfno -gnatwa -gnatVa

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

%:
	dh $@ --with ada-library

######################################################################
# Ignore upstream Makefile/configure.
.PHONY: $(addprefix override_dh_auto_,configure build test install)
# clean target is OK.

override_dh_auto_build:
	$(MAKE) \
	  GPRBUILD='gprbuild $(BUILDER_OPTIONS) -XLIBRARY_KIND=static "-XADAFLAGS=$(ADAFLAGS) -fPIE" "-XSOVERSION=$(SOVERSION)"'

	$(MAKE) \
	  GPRBUILD='gprbuild $(BUILDER_OPTIONS) -XLIBRARY_KIND=dynamic $(foreach var,ADAFLAGS LDFLAGS SOVERSION,"-X$(var)=$($(var))")'

######################################################################
# The order in this list matters for rdfind.
INSTALLED_EXAMPLES := \
  $(subst examples/,debian/$(DEV_PKG)/usr/share/doc/$(DEV_PKG)/examples/, \
    $(wildcard examples/*))

.PHONY: override_dh_installexamples
override_dh_installexamples:
	dh_installexamples --all
  # 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=$(DEV_PKG) --exclude=usr/share/doc/$(DEV_PKG)/examples/
# Check calculator/tests/stack-test.adb.gz
	dh_compress --remaining-packages
