#!/usr/bin/make -f

COMM_REPO	:=	http://hg.mozilla.org/releases/comm-beta
XUL_SDK_DIR	:=	/usr/lib/thunderbird-devel
XPI_STAGE	:=	mozilla/dist/xpi-stage
XPI_FILES = \
	lightning \
	calendar-timezones \
	gdata-provider \
	$(NULL)

DEB_CONFIGURE_USER_FLAGS = 				\
	--enable-application=calendar			\
	--with-libxul-sdk=$(XUL_SDK_DIR)		\
	--with-system-libxul				\
	--disable-strip					\
	--disable-strip-libs				\
	--enable-optimize				\
	--disable-debug 				\
	--enable-official-branding			\
	--disable-sunbird				\
	--disable-ogg					\
	--disable-wave					\
	--disable-dbus					\
	--disable-libnotify				\
	--disable-necko-wifi				\
	--disable-crashreporter				\
    	--disable-webm 					\
    	--disable-libjpeg-turbo 			\
    	--enable-chrome-format=jar 			\
    	--enable-libxul 				\
	--disable-tests					\
	--disable-elf-hack				\
	$(NULL)

%:
	dh $@ --with xul-ext

debian/stamp-autotools-moz:
	if [ ! -e $(CURDIR)/configure ]; then \
		`which autoconf2.13`; \
	fi
	if [ ! -e $(CURDIR)/mozilla/configure ]; then \
		cd mozilla; `which autoconf2.13`; \
	fi
	touch $@

override_dh_auto_configure: debian/stamp-autotools-moz
	dh_auto_configure -- $(DEB_CONFIGURE_USER_FLAGS)

override_dh_auto_install:
	$(foreach xpi,$(XPI_FILES),install-xpi -p xul-ext-$(xpi) $(XPI_STAGE)/$(xpi).xpi;)

override_dh_auto_clean:
	rm -f debian/stamp-autotools-moz
	dh_auto_clean

# Below here for building the source tarball
DIRS = \
	bridge \
	build \
	calendar \
	config \
	mozilla/browser/config \
	mozilla/build \
	mozilla/config \
	mozilla/mfbt \
	mozilla/other-licenses/ply/ply \
	mozilla/other-licenses/simplejson-2.1.1 \
	mozilla/other-licenses/virtualenv \
	mozilla/testing/mozbase \
	mozilla/toolkit/mozapps/installer \
	mozilla/xpcom/idl-parser \
	mozilla/xpcom/typelib/xpt/tools \
	other-licenses/branding/sunbird \
	$(NULL)

FILES = \
	aclocal.m4 \
	allmakefiles.sh \
	configure.in \
	db/mork/Makefile.in \
	ldap/sdks/c-sdk/Makefile.in \
	ldap/xpcom/Makefile.in \
	mail/config/version.txt \
	mailnews/makefiles.sh \
	mailnews/Makefile.in \
	mailnews/base/Makefile.in \
	mailnews/mime/public/Makefile.in \
	Makefile.in \
	mozilla/aclocal.m4 \
	mozilla/allmakefiles.sh \
	mozilla/configure.in \
	mozilla/extensions/build.mk \
	mozilla/extensions/Makefile.in \
	mozilla/ipc/app/defs.mk \
	mozilla/LEGAL \
	mozilla/LICENSE \
	mozilla/Makefile.in \
	mozilla/netwerk/necko-config.h.in \
	mozilla/nsprpub/config/make-system-wrappers.pl \
	mozilla/probes/Makefile.in \
	mozilla/probes/mozilla-trace.d \
	mozilla/services/aitc/Makefile.in \
	mozilla/services/common/Makefile.in \
	mozilla/services/crypto/component/Makefile.in \
	mozilla/services/crypto/Makefile.in \
	mozilla/services/makefiles.sh \
	mozilla/services/Makefile.in \
	mozilla/services/notifications/Makefile.in \
	mozilla/services/sync/locales/Makefile.in \
	mozilla/services/sync/Makefile.in \
	mozilla/services/sync/tests/Makefile.in \
	mozilla/testing/testsuite-targets.mk \
	mozilla/toolkit/locales/l10n.mk \
	mozilla/toolkit/xre/make-platformini.py \
	mozilla/xpcom/xpcom-config.h.in \
	mozilla/xpcom/xpcom-private.h.in \
	mozilla/xpfe/components/autocomplete \
	suite/config/version.txt \
	$(NULL)

EXCLUDE = \
	*.hg* \
	*mozilla/config/*.exe \
	*mozilla/config/*.pyc \
	$(NULL)

$(foreach path, $(EXCLUDE), $(eval EXCLUDE_PARAMS += --exclude $(path)))

ifdef DEBIAN_TAG
REV_ARGS	:= --comm-rev=$(DEBIAN_TAG) --mozilla-rev=$(DEBIAN_TAG)
endif

ifdef LOCAL_BRANCH
COMM_REPO	:= $(shell echo $(LOCAL_BRANCH) | sed -n 's/\/\?$$//p')
COMM_REPO_NAME	:= $(notdir $(COMM_REPO))
MOZ_REPO	:= --mozilla-repo=$(dir $(COMM_REPO))/$(shell echo $(COMM_REPO_NAME) | sed -n 's/comm/mozilla/p')
endif

get-orig-source:
	rm -rf tmp
	mkdir tmp
	hg clone $(COMM_REPO) tmp/lightning-extension
	cd tmp/lightning-extension ; \
		python client.py checkout $(REV_ARGS) $(MOZ_REPO)
	cd tmp ; \
		if [ -z $(VERSION) ] ; then \
			ver=`cat $(CURDIR)/tmp/lightning-extension/calendar/sunbird/config/version.txt` ; \
		else \
			ver=$(VERSION) ; \
		fi ; \
		mv lightning-extension lightning-extension-$$ver ; \
		tar -jvc $(EXCLUDE_PARAMS) -f lightning-extension_$$ver.orig.tar.bz2 $(addprefix lightning-extension-$$ver/, $(DIRS) $(FILES))
	mv tmp/*.bz2 ../ && rm -rf tmp
