#!/usr/bin/make -f
# Build script for Templates_Parser in Debian.
# Copyright (c) 2003-2008 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2013      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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA

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

include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk

LDFLAGS += -l:libgnarl-4.6.so
# avoids FTBFS on arm* because of gnat bug #687642

ALIVERSION := $(shell sed --quiet -r 's/^Package: $(DEB_SOURCE)([0-9.]+)-dev$$/\1/p' debian/control)
SOVERSION := $(shell sed --quiet -r 's/^Package: $(DEB_SOURCE)([0-9.]+)$$/\1/p' debian/control)
mtnbase := base_debian.db
mtnversion := bdf9f07b1b98db7064abab7564892a9736b2edd9
branch := com.adacore.templates_parser.debian
orig_dir := $(DEB_SOURCE)_$(ALIVERSION).orig
orig_tgz := $(DEB_SOURCE)_$(ALIVERSION).orig.tar.gz

SONAME := $(subst -,_,$(DEB_SOURCE)).so.$(SOVERSION)

######################################################################
POLICY_TARGETS := binary binary-arch binary-indep build build-arch \
  build-indep clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh $@ --with ada-library

# Ignore upstream build system.
override_dh_auto_configure:
override_dh_auto_build-arch:
override_dh_auto_build-indep:
override_dh_auto_test:
override_dh_auto_install:
override_dh_auto_clean::

######################################################################
get-orig-source:
	if [ ! -e ../$(mtnbase) ]; then \
		mtn -d../$(mtnbase) db init; \
	fi; \
	mtn -d../$(mtnbase) -k "" pull www.ada-france.org '$(branch)'; \
	cd .. && \
	mtn -d$(mtnbase) co -r$(mtnversion) -b$(branch) $(orig_dir) && \
	rm -rf $(orig_dir)/_MTN $(orig_dir)/.*ignore&& \
	tar czf $(orig_tgz) $(orig_dir) && \
	rm -r $(orig_dir)

######################################################################

# The static archive is needed to generate the docs, so for testing
# the -indep target it is convenient to have a separate subtarget.
build_static_archive:
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build.gpr -XKIND=static \
          $(foreach v,ADAFLAGS,-X$(v)="$($(v))")

override_dh_auto_build-arch: build_static_archive
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build.gpr -XKIND=dynamic \
          $(foreach v,ADAFLAGS LDFLAGS SONAME,-X$(v)="$($(v))")
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build_tools.gpr -XKIND=dynamic \
          $(foreach v,ADAFLAGS LDFLAGS SONAME,-X$(v)="$($(v))")

override_dh_auto_clean::
	rm -f $(foreach d,lib obj,$(foreach k,dynamic static,debian/build_$(d)_$(k)/*))

######################################################################

DOC_MAINS := $(shell sed -n -e "s/^procedure \(\w\+\) is$$/\L\1/gp" docs/all_sources.ada)

override_dh_auto_build-indep: build_static_archive
# This part of the upstream build system is too complicated to
# seriously consider configuring it, even with patches. I want to set
# my CFLAGS, I do not want to compile the library a third time.
	gnatchop -w docs/all_sources.ada docs
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build_docs.gpr -XKIND=static \
          $(foreach v,ADAFLAGS DOC_MAINS LDFLAGS,"-X$(v)=$($(v))")
# What remains can be patched.
	$(MAKE) -C docs

override_dh_auto_clean::
	rm -f $(addprefix docs/, $(foreach m, $(DOC_MAINS) \
            ,$(m).adb $(m).ali $(m).o $(m) $(m).adb.res $(m).adb.texi))
	rm -f $(foreach t,$(DOC_MAINS) table_block user,docs/$(t).tmplt.texi)
	rm -f $(foreach ext,ads.texi html info pdf txt,docs/templates_parser.$(ext))
	rm -f $(foreach sub,debug utils xml,docs/templates_parser-$(sub).ads.texi)
	rm -f ada.sed

######################################################################

override_dh_installchangelogs:
	dh_installchangelogs --package=libtemplates-parser-doc src/ChangeLog
	dh_installchangelogs --remaining-packages

DOC_PKG := libtemplates-parser-doc
EXAMPLES_DIR := debian/$(DOC_PKG)/usr/share/doc/$(DOC_PKG)/examples
override_dh_fixperms-arch:
	if [ -d $(EXAMPLES_DIR) ]; then \
          chmod ugo-x $(EXAMPLES_DIR)/*.tmplt; \
        fi
	dh_fixperms --all

override_dh_compress:
	dh_compress --all --exclude=examples/templates.tads

######################################################################

RUN_TESTS_TMP := $(DEB_SOURCE)-run-tests-XXXXXXXXXX
TESTS := $(addprefix test_, \
  link-with-shared \
  templatespp)
run-tests: $(TESTS)
$(TESTS): test_%:
	ADTTMP=`mktemp --tmpdir --directory $(RUN_TESTS_TMP)` && \
        ADTTMP=$$ADTTMP sh debian/tests/$* 2> $$ADTTMP/stderr; \
        status=$$?; cat $$ADTTMP/stderr; \
        if test $$status != 0; then \
          echo "Test failed: non zero exit status ($$status)."; \
        elif test -s $$ADTTMP/stderr; then \
          echo "Test failed: activity on stderr."; \
        fi; \
        rm -f -r $$ADTTMP
