#!/usr/bin/make -f

NAME      := $(shell dpkg-parsechangelog|sed -n '/^Source/{s/Source: \(.*\)/\1/p}')
VERSION   := $(shell dpkg-parsechangelog|sed -n '/^Version/{s/Version: \(.*\)-[0-9]*$$/\1/p}')
TMPDIR    := $(CURDIR)/get-orig-source-tmp
TARBALL   := $(NAME)_$(VERSION).orig.tar
DESTDIR   := $(CURDIR)/debian/instantbird
OBJDIR    := $(CURDIR)/obj-instantbird
AUTOCONF_DIRS := $(CURDIR)/mozilla/build/autoconf
MOZCONFIG := $(CURDIR)/debian/instantbird.mozconfig
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEBIAN_XUL_VER := $(shell MALLOC_OPTIONS=O xulrunner-2.0 --gre-version)

%:
	dh --builddirectory=$(OBJDIR) $@

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
        CONFIGURE_FLAGS += --disable-optimize
endif
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
        CONFIGURE_FLAGS += --enable-debug
endif
ifneq (,$(filter kfreebsd-% hurd-%,$(DEB_BUILD_ARCH)))
        CONFIGURE_FLAGS += --disable-necko-wifi
endif

ifneq (,$(filter armel powerpc, $(DEB_BUILD_ARCH)))
        CONFIGURE_FLAGS += --disable-elf-hack
endif

debian/instantbird.mozconfig: debian/instantbird.mozconfig.in
	sed -e 's/@DEBIAN_XUL_VER@/$(DEBIAN_XUL_VER)/g' < $< > $@

override_dh_auto_configure: debian/instantbird.mozconfig
	for dir in $(AUTOCONF_DIRS); do \
		for file in config.guess config.sub; do \
			sed -i '2!b;/^#/ i\exec "/usr/share/misc/'$$file'" "$$@"' $$dir/$$file; \
		done; \
	done
	MOZCONFIG=$(MOZCONFIG) CONFIGURE_ARGS="$(CONFIGURE_FLAGS) --host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE)" $(MAKE) -f client.mk configure

override_dh_auto_build:
	MOZCONFIG=$(MOZCONFIG) $(MAKE) -f client.mk build

override_dh_auto_install:
	# make
	$(MAKE) -C $(OBJDIR) package
	cp -R $(OBJDIR)/mozilla/dist/instantbird debian/tmp

	# GRE version
	sed -i 's/\(MinVersion=1.9.1\).*/\1/;s/\(MaxVersion=1.9.1\).*/\1.*/' debian/tmp/application.ini

override_dh_install:
	dh_install

	# icons
	install -d -m 755 $(DESTDIR)/usr/share/icons/hicolor/16x16/apps
	install -m 644 debian/tmp/chrome/icons/default/default16.png \
		$(DESTDIR)/usr/share/icons/hicolor/16x16/apps/instantbird.png
	install -d -m 755 $(DESTDIR)/usr/share/icons/hicolor/32x32/apps
	install -m 644 debian/tmp/chrome/icons/default/default.png \
		$(DESTDIR)/usr/share/icons/hicolor/32x32/apps/instantbird.png
	install -d -m 755 $(DESTDIR)/usr/share/icons/hicolor/48x48/apps
	install -m 644 debian/tmp/chrome/icons/default/default48.png \
		$(DESTDIR)/usr/share/icons/hicolor/48x48/apps/instantbird.png
	install -d -m 755 $(DESTDIR)/usr/share/icons/hicolor/128x128/apps
	install -m 644 debian/tmp/icons/mozicon128.png \
		$(DESTDIR)/usr/share/icons/hicolor/128x128/apps/instantbird.png
	install -d -m 755 $(DESTDIR)/usr/share/pixmaps
	convert debian/tmp/chrome/icons/default/default.png \
		$(DESTDIR)/usr/share/pixmaps/instantbird.xpm

	# fixperms
	find $(DESTDIR)/usr/share/instantbird -type f -exec chmod 644 {} \;

override_dh_installdocs:
	dh_installdocs
	cp $(CURDIR)/mozilla/LICENSE $(DESTDIR)/usr/share/doc/instantbird/MPL

override_dh_auto_clean:
	for dir in $(AUTOCONF_DIRS); do \
		for file in config.guess config.sub; do \
			sed -i '2!b;/^exec "/ d' $$dir/$$file; \
		done; \
	done

	dh_auto_clean
	cp $(CURDIR)/mozilla/build/automationutils.py $(CURDIR)/debian/automationutils.py
	-cd $(CURDIR)/mozilla && make distclean
	mv $(CURDIR)/debian/automationutils.py $(CURDIR)/mozilla/build/automationutils.py
	find $(CURDIR)/instantbird -name Makefile -delete
	find $(CURDIR)/purple -name Makefile -delete
	rm -f Makefile config/autoconf.mk config.* mozilla-config.h mozilla/config/*.pyc
	rm -f mozconfig .mozconfig.mk .mozconfig.out mozilla/build/leaktest.py
	rm -f configure mozilla/configure mozilla/js/src/configure unallmakefiles
	rm -f instantbird/app/profile/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}/install.rdf
	rm -rf mozilla/_leaktest $(OBJDIR)
	rm -f debian/instantbird.mozconfig

override_dh_auto_test:

get-orig-source:
	uscan --verbose --download-version $(VERSION) \
		--force-download --repack --no-symlink --rename
	-mkdir $(TMPDIR)
	cd $(TMPDIR) && tar -zxf ../../$(TARBALL).gz
	cd $(TMPDIR)/instantbird* && sh $(CURDIR)/debian/remove.nonfree
	cd $(TMPDIR) && tar -jcf ../../$(TARBALL).bz2 *
	rm -rf $(TMPDIR) ../$(TARBALL).gz
