#!/usr/bin/make -f
# Build script for GNATColl in Debian.
# Copyright (c) 2014-2014 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, 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.

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

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

# Move some files out of the way to ensure that they're not used.
BACKED_UP := \
 src/gnatcoll-io-native-codec__win32.adb \
 src/gnatcoll-mmap-system__win32.adb \
 src/gnatcoll-mmap-system__win32.ads
# Build-Depends: libgnatprj-dev libgnatvsn-dev
# gnat/ must stay because configure generates sdefaults.
BACKED_UP += src/gnatcoll__copy.gpr
# Iconv is provided by libc.
BACKED_UP += src/iconv/no_iconv
# Build-Depends: libpython-dev.
BACKED_UP += src/gtk/nopython src/nopython
# Build-Depends: libc6-dev.
BACKED_UP += src/nosyslog
# libpq-dev depends on OpenSSL, which is incompatible with the GPL.
BACKED_UP += src/postgres/with_postgres
# Build-Depends: libreadline-dev.
BACKED_UP += src/readline/no_readline
# Build-Depends: libsqlite3-dev and --with-sqlite configure option
BACKED_UP += src/sqlite/amalgamation src/sqlite/no_sqlite
# We want to use libtemplates-parser, but GPL 2014 hardcodes does not
# support it yet. See gnatcoll_shared.gpr.
BACKED_UP += src/tools/templates
# This executable depends on gnatcoll-sqlite and can regenerate
# src/generated/*. See Makefile/generate-sources. The generated
# sources are short, and I cannot see a way to bootstrap.
# BACKED_UP += src/tools/gnatcoll_db2ada.adb

# Build-Depends: autotools-dev
BACKED_UP += config.guess config.sub
config.guess.backup: replace := ln -s /usr/share/misc/config.guess
config.sub.backup  : replace := ln -s /usr/share/misc/config.sub

# Build-Depends: autoconf
BACKED_UP += configure
configure.backup: replace := autoconf

# Unused, but configure insists on seeing it.
BACKED_UP += install-sh
install-sh.backup: replace := touch install-sh

# Patch version number for documentation.
BACKED_UP += configure.in
configure.backup: configure.in.backup
configure.in.backup: replace := sed -e 2s/\d\.\dw/$(DEB_VERSION_UPSTREAM)/ configure.in.backup > configure.in

# If the original file were a prerequisite, Make would update the
# backup after the original file is replaced.
$(addsuffix .backup,$(BACKED_UP)): %.backup:
	mv $* $@
	$(replace)

# aclocal.m4 insists on passing -Lfoo/lib to the linker with
# --with-sqlite=foo.  This adds a faulty path, but the linker finds
# sqlite in the default location nevertheless.
configure-stamp: $(addsuffix .backup,$(BACKED_UP))
	dh_auto_configure -- --enable-gpl --enable-shared \
          --without-postgresql \
          --with-sqlite=/usr/include
	touch $@

override_dh_auto_configure: configure-stamp

# patches/inherit_compilation_options.diff
BUILDER_OPTIONS += $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS,"-X$(v)=$($(v))")
# patches/inherit_library_versions.diff
# This sed script constructs a -X option for every library package.
BUILDER_OPTIONS += $(shell sed debian/control -ne ' \
  /^Package: lib\([a-z-]\+\)\([0-9.]\+\(gpl[0-9]\{4\}\)\?\)$$/ { \
    s//\U\1\E_VERSION=\2/; \
    y/-/_/; \
    s/^/-X/; \
    p}')
# Makefile says:
# # Builds relocatble first so that the tools are preferably linked statically.
# all: relocatable static
# We want the contrary.
override_dh_auto_build-arch:
	$(MAKE) static relocatable 'GPRBUILD_OPTIONS=$(BUILDER_OPTIONS)'

# Upstream docs target does not generate text output.
override_dh_auto_build-indep:
	$(MAKE) -C docs html latexpdf text SPHINXOPTS=-j$(BUILDER_JOBS)

override_dh_auto_test:

override_dh_auto_install:

# Configure, so that we can assume *.backup and Makefiles exist.
override_dh_auto_clean: configure-stamp
	dh_auto_clean -- GPRCLEAN_OPTIONS=

override_dh_clean:
	dh_clean --exclude=testsuite/xref/F516-004/default.gpr.orig
 # Remove backed up directories so that mv below has the wanted effect.
	for f in $(BACKED_UP); do \
          if test -e $$f.backup; then \
            rm -fr $$f && mv $$f.backup $$f; \
          fi; \
        done

# Check dependencies across -dev packages (or generated projects)
# after any change in the structure of upstream projects.
override_dh_ada_library:
	dh_ada_library $(shell sed debian/control -ne ' \
          /^Package: lib\([a-z-]\+\)[0-9.]\+\(gpl[0-9]\{4\}\)\?-dev$$/ { \
            s~~src/\1.gpr~; \
            y/-/_/; \
            p}')

override_dh_compress:
	dh_compress --package=libgnatcoll-doc --exclude=.adb --exclude=.ads
	dh_compress --remaining-packages
