#!/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
  $(error 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
  $(error 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_clean:
	$(RM) -r shared-* static-*

override_dh_auto_build-indep:
override_dh_auto_build-arch:
	gprbuild $(GPRBUILD_FLAGS) --create-missing-dirs build.gpr \
          -XLIB_NAME=$(LIB_NAME) \
          -XSONAME=$(SONAME) \
          -XLIB_KIND=static \
          -XLIB_DIR=static-lib \
          -XLIB_OBJ_DIR=static-obj \
          -XLIB_ALI_DIR=static-ali
	gprbuild $(GPRBUILD_FLAGS) --create-missing-dirs build.gpr \
          -XLIB_NAME=$(LIB_NAME) \
          -XSONAME=$(SONAME) \
          -XLIB_KIND=dynamic \
          -XLIB_DIR=shared-lib \
          -XLIB_OBJ_DIR=shared-obj \
          -XLIB_ALI_DIR=shared-ali

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_compress:
	dh_compress --package=$(DEV_PKG) \
          --exclude=.ads \
          --exclude=.adb \
          --exclude=Makefile \
          --exclude=.c \
          --exclude=.h
	dh_compress --package=$(DOC_PKG) \
          --exclude=.ads \
          --exclude=.adb \
          --exclude=Makefile \
          --exclude=.c \
          --exclude=.h \
          --exclude=.cpp
	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

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

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