#!/usr/bin/make -f

DEB_BUILD_MAINT_OPTIONS := hardening=+all
# Set C and LD flags here, so that they also affect the upstream build
# system when it compiles the source generator.
# For now, all other compilations are driven by the project.
DEB_CFLAGS_MAINT_APPEND := -Wall -Wextra
DEB_LDFLAGS_MAINT_APPEND := \
  -Wl,--no-allow-shlib-undefined \
  -Wl,--no-copy-dt-needed-entries \
  -Wl,--no-undefined
DPKG_EXPORT_BUILDFLAGS := 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/ada/packaging.mk

%:
	dh $@

override_dh_autoreconf:
# The ncurses maintainer maintains his own fork of autoconf, which
# refuses the -f option (checked on 2020/04).
	rm -f configure
	dh_autoreconf autoconf-dickey

# TODO: is it now possible to use upstream build system?

# For ./configure:
export CC := gcc-$(DEB_GNAT_VERSION)

NCURSES_MODULES := form menu panel ncurses
NCURSES_CFLAGS  := `pkg-config --cflags $(NCURSES_MODULES)`
LDLIBS          := `pkg-config --libs $(NCURSES_MODULES)`
PROJECT := debian/ncursesada.gpr

override_dh_auto_build: default.cgpr
	$(MAKE) -Cinclude ncurses_def.h
	$(MAKE) -Cgen
 # The src/ build system ignores CFLAGS when building two versions of
 # the library. We only call it to generate a needed file.
 # TODO: the NEWS changelog seems to tell that this has been fixed.
 # Is it possible to pass ADAFLAGS?
	$(MAKE) -Csrc terminal_interface-curses-trace.adb

	gprbuild $(PROJECT) $(GPRBUILDFLAGS) -p \
	  -XSOVERSION= -XNCURSES_CFLAGS="$(NCURSES_CFLAGS)"
	gprbuild $(PROJECT) $(GPRBUILDFLAGS) -p \
	  -XSOVERSION=$(ncursesada_SO_VERSION) -XLDLIBS="$(LDLIBS)" \
	  -XNCURSES_CFLAGS="$(NCURSES_CFLAGS)"

 # The HTML pages must be rebuilt from source, but cannot go to a -doc
 # package because their contents varies accross architectures.
	$(MAKE) -Cgen html GNATHTML='gnathtml -I../debian/build/obj-dynamic'

 # A generated source installed as an example and built by autopkgtests.
	$(MAKE) -Csamples sample-explanation.adb

default.cgpr:
	gprconfig --batch $(GPRCONFIGFLAGS)

override_dh_auto_install:
	gprinstall $(PROJECT) $(static_GPRINSTALLFLAGS) -XSOVERSION=
	gprinstall $(PROJECT) $(call shared_GPRINSTALLFLAGS,ncursesada) \
	  -XSOVERSION=$(ncursesada_SO_VERSION) -XLDLIBS="$(LDLIBS)"

# doc/ provides a -config script giving build flags for
# reverse dependencies. We ignore it as per Debian Ada Policy
# we prefer project files.

.PHONY: override_dh_installchangelogs
override_dh_installchangelogs:
	dh_installchangelogs --all NEWS

execute_before_dh_installdocs:
	sed s/@DEV_PKG@/$(ncursesada_DEV_PKG)/ debian/doc-base-template \
	  > debian/$(ncursesada_DEV_PKG).doc-base.ncurses-ada-binding
	dh_installdocs --package=$(ncursesada_DEV_PKG) doc/Ada95.html doc/ada

execute_before_dh_installexamples:
	dh_installexamples --package=$(ncursesada_DEV_PKG) \
	  samples/explain.txt samples/README samples/*.ad[bs]
