#!/usr/bin/make -f

$(foreach line,$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/ \
    gnat_version:=\1 \
  /p;\
  s/^Package: libdbusada\([0-9.]\+\)$$/ \
    VERSION:=\1 \
  /p;\
  s/^Package: \(libdbusada[0-9.]\+-dev\)$$/ \
    dev_pkg:=\1 \
  /p;\
  ' debian/control),$(eval $(line)))

DEB_BUILD_MAINT_OPTIONS := hardening=+all
DEB_LDFLAGS_MAINT_APPEND := -Wl,--as-needed -Wl,--no-undefined
include /usr/share/dpkg/buildflags.mk
include /usr/share/ada/debian_packaging-$(gnat_version).mk

make_vars := \
  GNAT_BUILDER_FLAGS='$(BUILDER_OPTIONS)' \
  $(foreach v,ADAFLAGS LDFLAGS VERSION,$(v)='$($(v))')

%:
	dh $@ --with ada-library

.PHONY: override_dh_auto_build
override_dh_auto_build:
	if [ ! -e thin.bak ]; then \
	  mv thin thin.bak && \
	  sh scripts/generate-thin-binding.sh; \
	fi
	dh_auto_build -- $(make_vars)
	dh_auto_build -- LIBRARY_KIND=static $(make_vars)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_auto_build -- doc $(make_vars)
endif

.PHONY: override_dh_auto_test
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) tests $(make_vars)
endif

.PHONY: override_dh_auto_install
# Empty.

.PHONY: override_dh_install
override_dh_install:
	dh_install -p$(dev_pkg) debian/dbus_ada.gpr usr/share/gpr
	dh_install --remaining-packages

.PHONY: override_dh_clean
override_dh_clean:
	dh_clean debian/$(dev_pkg).doc-base
	if [ -e thin.bak ]; then \
	  rm -r thin && \
	  mv thin.bak thin; \
	fi

.PHONY: override_dh_installdocs
override_dh_installdocs: debian/$(dev_pkg).doc-base
	dh_installdocs -p$(dev_pkg) doc/html/*
	dh_installdocs --remaining-packages

debian/$(dev_pkg).doc-base: debian/template_libdbusada-dev.doc-base
	sed s/@dev_pkg@/$(dev_pkg)/ $< > $@

.PHONY: override_dh_installexamples
override_dh_installexamples:
	dh_installexamples -p$(dev_pkg) examples/*
	dh_installexamples --remaining-packages

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress -p$(dev_pkg) -X.ads -X.adb -XMakefile
	dh_compress --remaining-packages
