#!/usr/bin/make -f
# Build script for Templates_Parser in Debian.
# Copyright (c) 2003-2008 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2013-2017 Nicolas Boulenguez <nicolas@debian.org>

# This build script is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

# On Debian systems, the full text of the GPL is in the file
# /usr/share/common-licenses/GPL-3.

$(foreach line,$(shell sed -n '\
  s/^ gnat, gnat-\(.*\),$$/ \
    GNAT_VERSION:=\1 \
  /p;\
  s/^Package: lib[a-z-]\+\([0-9.]\+\)$$/ \
    SOVERSION:=\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/default.mk
include /usr/share/ada/debian_packaging-$(GNAT_VERSION).mk
BUILDER_OPTIONS := $(filter-out -j$(BUILDER_JOBS),$(BUILDER_OPTIONS))

# Some of these options are stored into makefile.setup, but not all.
MAKE_OPTS := \
  VERSION=$(SOVERSION) \
  DEFAULT_LIBRARY_TYPE=relocatable \
  GPRBUILD="gprbuild $(BUILDER_OPTIONS) $(foreach v,ADAFLAGS LDFLAGS,'-X$(v)=$($(v))')" \
  PROCESSORS=$(BUILDER_JOBS) \
  DOC_FORMATS="html info latexpdf text"

######################################################################
%:
	dh $@ --with ada-library

# Avoid DebHelper default calls to usual Make targets.
.PHONY: $(addprefix override_dh_auto_, \
  configure build-arch build-indep test install clean)

# Force reconfiguration.
override_dh_auto_configure:
	rm -f makefile.setup tp_xmlada.gpr

override_dh_auto_build-arch:
	$(MAKE) $(MAKE_OPTS)

override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) build-doc $(MAKE_OPTS) SPHINXOPTS=-j$(BUILDER_JOBS)
endif

override_dh_auto_clean:
	$(MAKE) clean $(MAKE_OPTS)
	rm -f config/setup/foo.ads.std*
	rm -fr docs/__pycache__

######################################################################
.PHONY: override_dh_ada_library-indep
.PHONY: override_dh_ada_library-arch
override_dh_ada_library-arch:
 # NOTE:
 # xmlada_*.gpr are not imported directly by upstream build project, so
 # - ada:Depends for the -dev package will not contain libxmlada*-dev.
 # - the generated project will not contain with "xmlada_*.gpr".
 # This is OK as long as XMLAda is only used in bodies and not in specs.
	dh_ada_library \
	  PRJ_BUILD=release TP_XMLADA=Installed LIBRARY_TYPE=relocatable \
	  VERSION=ignored TARGET=$(DEB_HOST_GNU_TYPE) templates_parser.gpr

# Mimic 'dh --with sphinxdoc', but without adding sphinx-common to
# Build-Depends for arch-only builds.
.PHONY: override_dh_installdocs-indep
override_dh_installdocs-indep:
	dh_installdocs -i
	dh_sphinxdoc

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=libtemplates-parser-doc --exclude=examples/templates.tads
	dh_compress --remaining-packages