#! /usr/bin/make -rf
# Debian build script for asis
# 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 terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2, or (at your
# option) any later version. This build script 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
# distributed with this build script; see file
# /usr/share/common-licenses/GPL. If not, write to the Free Software
# Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk
ADAFLAGS += -gnatafno -gnatwa -gnatVa
ADAFLAGS_no_opt := $(subst -O2,-O0,$(ADAFLAGS))
ADAFLAGS_style := $(subst -gnatfnoy,-gnatfno,$(ADAFLAGS))

# Generate the -X options for gnatmake.
X_flags = $(foreach v,$(1),"-X$(v)=$($(v))")

aliversion := $(shell sed -n -e "s/^Package: libasis\([[:digit:]]\+-dev\)$$/\1/p" debian/control)
ifndef aliversion
  $(warning Could not guess aliversion from debian/control)
endif
soversion := $(shell sed -n -e "s/^Package: libasis\([[:digit:]]\+\)$$/\1/p" debian/control)
ifndef soversion
  $(warning Could not guess soversion from debian/control)
endif

mtnbase := base_debian.db
mtnversion := af45f0024c1a910bdd57055a7b2a1834380c9aa8
branch := com.adacore.asis.debian
orig_dir := asis_$(aliversion).orig
orig_tgz := asis_$(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 Makefile.
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)

######################################################################
# Build dynamic and static versions of the library.
LIB_PROJECT := debian/install_asis

define for_kind_in_dynamic_static_loop
  override_dh_auto_build-arch::
	gnatmake $(BUILDER_OPTIONS) -P$(LIB_PROJECT).gpr \
          $(call X_flags, ADAFLAGS ADAFLAGS_no_opt KIND LDFLAGS soversion)
  override_dh_auto_clean::
	rm -f $(LIB_PROJECT)_lib_$(KIND)/* $(LIB_PROJECT)_obj_$(KIND)/*
endef
$(foreach KIND,dynamic static,$(eval $(for_kind_in_dynamic_static_loop)))

######################################################################
# Import the dynamic version of the library and build each tool.
TOOLS := asistant gnatcheck gnatelim gnatmetric gnatpp gnatstub gnatsync
TOOLS_PROJECT := debian/tools

define for_tool_in_tools_loop
  override_dh_auto_build-arch::
	gnatmake $(BUILDER_OPTIONS) -P$(TOOLS_PROJECT).gpr -XKIND=dynamic \
          $(call X_flags,ADAFLAGS ADAFLAGS_no_opt ADAFLAGS_style LDFLAGS \
                         soversion tool)
  override_dh_auto_clean::
	rm -f $(TOOLS_PROJECT)_obj_$(tool)/*
  override_dh_install-arch::
	dh_install --package=asis-programs $(TOOLS_PROJECT)_obj_$(tool)/$(tool) usr/bin
	dh_installman --package=asis-programs debian/$(tool).1
endef
$(foreach tool,$(TOOLS),$(eval $(for_tool_in_tools_loop)))

######################################################################
# Documentation

override_dh_auto_build-indep:
	$(MAKE) -C documentation
override_dh_auto_clean::
	$(MAKE) -C documentation clean
override_dh_install-indep:
	dh_install --indep
	dh_installdocs --package=asis-doc $(foreach e,html pdf txt,documentation/*.$(e))
	dh_installinfo --package=asis-doc documentation/*.info
	cp tools/gnatmetric/README \
		debian/asis-doc/usr/share/doc/asis-doc/README.gnatmetric
	cp tools/gnatpp/README \
		debian/asis-doc/usr/share/doc/asis-doc/README.gnatpp
	cp tools/gnatsync/README \
		debian/asis-doc/usr/share/doc/asis-doc/README.gnatsync

######################################################################
override_dh_install-arch::
	dh_install --arch

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

RUN_TESTS_TMP := libasis-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
