#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

$(foreach line,$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/ \
    GNAT_VERSION:=\1 \
  /p;\
  s/^Package: \(libadasockets\([0-9.]\+\)\)$$/ \
    soversion:=\2 \
  /p;\
  s/^Package: \(libadasockets[0-9.]\+-dev\)$$/ \
    DEV_PKG:=\1 \
  /p;\
  ' debian/control),$(eval $(line)))
DPKG_EXPORT_BUILDFLAGS := 1
DEB_BUILD_MAINT_OPTIONS := hardening=+all
DEB_LDFLAGS_MAINT_APPEND := \
  -Wl,--no-allow-shlib-undefined \
  -Wl,--no-copy-dt-needed-entries \
  -Wl,--no-undefined
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/buildopts.mk
include /usr/share/ada/debian_packaging.mk

%:
	dh $@

# Compile Ada and C sources with the same GCC version.
# Libtool finds symbols from libgnat, but not from libgnatrl (#558836).
.PHONY: override_dh_auto_configure
override_dh_auto_configure:
	dh_auto_configure -- \
	  --with-libversioninfo=$(soversion) \
	  --with-adasrcinstalldir=/usr/share/ada/adainclude \
	  --with-aliinstalldir=/usr/lib/$(DEB_HOST_MULTIARCH)/ada/adalib \
	  $(if $(filter nodoc,$(DEB_BUILD_OPTIONS)),--disable-doc) \
	  CC=gnatgcc \
	  GNATMAKE='gnatmake $(GNATMAKEFLAGS)' \
	  LIBS='-lgnarl-$(GNAT_VERSION) -lgnat-$(GNAT_VERSION)'

override_dh_auto_configure: debian/doc-base
debian/doc-base: debian/doc-base_sed
	sed s/@DEV_PKG@/$(DEV_PKG)/ $< > $@

# Adasockets doesn’t need dh-ada-library for the installation phase,
# but it is convenient for changes affecting all Ada libraries.
override_dh_ada_library:
  # Multiple static archives confuse dh-ada-library.
	rm debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libadasockets.a
	dh_ada_library libdir=/usr/lib/$(DEB_HOST_MULTIARCH) debian/fake.gpr
