#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

SOURCE_DIR=$(CURDIR)
BUILD_SHARED_DIR=$(SOURCE_DIR)

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

# Prefer hardware floating point if enabled
ifneq (,$(findstring vfp,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mfpu=vfp -mfloat-abi=softfp
        ENABLE_GTK_DOC = --disable-gtk-doc
else
# Use soft-float and thumb mode if it enabled.
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifeq (,$(findstring nodocs,$(DEB_BUILD_OPTIONS)))
ENABLE_GTK_DOC ?= --enable-gtk-doc
else
ENABLE_GTK_DOC = --disable-gtk-doc
endif

ifneq (,$(findstring parallel,$(DEB_BUILD_OPTIONS)))
    PARALLEL_JOBS := $(shell echo $(DEB_BUILD_OPTIONS) | \
        sed -e 's/.*parallel=\([0-9]\+\).*/\1/')
    ifeq ($(DEB_BUILD_OPTIONS),$(PARALLEL_JOBS))
        PARALLEL_JOBS := $(shell if [ -f /proc/cpuinfo ]; \
            then echo `cat /proc/cpuinfo | grep 'processor' | wc -l`; \
            else echo 1; fi)
    endif
    NJOBS := -j$(PARALLEL_JOBS)
endif

# Ensure the build aborts when there are still references to undefined
# symbols.
### Disabled for now as gtk is actually broken currently
###LDFLAGS += -Wl,-z,defs

# Make the linker work a bit harder so dynamic loading can be done faster
LDFLAGS += -Wl,-O1

# Disable all locales to save footprint
export LINGUAS = 

##
version_with_epoch=$(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //' -e 's/-[A-Za-z0-9\.\+]*$$//g' -e 's/+[A-Za-z0-9\.]*$$//g')
version=$(shell echo ${version_with_epoch} | sed -e 's/^[0-9]*://')
major=$(shell echo ${version} | sed -e 's/\(^[0-9]*\)\.[0-9]*\.[0-9]*$$/\1/')
minor=$(shell echo ${version} | sed -e 's/^[0-9]*\.\([0-9]*\)\.[0-9]*$$/\1/')
rel=$(shell echo ${version} | sed -e 's/^[0-9]*\.[0-9]*\.\([0-9]*\)$$/\1/')
shortver=$(major).$(minor)
apiver=2.0
soname=0
suffix=
modver=$(major).$(minor).0
glibver=2.0
atkver=1.0
pangover=1.0
libpath=$(BUILD_SHARED_DIR)/gdk-pixbuf/.libs:$(BUILD_SHARED_DIR)/gdk/.libs:$(BUILD_SHARED_DIR)/gtk/.libs


debian/control:
	dh_testdir

	sed -e 's/@SONAME@/${soname}${suffix}/g' -e 's/@VERSION@/${version}/g' $@.in > $@

$(SOURCE_DIR)/configure:
	AUTOGEN_SUBDIR_MODE=1 $(SOURCE_DIR)/autogen.sh

configure-shared: $(BUILD_SHARED_DIR)/config.status
$(BUILD_SHARED_DIR)/config.status: $(SOURCE_DIR)/configure
	dh_testdir

	# Add here commands to configure the package.
	cd $(BUILD_SHARED_DIR) && \
	env CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" $(SOURCE_DIR)/configure \
			--host=$(DEB_HOST_GNU_TYPE) \
			--build=$(DEB_BUILD_GNU_TYPE) \
			--prefix=/usr \
			--mandir=\$${prefix}/share/man \
			--infodir=\$${prefix}/share/info \
			--sysconfdir=/etc \
			--with-html-dir=\$${prefix}/share/doc/libgtk$(apiver)-doc \
			--with-xinput=yes \
			--enable-shared \
			--disable-static \
			--disable-dependency-tracking \
			--disable-gtk-doc \
			--with-maemo=yes \
			--without-libjasper

	cd $(BUILD_SHARED_DIR) && \
	sed < libtool > libtool-2 \
	 -e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
	 -e '/^archive_cmds="/s/"$$/ \\$$deplibs"/' && \
	mv libtool-2 libtool


configure: configure-shared

build-shared: debian/control configure-shared $(BUILD_SHARED_DIR)/build-stamp
$(BUILD_SHARED_DIR)/build-stamp:
	dh_testdir

	-rm -f $(SOURCE_DIR)/install-test-stamp

	# Add here commands to compile the package.
	$(MAKE) $(NJOBS) -C $(BUILD_SHARED_DIR) \
		LD_LIBRARY_PATH=$(libpath):$(LD_LIBRARY_PATH)

	# The FAQ and tutorial are normally built only during 'make dist'
	$(MAKE) -C $(BUILD_SHARED_DIR)/docs/faq html
	$(MAKE) -C $(BUILD_SHARED_DIR)/docs/tutorial html

	touch $@

build: build-shared

clean: debian/control
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process.
	-make maintainer-clean
	-rm -f $(SOURCE_DIR)/install-test-stamp
	-rm -f $(SOURCE_DIR)/build-stamp

	dh_clean

install-test: $(SOURCE_DIR)/install-test-stamp
$(SOURCE_DIR)/install-test-stamp:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	touch $@

install-shared: build-shared install-test
	# Add here commands to install the package into debian/tmp
	$(MAKE) -C $(BUILD_SHARED_DIR) install \
		DESTDIR=$(CURDIR)/debian/tmp

install: install-shared
	# generating debian files from .in
	for f in `find debian/ -name "[^c]*.in"`; do \
		sed -e "s/@VERSION@/${version}/g" -e "s/@MODVER@/${modver}/g" -e "s/@APIVER@/${apiver}/g" $$f > `echo $$f | sed -e "s/\.in//"`; \
	done

	# for -doc
	# (disabled pending on tools update)
	#cp -a $(BUILD_SHARED_DIR)/docs/faq/html debian/libgtk$(apiver)-doc/usr/share/doc/libgtk$(apiver)-doc/gtk-faq/
	#cp -a $(CURDIR)/debian/gtk-faq.devhelp debian/libgtk$(apiver)-doc/usr/share/doc/libgtk$(apiver)-doc/gtk-faq/

	# (disabled pending on tools update)
	#cp -a $(BUILD_SHARED_DIR)/docs/tutorial/html debian/libgtk$(apiver)-doc/usr/share/doc/libgtk$(apiver)-doc/gtk-tutorial/
	#cp $(CURDIR)/debian/gtk-tutorial.devhelp debian/libgtk$(apiver)-doc/usr/share/doc/libgtk$(apiver)-doc/gtk-tutorial/

	cp $(BUILD_SHARED_DIR)/docs/*.txt debian/libgtk$(apiver)-doc/usr/share/doc/libgtk$(apiver)-doc

	# examples files (without Makefile.am, find-examples.sh and .cvsignore)
	mkdir -p $(CURDIR)/debian/gtk$(apiver)-examples/usr/share/doc/gtk$(apiver)-examples
	find examples -name .svn -prune -o \
		-not \( -type d -o -name Makefile.am -o -name find-examples.sh -o -name .cvsignore \) -print | \
		tar cf - -T- | \
		tar xf - -C $(CURDIR)/debian/gtk$(apiver)-examples/usr/share/doc/gtk$(apiver)-examples

	# debian files
	install -m 755 $(CURDIR)/debian/update-gtk-immodules \
		       $(CURDIR)/debian/libgtk$(apiver)-bin/usr/sbin/
	install -m 755 $(CURDIR)/debian/update-gdkpixbuf-loaders \
		       $(CURDIR)/debian/libgtk$(apiver)-bin/usr/sbin/

	# don't need executable attribute
	for f in `find $(CURDIR)/debian/tmp/usr/share/themes -type f`; do \
		chmod a-x $$f; \
	done

	# replace gtk-update-icon-cache with no-op wrapper
	rm debian/tmp/usr/bin/gtk-update-icon-cache
	install -m 755 $(CURDIR)/debian/gtk-update-icon-cache $(CURDIR)/debian/tmp/usr/bin/

	# clean the unnecessary files up
	rm -rf debian/tmp/usr/share/gtk-doc

	dh_movefiles --sourcedir=debian/tmp
	-find $(CURDIR)/debian/ -type d -empty | xargs rmdir -p > /dev/null 2>&1

	# don't delete /etc/gtk-2.0
	install -d $(CURDIR)/debian/libgtk$(apiver)-bin/etc/gtk-$(apiver)

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i

	dh_installdocs -i

	rm -rf $(CURDIR)/debian/libgtk$(apiver)-common/usr/share/doc/libgtk$(apiver)-common

	dh_link -plibgtk$(apiver)-doc				\
		usr/share/doc/libglib$(glibver)-doc/glib	\
		usr/share/doc/libgtk$(apiver)-doc/glib
	dh_link -plibgtk$(apiver)-doc				\
		usr/share/doc/libglib$(glibver)-doc/gobject	\
		usr/share/doc/libgtk$(apiver)-doc/gobject
	dh_link -plibgtk$(apiver)-doc				\
		usr/share/doc/libatk$(atkver)-doc/atk		\
		usr/share/doc/libgtk$(apiver)-doc/atk
	dh_link -plibgtk$(apiver)-doc				\
		usr/share/doc/libpango$(pangover)-doc/pango	\
		usr/share/doc/libgtk$(apiver)-doc/pango
	dh_link -plibgtk$(apiver)-common			\
		usr/share/doc/libgtk$(apiver)-$(soname)$(suffix) \
		usr/share/doc/libgtk$(apiver)-common

# create symlinks in /usr/share/gtk-doc/html
	for i in gdk gdk-pixbuf gtk gtk-faq gtk-tutorial; do	\
		dh_link -plibgtk$(apiver)-doc			\
			usr/share/doc/libgtk$(apiver)-doc/$$i	\
			usr/share/gtk-doc/html/$$i;		\
	done

	dh_installinfo -i
	dh_installchangelogs -i $(SOURCE_DIR)/ChangeLog
	dh_compress -i -Xindex.sgml
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i
	touch debian/files
	rm debian/files
	touch debian/files
# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a

#	dh_installdebconf	

	rm -rf $(CURDIR)/debian/libgtk$(apiver)-$(soname)-dbg/usr/share/doc/libgtk$(apiver)-$(soname)-dbg
	rm -rf $(CURDIR)/debian/libgtk$(apiver)-dev/usr/share/doc/libgtk$(apiver)-dev
	rm -rf $(CURDIR)/debian/libgtk$(apiver)-bin/usr/share/doc/libgtk$(apiver)-bin

	dh_link -plibgtk$(apiver)-$(soname)-dbg \
		usr/share/doc/libgtk$(apiver)-$(soname)$(suffix) \
		usr/share/doc/libgtk$(apiver)-$(soname)-dbg
	dh_link -plibgtk$(apiver)-dev \
		usr/share/doc/libgtk$(apiver)-$(soname)$(suffix) \
		usr/share/doc/libgtk$(apiver)-dev
	dh_link -plibgtk$(apiver)-bin \
		usr/share/doc/libgtk$(apiver)-$(soname)$(suffix) \
		usr/share/doc/libgtk$(apiver)-bin
	
	mkdir -p debian/libgtk-dnd/opt/gtk-dnd
	cp debian/tmp/usr/lib/libgtk-x11-2.0.so.0.1400.7 debian/libgtk-dnd/opt/gtk-dnd/
	dh_compress -a -Xindex.sgml
	dh_fixperms -a
	dh_installdeb -a
	dh_gencontrol -a
	rm -rf debian/*postins*
	rm -rf debian/*prerm*
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build build-shared build-static clean binary-indep binary-arch binary install install-shared install-static configure configure-shared configure-static debian/control test-shared
