#!/usr/bin/make -f

# Copyright (c) 2003-2014 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2014-2018 Nicolas Boulenguez <nicolas@debian.org>

# This build script 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/>.

# On Debian systems, the full text of the GPL is in the file
# /usr/share/common-licenses/GPL-3.

######################
# Set some variables #
######################

$(foreach line,$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/ \
    GNAT_VERSION:=\1 \
  /p;\
  s/^Package: libflorist\([0-9.]\+\)$$/ \
    soversion:=\1 \
  /p;\
  ' debian/control),$(eval $(line)))

DEB_CFLAGS_MAINT_APPEND := -w
DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging-$(GNAT_VERSION).mk

ADAFLAGS += -gnatVa -gnatafno -gnatwa

%:
	dh $@ --with ada-library

# Disable autoheader, upstream maintains the macros manually.
.PHONY: override_dh_autoreconf
override_dh_autoreconf:
	AUTOHEADER=true dh_autoreconf

.PHONY: $(addprefix override_dh_auto_, configure build test install clean)

override_dh_auto_configure:
	dh_auto_configure -- $(foreach v, \
	  ADAFLAGS CC CFLAGS CPPFLAGS LDFLAGS \
	  ,"$(v)=$($(v))")

GPR_CONFIG := default.cgpr
override_dh_auto_build: $(GPR_CONFIG)
$(GPR_CONFIG):
	gprconfig --batch --config=Ada --config=C,,,,gnatgcc
override_dh_auto_clean::
	rm -f $(GPR_CONFIG)

override_dh_auto_build:
	$(MAKE) LIBRARY_TYPE_OPTION=-XLIBRARY_TYPE=static \
	  "GPRBUILD_FLAGS=$(BUILDER_OPTIONS) -XSOVERSION=$(soversion)"
	$(MAKE) LIBRARY_TYPE_OPTION=-XLIBRARY_TYPE=relocatable \
	  "GPRBUILD_FLAGS=$(BUILDER_OPTIONS) -XSOVERSION=$(soversion)"
override_dh_auto_clean::
	dh_auto_clean

.PHONY: override_dh_ada_library
override_dh_ada_library:
	dh_ada_library LIBRARY_TYPE=relocatable \
	  SOVERSION=$(soversion) florist.gpr