#!/usr/bin/make -f

#export DH_VERBOSE=1
export DH_OPTIONS

mu := $(CURDIR)/debian/maildir-utils
mu4e := $(CURDIR)/debian/mu4e


# ia64 cannot build guile-2.0, so we have to disable it
# one part is in the control file removing the build-deps on libguile-2.0-dev
# for ia64, one part is here to disable guile compiling and some rules
# same for armel

DOGUILECONFIG=
BAD_GUILE_ARCHS := ia64 armel
DEB_HOST_ARCH     ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifneq ($(filter $(DEB_HOST_ARCH),$(BAD_GUILE_ARCHS)),)
  DOGUILECONFIG=--disable-guile
endif

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

CFLAGS += -g -Wall

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp:
	dh_testdir
	dh_autoreconf
	CXXFLAGS="$(CXXFLAGS)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		./configure --prefix=/usr --docdir='$${prefix}/share/doc/maildir-utils' --disable-webkit $(DOGUILECONFIG)
	$(MAKE)
	touch $@

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_autoreconf_clean
	dh_clean install-stamp build-stamp config.log

install: install-stamp

install-stamp: DH_OPTIONS=
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) install prefix=$(mu)/usr
	# remove wrong info/dir
	rm -f $(mu)/usr/share/info/dir*
	# mu4e package
	mkdir -p $(mu4e)/usr/share
	mv $(mu)/usr/share/emacs $(mu4e)/usr/share
	mkdir -p $(mu4e)/usr/share/info
	mv $(mu)/usr/share/info/mu4e.info* $(mu4e)/usr/share/info
	# shared libs dealing
ifeq ($(filter $(DEB_HOST_ARCH),$(BAD_GUILE_ARCHS)),)
	rm $(mu)/usr/lib/libguile-mu.la
endif
	touch install-stamp

binary-indep: install
	dh_testdir
	dh_testroot
	dh_makeshlibs
	dh_shlibdeps
	dh_installdocs README NEWS NEWS.org TODO HACKING
	dh_installchangelogs
	dh_installemacsen -pmu4e
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: DH_OPTIONS=-a
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installdocs README NEWS NEWS.org TODO HACKING
	#dh_installchangelogs ChangeLog
	dh_installchangelogs
ifeq "$(findstring nostrip,$(DEB_BUILD_OPTIONS))" ""
	dh_strip
endif
	dh_compress -XNEWS.org
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

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