#!/usr/bin/make -f

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

# Copyright (c) 2003-2006, 2011-2012 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2010-2013 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.

########################
# Guess some variables.
########################

include /usr/share/dpkg/default.mk
# Set CFLAGS (handling noopt from DEB_BUILD_OPTIONS), LDFLAGS,
# DEB_HOST_GNU_TYPE, DEB_HOST_ARCH, DEB_VERSION...

include /usr/share/ada/debian_packaging.mk

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

# Do not try to call an upstream configure or Makefile.
override_dh_auto_configure:
override_dh_auto_build-arch:
override_dh_auto_build-indep:
override_dh_auto_test:
override_dh_auto_install:
override_dh_auto_clean::

######################################################################
# See gnat-gps.TODO and the share/* part of gnat-gps-common.install.
GNAT_SWITCHES_PY := share/plug-ins/gps_utils/gnat_switches.py
binary-indep: $(GNAT_SWITCHES_PY)
$(GNAT_SWITCHES_PY):
	touch $@
override_dh_auto_clean::
	rm -f $(GNAT_SWITCHES_PY)

######################################################################
override_dh_auto_build-arch: gnat-gps

# gnatmake knows if the file needs recompilation.
.PHONY: gnat-gps
gnat-gps:
	gnatmake $(BUILDER_OPTIONS) -k -a -m -Pdebian/build_gps.gpr \
          -XADAFLAGS="$(ADAFLAGS)" \
          -XLDFLAGS="$(LDFLAGS)" \
          -XLDLIBS="$(LDLIBS)" \
          -XC_OBJECTS="$(addprefix ../,$(C_OBJECTS))"

# C_OBJECTS prerequisite is defined later, but may be used in the recipe now.

gnat-gps: LDLIBS := `pkg-config --libs gtk+-2.0 cairo-xlib-xrender python`

gnat-gps: LDLIBS += -lutil
# common/tty/terminals.c calls openpty, needing -lutil on FreeBSD

override_dh_auto_clean::
	rm -f gnat-gps obj/*

######################################################################
CONFIG_ADS := common/src/config.ads
gnat-gps: $(CONFIG_ADS)
$(CONFIG_ADS): %: %.in
	sed -e 's/@GPS_VERSION@/$(DEB_VERSION)/' \
	    -e 's/@TARGET@/$(DEB_HOST_GNU_TYPE))/' \
	    -e 's!@prefix@!/usr!' \
	    -e 's/@HOST_TYPE@/Unix/' \
	    -e 's/@STRIP_CR@/False/' \
	    -e 's/@CAN_OUTPUT@/True/' \
	    -e 's/@EXEC_COMMAND@/sh -c/' \
	    -e 's/@DEFAULT_FONT@/Sans 9/' \
	    -e 's/@DEFAULT_PS@/ps/' \
	    -e 's/@PRINT_CMD@/lpr/' \
	    -e 's/@OPAQUE_MDI@/True/' \
	    -e 's/@DIFF_CMD@/diff -u/' \
	    -e 's/@DIFF3_CMD@/diff3/' \
	    -e 's/@PATCH_CMD@/patch/' \
	    -e 's/@EXECUTION_WINDOW@/True/' \
	    -e "s/unknown date/Debian/" \
	    -e 's/@CHARSET@/UTF-8/' $< > $@
override_dh_auto_clean::
	rm -f $(CONFIG_ADS)

######################################################################
DIRS_WITH_C_SOURCES := \
 common/src \
 common/tty \
 examples/demo/matrix_handling \
 examples/language \
 gbuilder/pixmaps \
 gbuilder/src \
 gnatlib/src \
 gnatlib/src/gtk/python \
 gnatlib/src/postgres/with_postgres \
 gnatlib/src/python \
 gvd/pixmaps \
 prj_editor/src \
 vcs/pixmaps \
 vdiff/src \
 widgets/src
C_SOURCES := $(wildcard $(foreach dir,$(DIRS_WITH_C_SOURCES),$(dir)/*.c))
C_OBJECTS := $(patsubst %.c,%.o,$(C_SOURCES))

gnat-gps: $(C_OBJECTS)

# Default rule, sufficient for most C files. We repeat it here so that it
# is not erased when we add prerequisites to some specific sources.
$(C_OBJECTS): %.o: %.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@

# Use pkg-config to figure the exact cflags needed.
pkg_config_depends = $(1).o: CFLAGS += `pkg-config --cflags $(2)`
$(call pkg_config_depends,gbuilder/src/utils,glib-2.0)
$(call pkg_config_depends,gnatlib/src/gtk/python/python_support_gtk,gtk+-2.0)
$(call pkg_config_depends,widgets/src/check_button,gtk+-2.0)
$(call pkg_config_depends,widgets/src/gtkada_terminal,gtk+-2.0)
$(call pkg_config_depends,widgets/src/render,gtk+-2.0)
$(call pkg_config_depends,gnatlib/src/python/python_support,python)

override_dh_auto_clean::
	rm -f $(C_OBJECTS)

######################################################################
manuals := gps gps_pg tutorial

override_dh_auto_build-indep: docs
.PHONY: docs
docs: docs/Makefile
	$(MAKE) -C docs

docs/Makefile: docs/Makefile.in
	sed -e 's,@prefix@,/usr,' -e 's,@MAKEINFO@,makeinfo,' \
	    -e 's,@TEXI2DVI@,texi2dvi,' -e 's,@INSTALL@,install,' \
	    -e 's,@INSTALL_DATA@,install -m 644,' $< > $@
override_dh_auto_clean::
	if test -f docs/Makefile; then $(MAKE) -C docs distclean; fi
	cd docs && rm -f *.pdf circle_x.png diamond_x.png dot_x.png	\
          gps_pg.txt square_x.png triangle_x.png tutorial.html		\
          tutorial.txt

INFO_MANUALS := $(patsubst %,docs/%.info,$(manuals))
override_dh_auto_build-indep: $(INFO_MANUALS)
$(INFO_MANUALS): docs/%.info: docs/%.texi
	cd docs && makeinfo --no-split -o $*.info $*.texi
override_dh_auto_clean::
	rm -f $(INFO_MANUALS)

######################################################################
# Replacing documentation directories with symbolic links would spare
# 10Kb but creates too much maintainance, see #684194 and #655383.
# From 4.3-1 to 5.0-12, every binary package has created at least a
# link, that dpkg may never have removed since (policy 6.6.4).
# Generated preinst scripts will remove them.
PREINST_ARCH  := debian/gnat-gps.preinst \
                 debian/gnat-gps-dbg.preinst
PREINST_INDEP := debian/gnat-gps-common.preinst \
                 debian/gnat-gps-doc.preinst
binary-arch: $(PREINST_ARCH)
binary-indep: $(PREINST_INDEP)
clean: clean_preinst
$(PREINST_ARCH) $(PREINST_INDEP): debian/%.preinst: debian/rm_doc_symlink.preinst.sed
	sed s/PKGNAME/$*/ $< > $@
.PHONY: clean_preinst
clean_preinst:
	rm -f $(PREINST_ARCH) $(PREINST_INDEP)

######################################################################
override_dh_compress:
	dh_compress --all -X.xml -X.ads -X.adb -X.c -X.py

override_dh_strip:
	dh_strip --package=gnat-gps --dbg-package=gnat-gps-dbg
	dh_strip --remaining-packages
