#!/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.]\+\)\)$$/ \
    LIB_PKG:=\1 \
    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/ada/debian_packaging-$(GNAT_VERSION).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 $(BUILDER_OPTIONS)' \
	  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)/ $< > $@

# libadasocketsSOVERSION.install would be more readable, but we do not
# want to rename the file on every SOVERSION bump.
.PHONY: override_dh_install
override_dh_install:
	dh_install -p$(LIB_PKG) usr/lib/$(DEB_HOST_MULTIARCH)/libadasockets.so.*
	dh_install --remaining-packages
