#!/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),alpha)
	MAIN_LDFLAGS += -Wl,--no-relax
endif

ifeq ($(DEB_BUILD_ARCH),ppc64)
	CFLAGS += -mminimal-toc
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)/mozilla/dist
AUTOCONF_DIRS = mozilla/build/autoconf directory/c-sdk/config/autoconf mozilla/js/src/build/autoconf

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

	MOZCONFIG=debian/mozconfig \
	ASFLAGS="-g" \
	CFLAGS="$(CFLAGS)" \
	CXXFLAGS="$(CFLAGS)" \
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr $(CONFIGURE_FLAGS)
	mozilla/build/autoconf/make-makefile -t . debian/extra-stuff
	sed -i '/^OS_LDFLAGS/s|=|& $(MAIN_LDFLAGS)|' config/autoconf.mk mozilla/config/autoconf.mk mozilla/js/src/config/autoconf.mk directory/c-sdk/config/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

override_dh_auto_build:
	# 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
	$(MAKE) -C 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

	[ ! -f config/autoconf.mk ] || $(MAKE) -C debian/extra-stuff distclean
	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

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

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

# Install nspr and nss links
	for lib in ssl3 softokn3 smime3 nss3 nspr4 plc4 plds4; do \
		[ -f /usr/lib/lib$$lib.so ] && libpath=/usr/lib; \
		[ -f /usr/lib/nss/lib$$lib.so ] && libpath=/usr/lib/nss; \
		ln -s $$libpath/$$(objdump -p $$libpath/lib$$lib.so | awk '/SONAME/{print $$2}') debian/tmp/usr/lib/iceape/lib$$lib.so; \
	done
	ln -s /usr/lib/nss/libnssckbi.so debian/tmp/usr/lib/iceape/

	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 $(TMP_DIR)/usr/lib/iceape/extensions/{59c81df5-4b7a-477b-912d-4e0fdf64e5f2}/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 -m 644 $(DIST)/bin/.autoreg $(TMP_DIR)/usr/lib/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-browser.substvars -dRecommends -piceape $(foreach lib,dbusservice mozgnome nkgnomevfs,debian/iceape-browser/usr/lib/iceape/components/lib$(lib).so)

override_dh_bugfiles:
	dh_bugfiles -A

source: clean
	sh $(CURDIR)/debian/remove.nonfree
	tar -C .. -jcf ../`dpkg-parsechangelog | sed -n '/Source:/ N; s/Source: \(.*\)\nVersion: \(.*:\|\)\(.*\)-[0-9][0-9]*$$/\1_\3/ p'`.orig.tar.bz2 --exclude=`basename $(CURDIR)`/debian `basename $(CURDIR)`

debian/patches:
ifdef REVLIST
	rm -rf $@
	mkdir $@
	cd $@ && git format-patch -k -N $(REVLIST) > series
else
	@echo You must define the REVLIST variable to a git revisions list
endif

binary binary-arch binary-indep build clean install:
	dh $@

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