#!/usr/bin/make -f

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

# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2009-2011 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 #
######################

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

SONAME := lib$(LIB_NAME).so.$(SOVERSION)
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

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

############################
# Ada Library Debian Helper
############################
# This a trick to use debhelper automation with external files,
# even if their names must match the package version.

# Each file named debian/ALDH.oldname is preprocessed and renamed
# according to these macros, before any build or install.  The
# preprocessed file is removed before any clean.
ALDH_macros := \
  ALIVERSION \
  DBG_PKG \
  DEB_HOST_MULTIARCH \
  DEV_PKG \
  DOC_PKG \
  LIB_NAME \
  LIB_PKG \
  SONAME \
  SOVERSION

ALDH_sed := sed $(foreach macro,$(ALDH_macros),-e s/$(macro)/$($(macro))/g)

define ALDH_template
ALDH_targets += $(2)
$(2): $(1)
	$(ALDH_sed) $(1) > $(2)
endef # ALDH_template

ALDH_targets :=
$(foreach template,$(wildcard debian/ALDH.*),\
  $(eval $(call ALDH_template,\
    $(template),\
    $(shell echo $(template) | $(ALDH_sed) -e s/ALDH.//))))

build build-arch build-indep binary binary-arch binary-indep install: $(ALDH_targets)

clean: ALDH_clean
.PHONY: ALDH_clean
ALDH_clean:
	rm -f $(ALDH_targets)

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

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

%:
	dh $@

override_dh_auto_configure-indep:
override_dh_auto_configure-arch:
	dh_auto_configure -- \
          --with-ada-sharedlib=$(SONAME) \
          --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
          $(shell dpkg-buildflags --export=configure)

override_dh_auto_build-indep:
override_dh_auto_build-arch:
	# No need to build examples or cross-distco configuration script manpage.
	$(MAKE) -C include
	$(MAKE) -C gen
	$(MAKE) -C src ADAMAKE="gprbuild $(GPRBUILD_FLAGS)"
	# For consistency. dh_install cannot rename files.
	mv lib/libAdaCurses.a lib/lib$(LIB_NAME).a

override_dh_auto_install:

override_dh_installdocs:
	dh_installdocs --link-doc=$(LIB_PKG) \
          --package=$(LIB_PKG) \
          --package=$(DEV_PKG) \
          --package=$(DBG_PKG)
	dh_installdocs --remaining-packages

override_dh_installchangelogs:
	dh_installchangelogs --all NEWS

# samples/explain.txt is read by samples/sample-explanation.adb.
override_dh_compress:
	dh_compress --package=$(DEV_PKG) \
          --exclude=.ads \
          --exclude=.adb
	dh_compress --package=$(DOC_PKG) \
          --exclude=.ads \
          --exclude=.adb \
          --exclude=explain.txt
	dh_compress --remaining-packages

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

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

GET_ORIG_FTP := ftp://invisible-island.net/AdaCurses/current
GET_ORIG_TGZ := AdaCurses.tar.gz
GET_ORIG_TMP := libgmpada-$@-XXXXXXXXXX

get-orig-source:
	wget $(GET_ORIG_FTP)/$(GET_ORIG_TGZ)
	@echo "The file $(GET_ORIG_TGZ) contains the latest snapshot of upstream sources."

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

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