#!/usr/bin/make -f

##############
# Legal stuff
##############

# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2009-2012 Nicolas Boulenguez <nicolas.boulenguez@free.fr>

# 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.

######################
# Set some variables #
######################

# Magic stuff from GNU Make manual.
comma := ,
empty :=
space := $(empty) $(empty)


# Export CFLAGS and LDFLAGS to upstream Makefile.
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

LIB_NAME := $(patsubst lib%,%,$(DEB_SOURCE))

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

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

# Gnat version has to be mentioned in Build-Depends anyways.
GNAT_VERSION := $(shell sed -n -e "s/.*gnat-\([[:digit:].]\+\),.*/\1/p" debian/control)
ifndef GNAT_VERSION
  $(warning Could not guess GNAT_VERSION from debian/control)
endif

LIB_PKG := lib$(LIB_NAME)$(SOVERSION)
DBG_PKG := lib$(LIB_NAME)-dbg
DEV_PKG := lib$(LIB_NAME)$(ALIVERSION)-dev
DOC_PKG := lib$(LIB_NAME)-doc

# Fill OLD_ALIVERSIONS by hand with obsolete aliversions that depends
# on the same gnat version. See debian-ada-policy for details.
OLD_ALIVERSIONS :=
OLD_DEV_PKGS := \
  $(subst $(space),$(comma) ,$(patsubst %,lib$(LIB_NAME)%-dev, \
  $(OLD_ALIVERSIONS)))

# -R: avoid setting run path option (aka rpath)
# -j: number of jobs
GPRBUILD_FLAGS := -R -j$(shell getconf _NPROCESSORS_ONLN)

######################
# debhelper overrides
######################

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

%:
	dh $@

# dh_clean is overriden with a double-colon rule, as it is convenient
# to add cleaning recipes close to the corresponding file creation.
override_dh_clean::
	dh_clean

override_dh_auto_build:
	dh_auto_build -- \
          LIB_NAME=$(LIB_NAME) \
          SOVERSION=$(SOVERSION) \
          GPRBUILD_FLAGS="$(GPRBUILD_FLAGS)"

override_dh_auto_test:
	dh_auto_test -- \
          LIB_NAME=$(LIB_NAME) \
          GPRBUILD_FLAGS="$(GPRBUILD_FLAGS)"

override_dh_auto_install:
	dh_auto_install -- \
          LIB_NAME=$(LIB_NAME) \
          SOVERSION=$(SOVERSION) \
          ALI_DIR=usr/lib/$(DEB_HOST_MULTIARCH)/ada/adalib \
          LIB_DIR=usr/lib/$(DEB_HOST_MULTIARCH)

override_dh_install:
	dh_install --package=$(DEV_PKG) usr/share/ada/adainclude
	dh_install --package=$(DEV_PKG) usr/lib/$(DEB_HOST_MULTIARCH)/ada/adalib/$(LIB_NAME)
	dh_install --package=$(DEV_PKG) usr/lib/$(DEB_HOST_MULTIARCH)/lib$(LIB_NAME).a
	dh_install --package=$(LIB_PKG) usr/lib/$(DEB_HOST_MULTIARCH)/lib$(LIB_NAME).so.$(SOVERSION)
	dh_install --remaining-packages

# According to dh_installdocs(1), debian/README.Debian should be
# installed automatically, but it is not.
override_dh_installdocs:
	dh_installdocs --package=$(LIB_PKG) debian/README.Debian
	dh_installdocs --package=$(DEV_PKG) --link-doc=$(LIB_PKG) README TODO
	dh_installdocs --package=$(DBG_PKG) --link-doc=$(LIB_PKG)
	dh_installdocs --remaining-packages

override_dh_installexamples:
	dh_installexamples --package=$(DEV_PKG) demo/*
	dh_installexamples --remaining-packages

# We create the lintian override file on the fly because it has to
# contain the soversion. As we cannot mention the multiarch triplet in
# an /usr/share file, we use a wildcard.
override_dh_lintian: debian/$(LIB_PKG).lintian-overrides
	dh_lintian --remaining-packages
debian/$(LIB_PKG).lintian-overrides:
	echo "# GNAT uses trampolines to implement Ada exceptions." >> $@
	echo "$(LIB_PKG) binary: shlib-with-executable-stack usr/lib/*/lib$(LIB_NAME).so.$(SOVERSION)" >> $@
override_dh_clean::
	$(RM) debian/$(LIB_PKG).lintian-overrides

override_dh_link:
	dh_link --package=$(DEV_PKG) \
          usr/lib/$(DEB_HOST_MULTIARCH)/lib$(LIB_NAME).so.$(SOVERSION) \
          usr/lib/$(DEB_HOST_MULTIARCH)/lib$(LIB_NAME).so
	dh_link --remaining-packages

override_dh_compress:
	dh_compress --package=$(DEV_PKG) \
          --exclude=.ads \
          --exclude=.adb \
          --exclude=Makefile \
          --exclude=.c \
          --exclude=.h
	dh_compress --remaining-packages

override_dh_strip:
	dh_strip --package=$(LIB_PKG) --dbg-package=$(DBG_PKG)
	dh_strip --package=$(DEV_PKG) --exclude=.a
	dh_strip --remaining-packages

override_dh_gencontrol:
	dh_gencontrol -- $(foreach var, \
          DEV_PKG \
          GNAT_VERSION \
          LIB_PKG \
          OLD_DEV_PKGS \
          , '-Vada:$(subst _,-,$(var))=$($(var))')

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

GET_ORIG_MTN := mtn://www.ada-france.org?net.prjek.mtn-host.libgmpada
GET_ORIG_DIR := libgmpada
GET_ORIG_TGZ := libgmpada.tgz
GET_ORIG_TMP := libgmpada-$@-XXXXXXXXXX

get-orig-source:
	TMPDIR=$$(mktemp --tmpdir --directory $(GET_ORIG_TMP)) && \
        mtn clone -d $$TMPDIR/db.mtn $(GET_ORIG_MTN) -k "" $$TMPDIR/$(GET_ORIG_DIR) && \
        (cd $$TMPDIR && tar --create --gzip --exclude="_MTN" $(GET_ORIG_DIR)) > $(GET_ORIG_TGZ) ; \
        rm -f -r $$TMPDIR
	@echo "The file $(GET_ORIG_TGZ) contains the latest Debian snapshot of upstream sources."

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

RUN_TESTS_TMP := libgmpada-run-tests-XXXXXXXXXX

run-tests:
	TMPDIR=$$(mktemp --tmpdir --directory $(RUN_TESTS_TMP)) && \
        export TMPDIR && \
        (cd $$TMPDIR && sh $(CURDIR)/debian/tests/link_with_shared); \
        if test $$? = 0; then echo ok; else echo ko; fi; \
        rm -f -r $$TMPDIR
