#!/usr/bin/make -f

DEB_SOURCE := $(shell dpkg-parsechangelog | grep Source: | sed -e 's/Source: //')
DEB_VERSION := $(shell dpkg-parsechangelog | grep Version: | sed -e 's/Version: //')
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | sed -e 's/-[^-]*$$//')

include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/autoreconf.mk

# Optimise the linking and only depend on directly required libraries
DEB_DH_AUTORECONF_ARGS = $(CURDIR)/$(DEB_SRCDIR)/autogen.sh
DEB_DH_MAKESHLIBS_ARGS_ibus-clutter := -Xim-ibus
DEB_CONFIGURE_EXTRA_FLAGS += --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)\

# tighten libglib2.0-0 dependency due to glib_check_version() usage (#985453)
DEB_LIBGLIB20=$(shell dpkg-query -f '$${package}\n' -W 'libglib2.0-0*' | grep -v dbg | tail -1)
DEB_DH_GENCONTROL_ARGS_ibus-clutter = -- -V'glib:Depends=$(shell dpkg-query -f '$${package} (>= $${source:Upstream-Version})' -W $(DEB_LIBGLIB20))'

GIT_URL = git://git.moblin.org/ibus-client-clutter

clean::
	rm -rf autom4te.cache

binary-post-install/ibus-clutter::
	rm -f debian/ibus-clutter/usr/lib/$(DEB_HOST_MULTIARCH)/clutter-imcontext/immodules/*.la
	rm -f debian/ibus-clutter/usr/lib/$(DEB_HOST_MULTIARCH)/clutter-imcontext/immodules/*.a

get-orig-source::
	set -e; if echo $(DEB_VERSION) | grep -c "git"; \
	then \
		git_version=`echo $(DEB_VERSION) | sed -e 's/^.*git\([0-9]*\)*\.\(.*\)-.*$$/\2/g'`; \
	else \
		git_version=$(DEB_UPSTREAM_VERSION); \
	fi; \
	tmpdir=`mktemp -d -t`; \
	cd $$tmpdir; \
	echo "checkout upstream repository ..."; \
	git clone $(GIT_URL); echo "getting specific upstream revision/tag: $$git_version"; \
	cd `ls | head -n 1`; git checkout -b orig $$git_version; cd ..; \
	tar --exclude=.git -czvf $(CURDIR)/$(DEB_SOURCE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz `ls | head -n 1`; \
	cd $(CURDIR); \
	rm -rf $$tmpdir
