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

include debian/scripts/vars
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk
BUILD_DIR=$(SOURCE_DIR)/$(TAR_DIR)
BUILD_SHARED_DIR=$(SOURCE_DIR)/$(TAR_DIR)-shared
BUILD_STATIC_DIR=$(SOURCE_DIR)/$(TAR_DIR)-static

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


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

##
version=$(shell echo "Version: 2.6.4-1osso1" | grep '^Version: ' | sed -e 's/^Version: //' -e 's/-[A-Za-z0-9\.\+]*$$//g' -e 's/+[A-Za-z0-9\.]*$$//g')
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
shver=2.6.4
libpath=$(CURDIR)/$(BUILD_SHARED_DIR)/gdk-pixbuf/.libs:$(CURDIR)/$(BUILD_SHARED_DIR)/gdk/.libs:$(CURDIR)/$(BUILD_SHARED_DIR)/gtk/.libs


debian/control:
	dh_testdir

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

extract: $(STAMP_DIR)/extract-stamp
$(STAMP_DIR)/extract-stamp:
	dh_testdir

	$(MAKE) -f debian/sys-build.mk source.make

	-test -r /usr/share/misc/config.sub && \
	   cp -f /usr/share/misc/config.sub $(BUILD_DIR)/config.sub
	-test -r /usr/share/misc/config.guess && \
	   cp -f /usr/share/misc/config.guess $(BUILD_DIR)/config.guess

	touch $@

configure-shared: extract $(STAMP_DIR)/configure-shared-stamp
$(STAMP_DIR)/configure-shared-stamp:
	dh_testdir

	cp -ar $(BUILD_DIR) $(BUILD_SHARED_DIR)

	# Add here commands to configure the package.
	cd $(BUILD_SHARED_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

	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

	touch $@

configure-shared-simple: extract $(STAMP_DIR)/configure-shared-stamp
$(STAMP_DIR)/configure-shared-stamp:
	dh_testdir

	ln -s ../$(BUILD_DIR) $(BUILD_SHARED_DIR)

	# Add here commands to configure the package.
	cd $(BUILD_SHARED_DIR) && \
	./configure	--host=$(DEB_HOST_GNU_TYPE) \
			--build=$(DEB_BUILD_GNU_TYPE) \
			--prefix=/usr \
			--mandir=\$${prefix}/share/man \
			--infodir=\$${prefix}/share/info \
			--sysconfdir=/etc \
			--disable-gtk-doc \
			--with-xinput=yes \
			--enable-shared \
			--disable-static

	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

	touch $@

configure-static: extract $(STAMP_DIR)/configure-static-stamp
$(STAMP_DIR)/configure-static-stamp:
	dh_testdir

	cp -ar $(BUILD_DIR) $(BUILD_STATIC_DIR)

	# Add here commands to configure the package.
	cd $(BUILD_STATIC_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-xinput=yes \
			--disable-modules \
			--with-included-loaders=png,jpeg \
			--disable-shared \
			--enable-static \
			--disable-dependency-tracking \
			--disable-shortcuts

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

	touch $@

configure: configure-static configure-shared

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

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

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

	touch $@

build-static: debian/control configure-static $(STAMP_DIR)/build-static-stamp
$(STAMP_DIR)/build-static-stamp:
	dh_testdir

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

	# Add here commands to compile the package.
	$(MAKE) -C $(BUILD_STATIC_DIR) \
		RUN_QUERY_IMMODULES_TEST=false

	touch $@

build-dirs:
	-cd $(SOURCE_DIR) && \
		ln -s $(TAR_DIR) gtk+-$(shver) && \
		ln -s $(TAR_DIR)-indep gtk+-$(shver)-indep && \
		ln -s $(TAR_DIR)-shared gtk+-$(shver)-shared && \
		ln -s $(TAR_DIR)-static gtk+-$(shver)-static     

build: build-dirs build-static build-shared

clean:: debian/control
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process.
	$(MAKE) -f debian/sys-build.mk source.clean
	-rm -rf $(BUILD_SHARED_DIR)
	-rm -rf $(BUILD_STATIC_DIR)
	-rm -rf $(STAMP_DIR)

	dh_clean

install-test: $(STAMP_DIR)/install-test-stamp
$(STAMP_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/libgtk$(apiver)-$(soname)$(suffix)

install-static: build-static install-test
	# Add here commands to install the package into debian/tmp
	$(MAKE) -C $(BUILD_STATIC_DIR) install \
		DESTDIR=$(CURDIR)/debian/libgtk$(apiver)-$(soname)$(suffix) \
		RUN_QUERY_IMMODULES_TEST=false \
		RUN_QUERY_LOADER_TEST=false

install: install-static 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
	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/

	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 -ar $(BUILD_SHARED_DIR)/docs/tutorial/images 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

	# for -dbg
	-rm -rf $(CURDIR)/debian/libgtk$(apiver)-$(soname)$(suffix)/usr/lib/debug
	mkdir -p $(CURDIR)/debian/libgtk$(apiver)-$(soname)$(suffix)/usr/lib/debug
	for l in `find $(CURDIR)/debian/libgtk$(apiver)-$(soname)$(suffix)/usr/lib -name 'lib*.so*'`; do \
		cp -vdf $$l \
			$(CURDIR)/debian/libgtk$(apiver)-$(soname)$(suffix)/usr/lib/debug; \
	done

	# 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/libgtk$(apiver)-$(soname)$(suffix)/usr/share/themes -type f`; do \
		chmod a-x $$f; \
	done

	# clean the unnecessary files up
	rm -rf debian/libgtk$(apiver)-$(soname)$(suffix)/usr/share/gtk-doc

	# remove extra input modules (leave X im for development use and OSSO im for real use)
	rm -rf debian/libgtk$(apiver)-$(soname)$(suffix)/usr/lib/gtk-2.0/2.4.0/immodules/{im-am-et,im-cedilla,im-cyrillic-translit,im-inuktitut,im-ipa,im-thai-broken,im-ti-er,im-ti-et,im-viqr}*        

	dh_movefiles --sourcedir=debian/libgtk$(apiver)-$(soname)$(suffix)
	-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_installexamples -i
	dh_installinfo -i
	dh_installchangelogs -i $(BUILD_DIR)/ChangeLog
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

test-shared: configure-shared-simple install-shared
	cp $(CURDIR)/debian/libgtk2.0-0/usr/lib/libgtk-x11-2.0.so.0.600.4 /usr/lib

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a

#	dh_installdebconf	
	dh_installdocs -a

	rm -rf $(CURDIR)/debian/libgtk$(apiver)-dbg/usr/share/doc/libgtk$(apiver)-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)-dbg \
		usr/share/doc/libgtk$(apiver)-$(soname)$(suffix) \
		usr/share/doc/libgtk$(apiver)-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

	dh_installexamples -a

	rm $(CURDIR)/debian/gtk$(apiver)-examples/usr/share/doc/gtk$(apiver)-examples/examples/find-examples.sh

	dh_installman -a
	dh_installinfo -a
	dh_installchangelogs -a $(BUILD_DIR)/ChangeLog
	dh_strip -a -Nlibgtk$(apiver)-dbg
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -Nlibgtk$(apiver)-dbg
	dh_makeshlibs -n -X usr/lib/gtk-$(apiver) -plibgtk$(apiver)-$(soname)$(suffix) -V "libgtk${apiver}-${soname}${suffix} (>= 1:${shver}-8)"
	dh_installdeb -a
	dh_shlibdeps -l$(libpath) -a
	cat debian/*/DEBIAN/shlibs > debian/shlibs.local
	dh_gencontrol -a
	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
