#!/usr/bin/make -f
# debian/rules for libgtkada
# Copyright (c) 2003-2012 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2011-2012 Nicolas Boulenguez <nicolas.boulenguez@free.fr>
#
# This program 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.
#
# The full text of the GNU General Public License is in the file
# /usr/share/common-licenses/GPL-3 on Debian systems.

.SUFFIXES:

binary binary-arch binary-indep build build-arch build-indep clean:
	dh $@ --with ada-library

# Ignore upstream's build system
override_dh_auto_configure:
override_dh_auto_build-arch:: # Will receive prerequisites and gprbuild calls.
override_dh_auto_build-indep: # Will receive prerequisites
override_dh_auto_test:
override_dh_auto_install:
override_dh_auto_clean::      # Will receive recipes

######################################################################
# Build directories

# In order to ease cleaning and avoid name conflicts, put as many
# generated files as possible into a specific directory.
# debian/*.gpr projects rely on this name.
GEN_DIR := debian/build

GEN_SUBDIRS := $(addprefix $(GEN_DIR)/,lib-dynamic lib-static obj-dynamic obj-static)

$(GEN_SUBDIRS): | $(GEN_DIR)
$(GEN_DIR) $(GEN_SUBDIRS):
	mkdir $@
override_dh_auto_clean::
	rm -f -r $(GEN_DIR)

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

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

C_INCLUDE_DIRS := $(patsubst -I%,%,$(shell \
  pkg-config --cflags-only-I gtk+-2.0 libgnomeui-2.0 gl x11))

exported_variables := $(foreach var,\
  ADAFLAGS C_INCLUDE_DIRS CFLAGS LDFLAGS,-X$(var)="$($(var))")
# They will actually be passed with command line -X options, making
# the information flow explicit and local. The content will grow-up
# with a new SOVERSION each time the template is instantiated.

# Template to avoid redundant code for libraries.

define library_template
# 1: library name, as in libNAME.so.xxx
# 1_VERSIONED_GPR: if not empty, a versioned link points to the system gpr file

$(1)_SOVERSION  := $(shell sed -n -r 's/^Package: lib$(1)([[:digit:].]+)$$/\1/p' debian/control)
$(1)_DEV_PKG    := $(shell sed -n -r 's/^Package: (lib$(1)[[:digit:].]+-dev)$$/\1/p' debian/control)
$(1)_LIB_PKG    := lib$(1)$$($(1)_SOVERSION)

exported_variables += -X$(1)_SOVERSION="$$($(1)_SOVERSION)"

override_dh_auto_build-arch::  | $(GEN_SUBDIRS)
	gprbuild debian/$(1).gpr -XKIND=dynamic $(BUILDER_OPTIONS) $$(exported_variables)
	gprbuild debian/$(1).gpr -XKIND=static  $(BUILDER_OPTIONS) $$(exported_variables)

# For compatibility with previous versions, add a duplicate project
# with a versioned name.
ifneq (,$$($(1)_VERSIONED_GPR))
override_dh_install::
	cd debian/$$($(1)_DEV_PKG)/usr/share/ada/adainclude && \
        sed -e "s/library project $(1) is/library project $(1)2 is/" \
            -e "s/end $(1);/end $(1)2;/" \
            $(1).gpr > $(1)2.gpr
endif

endef # library_template

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

# Because of :: rules, this has to come before gtkada recipes.
override_dh_auto_build-arch:: src/gtkada-intl.adb src/config.h
gtkada_VERSIONED_GPR := true
$(eval $(call library_template,gtkada))

# Some more prerequisites for generated sources.

src/gtkada-intl.adb: src/gtkada-intl.gpb
	gnatprep -DGETTEXT_INTL=False -DHAVE_GETTEXT=True $< $@
override_dh_auto_clean::
	rm -f src/gtkada-intl.adb

src/config.h: src/config.h.in
	touch $@
override_dh_auto_clean::
	rm -f src/config.h

$(eval $(call library_template,gtkglada))

gnomeada_VERSIONED_GPR := true
$(eval $(call library_template,gnomeada))

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

TOOLS := $(GEN_DIR)/obj-dynamic/gtkada-dialog

override_dh_auto_build-arch:: | $(GEN_SUBDIRS)
	gprbuild debian/tools.gpr -XKIND=dynamic $(BUILDER_OPTIONS) $(exported_variables)

override_dh_install::
	dh_install --package=libgtkada-bin $(TOOLS) usr/bin
	dh_installman --package=libgtkada-bin docs/gtkada-dialog.1

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

GTKADA_CONFIG := $(GEN_DIR)/gtkada-config

override_dh_auto_build-arch:: $(GTKADA_CONFIG)
override_dh_install::
	dh_install --package=libgtkada-bin $(GTKADA_CONFIG) usr/bin
	dh_installman --package=libgtkada-bin docs/gtkada-config.1

$(GTKADA_CONFIG): debian/template_gtkada-config | $(GEN_DIR)
	sed $< \
           -e 's/DEB_HOST_MULTIARCH/$(DEB_HOST_MULTIARCH)/g' \
           -e 's/GTKADA_LDFLAGS/$(shell pkg-config gtk+-2.0 --libs-only-l)/g' \
           -e 's/GTKADA_VERSION/$(gtkada_SOVERSION)/g' \
           > $@

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

TESTGTK_TGZ := $(GEN_DIR)/testgtk.tgz

override_dh_auto_build-indep: $(TESTGTK_TGZ)
override_dh_install::
	dh_installexamples --package=libgtkada-doc $(TESTGTK_TGZ)

$(TESTGTK_TGZ): | $(GEN_DIR)
	tar czf $@ testgtk

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

UG_DIR := docs/gtkada_ug


UG_TXT  := $(UG_DIR)/gtkada_ug.txt
override_dh_auto_build-indep: $(UG_TXT)
override_dh_install::
	dh_installdocs --package=libgtkada-doc $(UG_TXT)

$(UG_TXT): %/gtkada_ug.txt: %/gtkada_ug.texi %/hierarchy.txt
	makeinfo --plaintext -I $* -o $@ $<
override_dh_auto_clean::
	rm -f $(UG_TXT)


UG_INFO := $(UG_DIR)/gtkada_ug.info
override_dh_auto_build-indep: $(UG_INFO)
override_dh_install::
	dh_installinfo --package=libgtkada-doc $(UG_INFO)

# Generated in situ so that makeinfo references the images. This does
# not change much as long dh_installinfo dose not embed them.
$(UG_INFO): %/gtkada_ug.info: %/gtkada_ug.texi %/boxes.jpg %/hierarchy.jpg %/hierarchy.txt
	cd $* && makeinfo -o $(notdir $@) $(notdir $<)
override_dh_auto_clean::
	rm -f $(UG_INFO)


UG_PDF  := $(UG_DIR)/gtkada_ug.pdf
override_dh_auto_build-indep: $(UG_PDF)
override_dh_install::
	dh_installdocs --package=libgtkada-doc $(UG_PDF)

# Generated in situ so that texi2pdf embeds images.
$(UG_PDF): %/gtkada_ug.pdf: %/gtkada_ug.texi %/boxes.jpg %/hierarchy.jpg
	cd $* && texi2pdf --silent -o $(notdir $@) $(notdir $<)
override_dh_auto_clean::
	rm -f $(addprefix $(UG_DIR)/gtkada_ug.,aux cp fn ky log pdf pg toc tp vr)


UG_HTML := $(UG_DIR)/gtkada_ug.html
override_dh_auto_build-indep: $(UG_HTML)
override_dh_install::
	dh_installdocs --package=libgtkada-doc $(UG_HTML) $(UG_DIR)/boxes.jpg $(UG_DIR)/hierarchy.jpg

$(UG_DIR)/gtkada_ug.html: %/gtkada_ug.html: %/gtkada_ug.texi %/boxes.jpg %/hierarchy.jpg
	makeinfo --no-validate --no-split --html -I $* -o $@ $<
override_dh_auto_clean::
	rm -f $(UG_HTML)


# fig2dev correctly embeds boxes.gif even from another directory.
$(UG_DIR)/boxes.jpg: %.jpg: %.fig %.gif
	fig2dev -L jpeg $< $@
override_dh_auto_clean::
	rm -f $(UG_DIR)/boxes.jpg

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

# TODO: the reference manual needs generated parts:
# docs/gtkada_rm/generated.texi docs/gtkada_rm/generated_menu.texi:
# cd docs/gtkada_rm && \ ./generate_doc.pl `find
# $(addprefix ../../src/, . generated gnome opengl) -name "*.ads"`

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

# All arch packages depend on gtkada library package. --link-doc only
# has effect if it is used during the first call to dh_installdocs for
# such packages (anyway, you should not call it twice, see manpage).

# Manpage says that debian/README.Debian should be installed by
# default, but it is not, so we make an explicit call.

# -dbg have already been processed by dh_ada_library(1).
override_dh_installdocs:
	dh_installdocs --package=$(gtkada_LIB_PKG) debian/README.Debian debian/README.source
	dh_installdocs --arch --link-doc=$(gtkada_LIB_PKG)
	dh_installdocs --remaining-packages

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

override_dh_compress:
	dh_compress --package=libgtkada-doc \
           --exclude=.glade \
           --exclude=.adb --exclude=.ads \
           --exclude=.c --exclude=.h \
           --exclude=Makefile \
           --exclude=.pdf
	dh_compress --remaining-packages

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

GET_ORIG_MTN := mtn://www.ada-france.org?com.adacore.gtkada.debian
GET_ORIG_DIR := libgtkada
GET_ORIG_TGZ := libgtkada.tgz
GET_ORIG_TMP := libgtkada-get-orig-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 := libgtkada-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
