#!/usr/bin/make -f
export DH_VERBOSE=1

MAKEFILE = $(firstword $(MAKEFILE_LIST))
DEBIAN_DIR = $(dir $(MAKEFILE))
SOURCE_DIR = $(DEBIAN_DIR)/..

DEB_VERSION = $(shell dpkg-parsechangelog | grep ^Vers | cut -d\  -f2)
DEB_SOURCE_NAME = $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)/changelog | grep ^Source | cut -d" " -f2)
VERSION = $(shell echo $(DEB_VERSION) | sed 's,-.*,,' | sed 's,+dfsg.*,,')
DEB_CLI_ABI_VERSION = 2.4
DEB_CLI_API_VERSION = 2.4.4
DEB_C_API_VERSION = 2.4.11

-include /usr/share/cli-common/cli.make
include /usr/share/quilt/quilt.make
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk

autoreconf: autoreconf-stamp
autoreconf-stamp: $(QUILT_STAMPFN)
	autoreconf -f -i -s
	touch $@

override_dh_auto_configure: autoreconf-stamp
	dh_auto_configure -- \
		--enable-gtk-doc \
		--disable-rpath \
		--enable-largefile \
		CSC=/usr/bin/mono-csc

override_dh_auto_build: 
	dh_auto_build

	if [ -f $(CURDIR)/mono/gmime-sharp.dll ]; then \
	  # create API docs \
	  mdoc update \
	    -o $(CURDIR)/mono/monodocer \
	    $(CURDIR)/mono/gmime-sharp.dll; \
	  mdoc assemble \
	    -o $(CURDIR)/mono/gmime-sharp-2.4 \
	    $(CURDIR)/mono/monodocer; \
	fi

override_dh_auto_install:
	dh_auto_install

	# don't ship those, sharutils does already
	rm $(CURDIR)/debian/tmp/usr/bin/uudecode
	rm $(CURDIR)/debian/tmp/usr/bin/uuencode

override_dh_makeclilibs:
	dh_makeclilibs -m $(DEB_CLI_API_VERSION)

override_dh_makeshlibs:
	dh_makeshlibs
	dh_makeshlibs -p libgmime-2.4-2 -V 'libgmime-2.4-2 (>= $(DEB_C_API_VERSION))'

override_dh_clean: unpatch
	dh_clean
	
	# clean up our API docs
	rm -rf	$(CURDIR)/mono/monodocer \
		$(CURDIR)/mono/gmime-sharp-2.4.tree \
		$(CURDIR)/mono/gmime-sharp-2.4.zip

get-orig-source:
	uscan \
		--package $(DEB_SOURCE_NAME) \
		--watchfile $(DEBIAN_DIR)/watch \
		--upstream-version $(VERSION) \
		--download-version $(VERSION) \
		--destdir . \
		--force-download \
		--rename \
		--repack

clean::
	dh $@
	dh_clean

build:
	dh $@

%:
	dh $@

.PHONY: autoreconf configure build clean get-orig-source
