#!/usr/bin/make -f
# debian/rules for libgtkada
# Copyright (c) 2003-2012 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2011-2013 Nicolas Boulenguez <nicolas@debian.org>
#
# 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
override_dh_auto_build-indep: # Will receive prerequisites
override_dh_auto_test:
override_dh_auto_install:
override_dh_auto_clean::      # Will receive recipes

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

include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk
CFLAGS := $(CFLAGS) \
 -Wall -Wextra -fstack-check \
 $(shell pkg-config --cflags-only-I gtk+-2.0 libgnomeui-2.0 gl x11)

exported_variables := $(foreach var,\
  ADAFLAGS 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, add a compatibility copy of 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::
	gnatmake -Pdebian/$(1).gpr -XKIND=dynamic $(BUILDER_OPTIONS) $$(exported_variables)
	gnatmake -Pdebian/$(1).gpr -XKIND=static  $(BUILDER_OPTIONS) $$(exported_variables)
override_dh_auto_clean::
	rm -f $$(patsubst %,debian/$(1)_%/*,lib_dynamic lib_static obj_dynamic obj_static)

# 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

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

# Compile C files into the object directory, so that gnatlink embeds
# them. The project must mention the C language, and we must store
# objects from distinct libraries in distinct directories.
C_SRC := $(wildcard src/*.c)

C_DYNAMIC := $(patsubst src/%.c,debian/gtkada_obj_dynamic/%.o,$(C_SRC))
override_dh_auto_build-arch:: $(C_DYNAMIC)
$(C_DYNAMIC): debian/gtkada_obj_dynamic/%.o: src/%.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ -fPIC

C_STATIC  := $(patsubst src/%.c,debian/gtkada_obj_static/%.o,$(C_SRC))
override_dh_auto_build-arch:: $(C_STATIC)
$(C_STATIC): debian/gtkada_obj_static/%.o: src/%.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@

override_dh_auto_build-arch:: src/gtkada-intl.adb
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

$(C_STATIC) $(C_DYNAMIC): src/config.h
src/config.h: src/config.h.in
	touch $@
override_dh_auto_clean::
	rm -f src/config.h

# Template instantiation for each library, cumulating soversions.

gtkada_VERSIONED_GPR := true
$(eval $(call library_template,gtkada))

$(eval $(call library_template,gtkglada))

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

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

TOOLS := debian/tools_obj/gtkada-dialog

override_dh_auto_build-arch::
	gnatmake -Pdebian/tools.gpr -XKIND=dynamic $(BUILDER_OPTIONS) $(exported_variables)
override_dh_auto_clean::
	rm -f debian/tools_obj/*

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

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

GTKADA_CONFIG := debian/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/gtkada-config.sed
	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' \
           > $@
override_dh_auto_clean::
	rm -f $(GTKADA_CONFIG)

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

TESTGTK_TGZ := testgtk.tgz

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

$(TESTGTK_TGZ):
	tar czf $@ testgtk
override_dh_auto_clean::
	rm -f $(TESTGTK_TGZ)

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

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
	cd $* && makeinfo --plaintext -o $(notdir $@ $<)
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 $@ $<)
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 $@ $<)
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
	cd $* && makeinfo --no-validate --no-split --html -o $(notdir $@ $<)
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:
	export ADTTMP=`mktemp --tmpdir --directory $(RUN_TESTS_TMP)` && \
        ( cd $$ADTTMP && \
          sh $(CURDIR)/debian/tests/link-with-shared \
        ) 2> $$ADTTMP/stderr; \
        if test $$? != 0; then \
          echo "Test failed: non zero exit status"; \
          cat $$ADTTMP/stderr; \
        elif test -s $$ADTTMP/stderr; then \
          echo "Test failed: output on stderr"; \
          cat $$ADTTMP/stderr; \
        fi; \
        rm -f -r $$ADTTMP
