#!/usr/bin/make -f

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

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

########################
# 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

export LDFLAGS

GTKCFLAGS := $(shell pkg-config --cflags-only-I gtk+-2.0)
GTK_LARGS := $(shell pkg-config --libs gtk+-2.0 cairo-xlib-xrender)
DEFAULT_PYTHON := $(shell pyversions -d)
PYTHONCFLAGS := -I/usr/include/$(DEFAULT_PYTHON)

######################
# debhelper overrides
######################

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

build: # do nothing
build-arch: gnat-gps
build-indep: docs
binary: binary-arch binary-indep
binary-arch: build-arch
binary-indep: build-indep
	touch share/plug-ins/gps_utils/gnat_switches.py
	dh $@ --with linktree
%:
	dh $@ --with linktree

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

GENERATED_FILES := common/src/config.ads

# gnatmake knows if the file needs recompilation.
.PHONY: gnat-gps
gnat-gps: $(GENERATED_FILES) obj/libgps.a
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build_gps.gpr -XOPT_CFLAG=$(OPT_CFLAG) -XDEFAULT_PYTHON=$(DEFAULT_PYTHON) -largs $(GTK_LARGS)

common/src/config.ads: common/src/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/@EXECUTION_WINDOW@/True/' \
	    -e "s/unknown date/Debian/" \
	    -e 's/@CHARSET@/UTF-8/' < $< > $@

# Now we compile the other C language source files, and put them all
# together in one library, libgps.a.

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))

obj/libgps.a: $(C_OBJECTS)
	ar rcf $@ $^
	ranlib $@

# The individual objects in libgps.a.

# 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.
%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<
gbuilder/src/utils.o: CFLAGS += $(GTKCFLAGS)
common/gnat/gexpect_tty.o: CFLAGS += '-DSYSTEM_INCLUDE="system/gnu-linux.h"'
gnatlib/src/python/python_support.o: CFLAGS += $(GTKCFLAGS) $(PYTHONCFLAGS)
gnatlib/src/gtk/python/python_support_gtk.o: CFLAGS += $(GTKCFLAGS) $(PYTHONCFLAGS)
widgets/src/%.o: CFLAGS += $(GTKCFLAGS)

manuals := gps gps_pg tutorial

.PHONY: docs
docs: docs/Makefile $(foreach m,$(manuals),docs/$(m).info)
	$(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,' < $< > $@

docs/%.info: docs/%.texi
	makeinfo --no-split -o $@ $<

override_dh_auto_clean:
	for dir in docs; do \
	   if test -f $$dir/Makefile; then \
              $(MAKE) -C $$dir distclean; \
           fi; \
        done
	dh_clean $(C_OBJECTS)

override_dh_installdocs:
	dh_installdocs --package=gnat-gps-doc
	dh_installdocs --remaining-packages --link-doc=gnat-gps-common

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
