#!/usr/bin/make -f

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

DEB_BUILD_MAINT_OPTIONS := hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/ada/debian_packaging-$(gnat_version).mk

export GNAT_BUILDER_FLAGS = $(BUILDER_OPTIONS)

%:
	dh ${@}

# Force regeneration of the thin binding included in the tarball.
.PHONY: override_dh_auto_configure
override_dh_auto_configure:
	rm -fr thin
	$(MAKE) thin CXX=g++-$(gnat_version)

.PHONY: override_dh_auto_build
override_dh_auto_build:
	$(MAKE)
	$(MAKE) LIBRARY_KIND=static
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) doc
endif

.PHONY: override_dh_auto_install
override_dh_auto_install:
	$(MAKE) PREFIX=$(CURDIR)/debian/tmp install
	$(MAKE) LIBRARY_KIND=static PREFIX=$(CURDIR)/debian/tmp install
	rm -rf debian/tmp/lib/gnat

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

.PHONY: override_dh_install
override_dh_install: debian/dbusada.gpr
	dh_install -p$(lib_pkg) lib/libdbusada.so.$(soversion) usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(dev_pkg) lib/libdbusada.so usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(dev_pkg) lib/libdbusada.a usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(dev_pkg) lib/dbus-ada/*.ali usr/lib/$(DEB_HOST_MULTIARCH)/ada/adalib/dbus-ada
	dh_install -p$(dev_pkg) include/dbus-ada/*.ad[bs] usr/share/ada/adainclude/dbus-ada
	dh_install -p$(dev_pkg) debian/dbusada.gpr usr/share/gpr
	dh_install -p$(dev_pkg) debian/dbus_ada.gpr usr/share/gpr

.PHONY: override_dh_missing
override_dh_missing:
	dh_missing --fail-missing

debian/dbusada.gpr: debian/template_dbusada.gpr
	sed s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/ $< > $@

.PHONY: override_dh_clean
override_dh_clean:
	dh_clean debian/dbusada.gpr debian/$(dev_pkg).doc-base

.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
