#!/usr/bin/make -f

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

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

ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
OS = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
.NOTPARALLEL:
# since gnatmake will do the job(s)
CPUS := $(shell getconf _NPROCESSORS_ONLN)
CFLAGS := -g -O2
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)

version := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/^Version: //')

SN := kernel/src_info/sn/snsrc
CBROWSER := $(SN)/snavigator/parsers/cpp/cbrowser
DBIMP := $(SN)/snavigator/db/dbimp

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

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

.PHONY: build binary binary-arch binary-indep clean install
binary: binary-arch binary-indep
binary-arch: gnat-gps $(CBROWSER) $(DBIMP)
binary-indep: docs
build binary-arch binary-indep clean install:
	dh $@

# Do not try to call an upstream configure or Makefile.
override_dh_auto_configure:
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 obj/libdb.a
	gnatmake -j$(CPUS) -Pgps.gpr -XDEFAULT_PYTHON=$(DEFAULT_PYTHON) -largs $(GTK_LARGS)

common/src/config.ads: common/src/config.ads.in
	sed -e 's/@GPS_VERSION@/$(version)/' \
	    -e 's/@TARGET@/$(OS)/' \
	    -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/" < $< > $@

# How to create the libraries:

HYPER := $(SN)/snavigator/hyper
DB := $(SN)/db-2.7.7
LIBDB := $(DB)/build_unix/libdb.a

obj/libdb.a: $(LIBDB)
	ln -f $< $@
$(LIBDB):
	cd $(DB)/build_unix && ../dist/configure --enable-compat185
	$(MAKE) -C $(DB)/build_unix

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

DIRS_WITH_C_SOURCES := codefix/src codefix/ui/src common/src common/tty \
gbuilder/pixmaps gbuilder/src gnatlib/src gnatlib/src/gmp \
gnatlib/src/gtk/python gnatlib/src/python gvd/pixmaps kernel/src_info/sn/src \
prj_editor/src prj_editor/pixmaps 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
%.o: %.c
	gcc -c $< -o $@ $(CFLAGS)
# Some object files require special rules to build.
kernel/src_info/sn/src/db_capi.o: $(LIBDB)
kernel/src_info/sn/src/db_capi.o: CFLAGS += -I$(DB)/build_unix -I$(DB)/include -I$(HYPER)
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)

$(CBROWSER) $(DBIMP): $(SN)/snavigator/Makefile
	$(MAKE) -C $(SN)/snavigator

$(SN)/snavigator/Makefile: $(SN)/snavigator/Makefile.in
	cd $(SN)/snavigator; ./configure --prefix=/usr \
	   --with-tclconfig=/usr/lib/tcl8.4 \
	   --with-tclinclude=/usr/include/tcl8.4

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 $@ $<

SNAVIGATOR_GENERATED_FILES = hyper/Makefile install/Makefile

PARSERS_GENERATED_FILES = \
assembly/ppc601-eabi/Makefile assembly/ppc601-eabi/config.h \
assembly/ppc601-eabi/config.log assembly/ppc601-eabi/config.status \
assembly/ppc601-eabi/stamp-h cpp/cbrowser cpp/cpplib/Makefile cpp/Makefile \
Makefile

override_dh_auto_clean:
	for dir in docs $(DB)/build_unix $(SN)/snavigator; do \
	   if test -f $$dir/Makefile; then \
              $(MAKE) -C $$dir distclean; \
           fi; \
        done;
	dh_clean \
           $(GENERATED_FILES) $(C_OBJECTS) \
           $(addprefix $(SN)/snavigator/parsers/, $(PARSERS_GENERATED_FILES)) \
           $(addprefix $(SN)/snavigator/, $(SNAVIGATOR_GENERATED_FILES))

override_dh_installdocs:
	dh_installdocs --all --link-doc=gnat-gps

override_dh_install:
	dh_install --all
	dh_install --package=gnat-gps $(DBIMP) $(CBROWSER) usr/lib/gps

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

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