#!/usr/bin/make -f

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

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

# On Debian systems, the full text of the GPL is in the file
# /usr/share/common-licenses/GPL-3.

.SUFFIXES:

########################
# 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 := binary binary-arch binary-indep build build-arch \
  build-indep clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh $@ --with linktree --with sphinxdoc

# Do not try to call an upstream configure or Makefile.
.PHONY: $(addprefix override_dh_auto_, \
  clean configure build-arch build-indep test install)

######################################################################
# See gnat-gps-doc.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`

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/core/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@!/bin/sh -c!' \
	    -e 's/@DEFAULT_FONT@/Sans 9/' \
	    -e 's/@DEFAULT_FIXED_FONT@/Mono 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)

######################################################################
C_SOURCES := \
 common/tty/terminals.c \
 gvd/pixmaps/pixmaps.c \
 prj_editor/src/env.c \
 vcs/pixmaps/pixmaps_vcs.c \
 vdiff/src/pixmaps_vdiff.c \
 widgets/src/check_button.c \
 widgets/src/gtkada_terminal.c \
 widgets/src/native_file_selection.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,widgets/src/check_button,gtk+-2.0)
$(call pkg_config_depends,widgets/src/gtkada_terminal,gtk+-2.0)

override_dh_auto_clean::
	rm -f $(C_OBJECTS)

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

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 clean; fi
	rm -f docs/users_guide/GPS.pyc
	rm -f docs/Makefile

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

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

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