#!/usr/bin/make -f
# Build script for XML/Ada in Debian.
# Copyright (c) 2003-2009 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 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, 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-3.

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

ADAFLAGS += -gnatafno -gnatVa -gnatwa

ALIVERSION := $(shell sed -n -r 's/^Package: libxmlada(.*)-dev$$/\1/p' debian/control)
SOVERSION := $(shell sed -n -r 's/^Package: libxmlada([^-]+)$$/\1/p' debian/control)
soname := libxmlada.so.$(SOVERSION)

mtnbase := base_debian.db
mtnversion := bc73f0c0f1fe677f059a57ef1ccad9e2ca5160a9
branch := com.adacore.xmlada.debian
orig_dir := libxmlada_$(ALIVERSION).orig
orig_tgz := libxmlada_$(ALIVERSION).orig.tar.gz

######################################################################
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_install:
override_dh_auto_test:
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)

######################################################################
override_dh_auto_build-arch:
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build_xmlada -Xkind=static \
          $(foreach f,ADAFLAGS,"-X$(f)=$($(f))")
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build_xmlada -Xkind=dynamic \
          $(foreach f,ADAFLAGS LDFLAGS soname,"-X$(f)=$($(f))")

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

######################################################################
DOCS := $(foreach ext,html info pdf txt,docs/xmlada.$(ext))
override_dh_auto_build-indep: $(DOCS)

docs/xmlada.html: docs/xml.texi
	cd docs; makeinfo --html --no-split $(notdir $<) -o $(notdir $@)

docs/xmlada.info: docs/xml.texi
	cd docs; makeinfo --no-split $(notdir $<$) -o $(notdir $@)

docs/xmlada.pdf: docs/xml.texi
	cd docs; texi2pdf $(notdir $<) -o $(notdir $@)

docs/xmlada.txt: docs/xml.texi
	cd docs; makeinfo --plaintext --no-split $(notdir $<) -o $(notdir $@)

override_dh_auto_clean::
	rm -f $(DOCS)
	rm -f $(foreach ext,aux cp cps pg ky tp vr fn log pdf toc,docs/xml.$(ext))

######################################################################
XMLADA_CONFIG := debian/xmlada-config

override_dh_auto_build-arch: $(XMLADA_CONFIG)
$(XMLADA_CONFIG): %: %.sed
	sed $< $(foreach v,DEB_HOST_MULTIARCH SOVERSION \
          ,-e 's/@$(v)@/$($(v))/g') > $@
override_dh_auto_clean::
	rm -f $(XMLADA_CONFIG)

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

override_dh_installexamples:
	dh_installexamples --all
	find debian -path "debian/libxmlada-doc/usr/share/doc/libxmlada-doc/examples/*/.cvsignore" -delete

override_dh_compress:
	dh_compress --package=libxmlada-doc \
           --exclude=.adb --exclude=.ads --exclude=.gpr \
           --exclude=.xml --exclude=.xsd
	dh_compress --remaining-packages

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

RUN_TESTS_TMP := libxmlada-run-tests-XXXXXXXXXX

run-tests:
	ADTTMP=`mktemp --tmpdir --directory $(RUN_TESTS_TMP)` && \
        ADTTMP=$$ADTTMP sh debian/tests/link-with-shared 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
