#!/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

# Work around a compiler bug triggered by -O2 on ia64 (see TODO file).
ADAFLAGS_noO2 := $(patsubst -O2,-O1,$(ADAFLAGS))

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)
lib_pkg := libxmlada$(SOVERSION)
dev_pkg := libxmlada$(ALIVERSION)-dev
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

DIRS := dom input_sources sax schema unicode

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

# 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: libxmlada.a $(soname)

# Let gnatmake decide if rebuild is needed.
.PHONY: libxmlada.a $(soname)

libxmlada.a:
	gnatmake $(BUILDER_OPTIONS) -p -Pdebian/build_xmlada \
          -Xobj=obj-static -Xkind=static \
          -XSOURCE_DIRS="$(addprefix ../,$(DIRS))" \
          $(foreach f,ADAFLAGS ADAFLAGS_noO2 LDFLAGS soname,-X$(f)="$($(f))")

$(soname):
	gnatmake $(BUILDER_OPTIONS) -p -Pdebian/build_xmlada \
          -Xobj=obj-shared -Xkind=dynamic \
          -XSOURCE_DIRS="$(addprefix ../,$(DIRS))" \
          $(foreach f,ADAFLAGS ADAFLAGS_noO2 LDFLAGS soname,-X$(f)="$($(f))")

override_dh_auto_clean::
	rm -f libxmlada.a $(soname) libxmlada.so
	rm -rf ali-obj-static obj-static ali-obj-shared obj-shared

######################################################################
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
XMLADA_GPR    := debian/xmlada.gpr

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

######################################################################
override_dh_install:
	dh_install -p$(lib_pkg) $(soname) usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(dev_pkg) libxmlada.a usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(dev_pkg) $(XMLADA_CONFIG) usr/bin
	dh_install -p$(dev_pkg) obj-shared/*.ali usr/lib/$(DEB_HOST_MULTIARCH)/ada/adalib/xmlada
	dh_install -p$(dev_pkg) $(foreach d,$(DIRS),$(d)/*.ad[bs]) \
		usr/share/ada/adainclude/xmlada
	dh_install -p$(dev_pkg) $(XMLADA_GPR) usr/share/ada/adainclude
	dh_install --remaining-packages

override_dh_installman:
	dh_installman -p$(dev_pkg) debian/xmlada-config.1
	dh_installman --remaining-packages

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

override_dh_link:
	dh_link -p$(dev_pkg) usr/lib/$(DEB_HOST_MULTIARCH)/$(soname) \
                             usr/lib/$(DEB_HOST_MULTIARCH)/libxmlada.so
	dh_link --remaining-packages

override_dh_strip:
	dh_strip -p$(lib_pkg) --dbg-package=$(lib_pkg)-dbg
	dh_strip --remaining-packages

######################################################################
RUN_TESTS_TMP := libxmlada-run-tests-XXXXXXXXXX

run-tests:
	export ADTTMP=`mktemp --tmpdir --directory $(RUN_TESTS_TMP)` && \
        ( cd $$ADTTMP && \
          sh $(CURDIR)/debian/tests/link-with-shared \
        ) 2> $$ADTTMP/stderr; \
        if test $$? != 0; then \
          echo "Test failed: non zero exit status"; \
          cat $$ADTTMP/stderr; \
        elif test -s $$ADTTMP/stderr; then \
          echo "Test failed: output on stderr"; \
          cat $$ADTTMP/stderr; \
        fi; \
        rm -f -r $$ADTTMP
