#!/usr/bin/make -f
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_ARCH  ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

CFLAGS := -g
MAIN_LDFLAGS := -Wl,--as-needed

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_FLAGS += --disable-optimize
endif

ifeq ($(DEB_BUILD_ARCH),ppc64)
	CFLAGS += -mminimal-toc
endif

ifneq (,$(filter kfreebsd-% hurd-%,$(DEB_BUILD_ARCH)))
	CONFIGURE_FLAGS += --disable-necko-wifi
endif

ifneq (,$(filter armel,$(DEB_BUILD_ARCH)))
	CONFIGURE_FLAGS += --disable-methodjit
	CFLAGS += -D__ARM_PCS
endif

ifneq (,$(filter ia64,$(DEB_BUILD_ARCH)))
	CONFIGURE_FLAGS += --enable-optimize=-O2
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_FLAGS += --enable-debug
endif

TMP_DIR  = $(CURDIR)/debian/tmp
APPLICATIONS_DIR = $(TMP_DIR)/usr/share/applications
DIST = $(CURDIR)/build-iceape/mozilla/dist
AUTOCONF_DIRS = mozilla/build/autoconf directory/c-sdk/config/autoconf mozilla/js/src/build/autoconf

ifndef CXX
CXX := g++
endif
CXXFLAGS := $(CFLAGS)

override_dh_auto_configure:
	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

	$(if $(wildcard build-iceape),,mkdir build-iceape)
	cd build-iceape && \
	MOZCONFIG=$(CURDIR)/debian/mozconfig \
	ASFLAGS="-g" \
	CFLAGS="$(CFLAGS)" \
	CXXFLAGS="$(CXXFLAGS)" \
	../configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr $(CONFIGURE_FLAGS)
	cd build-iceape && ../mozilla/build/autoconf/make-makefile -t .. debian/extra-stuff
	sed -i '/^OS_LDFLAGS/s|=|& $(MAIN_LDFLAGS)|' $$(find build-iceape -name autoconf.mk)

	#cp -f debian/localstart.html debian/README.Debian $(CURDIR)/xpfe/global/resources/content/
	#cp -f debian/mailstart.xhtml $(CURDIR)/mailnews/base/resources/content/start.xhtml

check-system-libs:
# Ensure --{with,enable}-system options properly set expected variables
# according to the definitions in the mozconfig file.
	@awk -F' *# *| *$$' ' \
	BEGIN { "find $(CURDIR)/build-iceape/mozilla -name autoconf.mk -printf \"%p \"" | getline confs } \
	$$1 ~ /system/ { \
		if (! $$2) { \
			print FILENAME ": Missing variable for",$$1; \
			error=1; \
		} else { \
			split($$2,var,"="); \
			cmd = "grep -l " var[1] " " confs; \
			cmd | getline dir; \
			sub(/\/[^\/]*$$/, "", dir); \
			cmd = "$(MAKE) -C " dir " --no-print-directory echo-variable-" var[1]; \
			cmd | getline value; \
			if (value != var[2]) { print $$1, "triggered", var[1] "=" value,"instead of",$$2; error=1 } \
		} \
	} \
	END { if (error) { exit 1 }}' debian/mozconfig

override_dh_auto_build: check-system-libs
	# icons
	cp -f $(CURDIR)/debian/extras/Throbber-small.gif \
		$(CURDIR)/suite/themes/classic/communicator/brand/throbber-anim.png
	cp -f $(CURDIR)/debian/extras/Throbber-small.png \
		$(CURDIR)/suite/themes/classic/communicator/brand/throbber-single.png
	cp -f $(CURDIR)/debian/extras/Throbber-small.gif \
		$(CURDIR)/suite/themes/classic/communicator/brand/throbber16-anim.png
	cp -f $(CURDIR)/debian/extras/Throbber-small.png \
		$(CURDIR)/suite/themes/classic/communicator/brand/throbber16-single.png

	cp -f $(CURDIR)/debian/extras/Throbber-small.gif \
		$(CURDIR)/suite/themes/modern/communicator/brand/throbber-anim.png
	cp -f $(CURDIR)/debian/extras/Throbber-small.png \
		$(CURDIR)/suite/themes/modern/communicator/brand/throbber-single.png
	cp -f $(CURDIR)/debian/extras/Throbber-small.gif \
		$(CURDIR)/suite/themes/modern/communicator/brand/throbber16-anim.png
	cp -f $(CURDIR)/debian/extras/Throbber-small.png \
		$(CURDIR)/suite/themes/modern/communicator/brand/throbber16-single.png

	cp -f $(CURDIR)/debian/extras/preview.png \
		$(CURDIR)/suite/themes/classic/preview.png

	# full build
	dh_auto_build --parallel
	$(MAKE) -C build-iceape/debian/extra-stuff

override_dh_auto_clean:
	rm -f build-stamp install-stamp MPL
	rm -f $(CURDIR)/suite/themes/classic/communicator/brand/throbber-anim.png \
		$(CURDIR)/suite/themes/classic/communicator/brand/throbber16-anim.png \
		$(CURDIR)/suite/themes/classic/communicator/brand/throbber-single.png \
		$(CURDIR)/suite/themes/classic/communicator/brand/throbber16-single.png
	rm -f $(CURDIR)/suite/themes/modern/communicator/brand/throbber-anim.png \
		$(CURDIR)/suite/themes/modern/communicator/brand/throbber16-anim.png \
		$(CURDIR)/suite/themes/modern/communicator/brand/throbber-single.png \
		$(CURDIR)/suite/themes/modern/communicator/brand/throbber16-single.png
	rm -f $(CURDIR)/suite/themes/classic/preview.png

	dh_auto_clean --parallel

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

NO_PKG_FILES = \
	 $(shell $(MAKE) --no-print-directory -C build-iceape/suite/installer echo-variable-NO_PKG_FILES) \
	 $(shell sed /^\#/d debian/no_pkg_files)

override_dh_auto_install:
	$(MAKE) -C build-iceape/suite/installer package-manifest
	cat debian/pkg_files >> build-iceape/suite/installer/package-manifest
	dh_auto_install -- MOZ_PKG_REMOVALS= NO_PKG_FILES="$(NO_PKG_FILES)"
	$(MAKE) -C build-iceape/debian/branding install DESTDIR=$(TMP_DIR)

	install -d -m 755 $(TMP_DIR)/usr/share/pixmaps
	convert $(TMP_DIR)/usr/lib/iceape/chrome/icons/default/main-window.png $(TMP_DIR)/usr/share/pixmaps/iceape.xpm
	convert $(TMP_DIR)/usr/lib/iceape/chrome/icons/default/editorWindow.png $(TMP_DIR)/usr/share/pixmaps/iceape-composer.xpm
	install -m 644 $(CURDIR)/build-iceape/mozilla/dist/xpi-stage/chatzilla/chrome/icons/default/chatzilla-window.xpm $(TMP_DIR)/usr/share/pixmaps/chatzilla.xpm
	convert $(TMP_DIR)/usr/lib/iceape/chrome/icons/default/addressbookWindow.png $(TMP_DIR)/usr/share/pixmaps/iceape-addressbook.xpm
	convert $(TMP_DIR)/usr/lib/iceape/chrome/icons/default/messengerWindow.png $(TMP_DIR)/usr/share/pixmaps/iceape-mailnews.xpm
	convert $(TMP_DIR)/usr/lib/iceape/chrome/icons/default/msgcomposeWindow.png $(TMP_DIR)/usr/share/pixmaps/iceape-mail-compose.xpm

	install -d -m 755 $(TMP_DIR)/usr/share/applications
	install -m 644 $(CURDIR)/debian/menu_dir/*.desktop $(TMP_DIR)/usr/share/applications

	install -m 644 $(CURDIR)/debian/extras/*.src $(CURDIR)/debian/extras/*.gif $(TMP_DIR)/usr/lib/iceape/searchplugins

	install -d -m 755 $(TMP_DIR)/usr/sbin
	install -m 755 $(CURDIR)/debian/iceape-runner $(TMP_DIR)/usr/lib/iceape/iceape

	install -d -m 755 $(TMP_DIR)/etc/iceape/pref
	install -m 644 $(CURDIR)/debian/base.js $(TMP_DIR)/etc/iceape/pref
	install -m 644 $(CURDIR)/debian/iceaperc $(TMP_DIR)/etc/iceape

	install -m 644 $(CURDIR)/debian/vendor.js $(TMP_DIR)/usr/lib/iceape/defaults/pref

	cp -f mozilla/LICENSE MPL

override_dh_installdocs:
	dh_installdocs -A MPL

override_dh_install:
	dh_install $(if $(filter -i -a,$(DH_INTERNAL_OPTIONS)),--list-missing,--fail-missing)

override_dh_strip:
	dh_strip -a --dbg-package=iceape-dbg

override_dh_makeshlibs:

override_dh_shlibdeps:
	dh_shlibdeps -a -l $(TMP_DIR)/usr/lib/iceape -Xlibmozgnome -Xlibnkgnomevfs -Xlibdbusservice
	LD_LIBRARY_PATH=$(TMP_DIR)/usr/lib/iceape dpkg-shlibdeps -Tdebian/iceape.substvars -dDepends -pgnome $(foreach lib,dbusservice mozgnome nkgnomevfs,debian/iceape/usr/lib/iceape/components/lib$(lib).so)

override_dh_bugfiles:
	dh_bugfiles -A

debian/patches:
ifdef REVLIST
	rm -rf $@
	mkdir $@
	cd $@ && git format-patch -k -N $(REVLIST) > series
	sed -i '1d;:a;$$d;N;3,4ba;P;D' $@/*.patch
else
	@echo You must define the REVLIST variable to a git revisions list
endif

binary binary-arch binary-indep build clean install:
	dh --builddirectory=build-iceape $@

.PHONY: build clean binary-indep binary-arch binary install source debian/patches check-system-libs
