#!/usr/bin/make -f

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

# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2009-2015 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, see <http://www.gnu.org/licenses/>.

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

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

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

DEB_DATE_RFC_2822 := $(shell dpkg-parsechangelog -S date)
DEB_DATE_RFC_3339 := $(shell date "+%F %T" -d "$(DEB_DATE_RFC_2822)")

ADAFLAGS += -gnatn
# This is very useful for the thin binding.

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)
  # Not error. Policy defines targets that may be run from outside build dir.
endif

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

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

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

override_dh_auto_build override_dh_auto_test: override_%:
 # Ensure that generated sources have been produced.
	dh_auto_build -- src/gmp-constants.ads $(foreach var,\
          ADAFLAGS CC CFLAGS CPPFLAGS LDFLAGS SOVERSION,$(var)="$($(var))") \
          GNATMAKE_OPTIONS="$(BUILDER_OPTIONS)"
 # Touch generated sources for deterministic ALI files and tar headers.
	find . -depth -newermt '$(DEB_DATE_RFC_3339)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE_RFC_3339)'
 # CC is passed from debian_packaging.mk to Makefile to circumvent #768706.
 # Upstream should autodetect it correctly after 0.0.20131223.
	$* -- $(foreach var,\
          ADAFLAGS CC CFLAGS CPPFLAGS LDFLAGS SOVERSION,$(var)="$($(var))") \
          GNATMAKE_OPTIONS="$(BUILDER_OPTIONS)"

# Ignore these targets from upstream Makefile.
override_dh_auto_install:

override_dh_compress:
	dh_compress --all \
          --exclude=.ads \
          --exclude=.adb \
          --exclude=Makefile \
          --exclude=.c \
          --exclude=.h

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

GET_ORIG_MTN := mtn://www.ada-france.org?net.prjek.mtn-host.libgmpada
GET_ORIG_DIR := lib$(LIB_NAME)
GET_ORIG_TGZ := lib$(LIB_NAME).tgz
GET_ORIG_TMP := lib$(LIB_NAME)-get-orig-source-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."
