#!/usr/bin/make -f
# debian/rules for gnat-gps.
# Copyright (c) 2003, 2004, 2005, 2006 Ludovic Brenta <lbrenta@debian.org>
#
# This program 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 2 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.
# 
# The full text of the GNU General Public License is in the file
# /usr/share/common-licenses/GPL on Debian systems.

export DH_COMPAT = 4
ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
OS = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
CPUS := $(shell getconf _NPROCESSORS_ONLN)
CFLAGS := -g -O2
GTKCFLAGS := $(shell pkg-config --cflags-only-I gtk+-2.0)
PYTHONCFLAGS := -I/usr/include/$(shell pyversions -d)

regexp := ^Version: ([^.]+)\.([^.]+)\.([^.]+)-(.*)
major := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\1/')
minor := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\2/')
micro := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\3/')
upload := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\4/')

patch-stamp:
	dh_testdir
	-quilt push -a

unpatch:
	dh_testdir
	-quilt pop -a

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

build: patch-stamp gnat-gps $(CBROWSER) $(DBIMP)

GENERATED_FILES := common/src/config.ads

gnat-gps: patch-stamp
gnat-gps: $(GENERATED_FILES) obj/libgps.a obj/libdb.a
	gnatmake -j$(CPUS) -Pgps.gpr

common/src/config.ads: common/src/config.ads.in
	sed -e 's/@GPS_VERSION@/$(major).$(minor).$(micro)/' \
	    -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
LIBDB := $(DB)/PORT/libdb.a

obj/libdb.a: $(LIBDB)
	ln $< $@

# The only place where we do a recursive make.
$(LIBDB):
	cd $(DB); ./configure; $(MAKE)

# 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 common/gnat common/src gbuilder/pixmaps \
gnatlib/src gvd/pixmaps kernel/src_info/sn/src prj_editor/src prj_editor/pixmaps \
python/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.  Some object files require
# special rules to build.

kernel/src_info/sn/src/db_capi.o: kernel/src_info/sn/src/db_capi.c $(LIBDB)
	gcc -c $< -o $@ $(CFLAGS) -I$(DB) -I$(DB)/include -I$(HYPER)

gbuilder/src/utils.o: gbuilder/src/utils.c
	gcc -c $< -o $@ $(CFLAGS) $(GTKCFLAGS)

common/gnat/gexpect_tty.o: common/gnat/gexpect_tty.c
	gcc -c $< -o $@ $(CFLAGS) '-DSYSTEM_INCLUDE="system/gnu-linux.h"'

python/src/python_support.o: python/src/python_support.c
	gcc -c $< -o $@ $(CFLAGS) $(GTKCFLAGS) $(PYTHONCFLAGS)

widgets/src/%.o: widgets/src/%.c
	gcc -c $< -o $@ $(CFLAGS) $(GTKCFLAGS)

# Default rule, sufficient for most C files
%.o: %.c
	gcc -c $< -o $@ $(CFLAGS)

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

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

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f gps-main gnat-gps obj/* $(LIBDB)
	-$(MAKE) -C docs distclean
	rm -f docs/*.pdf docs/*.info docs/*.txt
	rm -f $(GENERATED_FILES) $(C_OBJECTS) gps.log
	for dir in $(DB) $(SN)/snavigator; do \
	   if [ -f $$dir/Makefile ] ; then $(MAKE) -C $$dir distclean; fi; \
	done
	cd $(SN)/snavigator/parsers; rm -f $(PARSERS_GENERATED_FILES)
	cd $(SN)/snavigator; rm -f $(SNAVIGATOR_GENERATED_FILES)
	rm -f $(DB)/include/Makefile
	dh_clean

binary-indep: docs
	dh_testdir
	dh_testroot
	dh_installinfo -pgnat-gps-doc docs/*.info
	dh_install -pgnat-gps-doc \
		docs/gps/* \
		/usr/share/doc/gnat-gps-doc/html/
	dh_install -pgnat-gps-doc \
		$(foreach m,$(manuals),docs/$(m).pdf) \
		/usr/share/doc/gnat-gps-doc/pdf
	dh_install -pgnat-gps-doc \
		$(foreach m,$(manuals),docs/$(m).txt) \
		/usr/share/doc/gnat-gps-doc/txt
	dh_installdocs -pgnat-gps-doc \
		docs/*.html \
		docs/*.gif \
		docs/*.jpg \
		docs/*.xml
	dh_installexamples -pgnat-gps-doc examples/*
	dh_installchangelogs -i
	dh_fixperms -i
	dh_compress -pgnat-gps-doc -X.xml -X.ads -X.adb -X.c -X.py -X.pdf
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build
	dh_testdir
	dh_testroot
	dh_installdirs usr/bin usr/lib/gps \
		usr/share/gps/customize usr/share/doc/gnat-gps \
		usr/share/gps/icons usr/share/gps/python \
		usr/share/make
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_install gnat-gps usr/bin
	dh_install $(DBIMP) usr/lib/gps
	dh_install $(CBROWSER) usr/lib/gps
	dh_install docs/*.png usr/share/gps
# Upstream ships many files with executable permission, lintian complains
	chmod u=rw,go=r debian/gnat-gps/usr/share/gps/*.*
	dh_install share/* usr/share/gps
	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman debian/gnat-gps.1
#	dh_link
	dh_strip
	dh_compress -X.py
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary docs
