#!/usr/bin/make -f
# -*- makefile -*-

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

include /usr/share/quilt/quilt.make

DEB_CONFIGURE_EXTRA_FLAGS := --enable-odbc --enable-pam
# Not sure if we should enable workaround for gateway subscription
# Adding it breaks XMPP compatibility
# Not adding breaks roster import for some gateways (specifically JIT)
#DEB_CONFIGURE_EXTRA_FLAGS := --enable-odbc --enable-roster-gateway-workaround


AUTO_OPTS = --sourcedirectory=src

DESTDIR=$(CURDIR)/debian/ejabberd

ifndef HOME
	export HOME="$(CURDIR)"
endif

configure: configure-stamp
configure-stamp: $(QUILT_STAMPFN)
	dh_testdir
	dh_autotools-dev_updateconfig
	dh_auto_configure $(AUTO_OPTS) -- $(DEB_CONFIGURE_EXTRA_FLAGS) \
		$(shell dpkg-buildflags --export=configure)

	touch configure-stamp


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

build-stamp: configure-stamp  
	dh_testdir

	dh_auto_build $(AUTO_OPTS)

	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	dh_auto_clean $(AUTO_OPTS)
	dh_autotools-dev_restoreconfig
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep  
	dh_installdirs

	dh_auto_install $(AUTO_OPTS)

	# Remove unused upstream config files:
	rm $(DESTDIR)/etc/ejabberd/ejabberd.cfg
	rm $(DESTDIR)/etc/ejabberd/ejabberdctl.cfg

	# Remove unused upstream directories:
	rm -rf $(DESTDIR)/var/lock

	# Remove upstream license file:
	rm $(DESTDIR)/usr/share/doc/ejabberd/COPYING

	# Remove autogenerated init-script:
	rm $(CURDIR)/src/ejabberd.init


# Build architecture-independent files here.
binary-indep: install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_install
#	dh_installmenu
	dh_installdebconf
	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_python
	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_makeshlibs
	erlang-depends
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

get-orig-source:
	dh_testdir
	wget -O ../ejabberd_$(DEB_UPSTREAM_VERSION).orig.tar.gz \
		http://www.process-one.net/en/projects/ejabberd/download/$(DEB_UPSTREAM_VERSION)/ejabberd-$(DEB_UPSTREAM_VERSION).tar.gz

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure get-orig-source

