#!/usr/bin/make -f

MOZ_PKG_BASENAME	:= icecat

include $(CURDIR)/debian/build/config.mk

MOZ_APP			:= browser
MOZ_VENDOR		:= Mozilla
MOZ_MOZDIR		:=

MOZ_APP_SUBDIR	:= browser

MOZ_BRANDING		:= $(CHANNEL)
ifneq (,$(filter release beta, $(MOZ_BRANDING)))
MOZ_BRANDING		:= official
endif
ifeq (1,$(MOZ_FORCE_UNOFFICIAL_BRANDING))
ifneq (,$(filter official aurora, $(MOZ_BRANDING)))
MOZ_BRANDING		:= unofficial
endif
endif
MOZ_BRANDING_DIR 	:= $(MOZ_APP)/branding/$(MOZ_BRANDING)
ifeq (official,$(BRANDING))
MOZ_BRANDING_OPTION	:= --enable-official-branding
else
MOZ_BRANDING_OPTION	:= --with-branding=$(MOZ_BRANDING_DIR)
endif

MOZ_PKGNAME_SUBST_FILES = \
	debian/usr.bin.$(MOZ_PKG_NAME) \
	debian/README.Debian \
	debian/$(MOZ_PKG_NAME)-locale.preinst \
	debian/$(MOZ_PKG_BASENAME).sh \
	debian/apport/blacklist \
	debian/apport/native-origins \
	debian/apport/source_$(MOZ_PKG_NAME).py \
	debian/testing/run_mochitest \
	debian/testing/run_reftest \
	$(NULL)

MOZ_APPNAME_SUBST_FILES = \
	debian/$(MOZ_APP_NAME).1 \
	$(NULL)

MOZ_PKGCONFIG_FILES = \
	debian/pkgconfig/mozilla-plugin.pc \
	$(NULL)

include $(CURDIR)/debian/build/rules.mk

#MOZ_EXECUTABLES_$(MOZ_PKG_NAME)-testsuite =	$(MOZ_LIBDIR)/testing/run_mochitest \
						$(MOZ_LIBDIR)/testing/run_reftest \
						$(MOZ_LIBDIR)/testing/run_xpcshell_tests \
						$(MOZ_LIBDIR)/xpcshell \
						$(MOZ_LIBDIR)/testing/filter_results \
						$(NULL)

debian/usr.bin.icecat.in:
	if [ '$(DISTRIB_VERSION_MAJOR)$(DISTRIB_VERSION_MINOR)' -ge '1410' ]; then \
		cp $(CURDIR)/debian/usr.bin.icecat.apparmor.14.10 $(CURDIR)/debian/usr.bin.icecat.in ; \
	elif [ '$(DISTRIB_VERSION_MAJOR)$(DISTRIB_VERSION_MINOR)' -ge '1310' ]; then \
		cp $(CURDIR)/debian/usr.bin.icecat.apparmor.13.10 $(CURDIR)/debian/usr.bin.icecat.in ; \
	else \
		cp $(CURDIR)/debian/usr.bin.icecat.apparmor.12.04 $(CURDIR)/debian/usr.bin.icecat.in ; \
	fi

ifeq (icecat, $(MOZ_APP_NAME))
ifneq (,$(filter precise, $(DISTRIB_CODENAME)))
PLUGIN_BREAKS := flashplugin-installer (<= 11.1.102.63ubuntu1), adobe-flashplugin (<= 11.1.102.63-0precise1)
PLUGIN_CONFLICTS := gcu-plugin (<= 0.12.10-1ubuntu1)
endif
endif

WRITE_SUBSTVARS = $(shell echo "$(2)=$(3)" | sed 's/[ \t\n]\+/ /g' >> debian/$(1).substvars)

ifeq (icecat, $(MOZ_PKG_NAME))
ifeq (,$(filter precise, $(DISTRIB_CODENAME)))
install/icecat::
	$(call WRITE_SUBSTVARS,icecat,transitional:Replaces,kubuntu-icecat-installer)
else
ICECAT_REPLACES = abrowser, abrowser-branding, icecat-branding, kubuntu-icecat-installer
ICECAT_BREAKS = abrowser (<= 4.0~b11+build3+nobinonly-0ubuntu1), abrowser-branding (<= 4.0~b11+build3+nobinonly-0ubuntu1), \
		 icecat-branding (<= 4.0~b11+build3+nobinonly-0ubuntu1), $(PLUGIN_BREAKS)
install/icecat::
	$(call WRITE_SUBSTVARS,icecat,transitional:Replaces,$(ICECAT_REPLACES))
	$(call WRITE_SUBSTVARS,icecat,transitional:Breaks,$(ICECAT_BREAKS))
	$(call WRITE_SUBSTVARS,icecat,transitional:Conflicts,$(PLUGIN_CONFLICTS))
endif
endif

binary-post-install/$(MOZ_PKG_NAME)::
	rm -f debian/$(MOZ_PKG_NAME)/$(MOZ_LIBDIR)/distribution/searchplugins/locale/en-US/ddg.xml

$(patsubst %,binary-post-install/%,$(MOZ_LOCALE_PKGS)):: binary-post-install/%: install-webapprt-langpack-links-%
	rm -f debian/$*/$(MOZ_LIBDIR)/distribution/searchplugins/locale/*/ddg.xml

install-webapprt-langpack-links-%: XPIS = $(notdir $(wildcard debian/$*/$(MOZ_ADDONDIR)/extensions/*.xpi))
install-webapprt-langpack-links-%:
	$(foreach xpi,$(XPIS),dh_link -p$* $(MOZ_ADDONDIR)/extensions/$(xpi) $(MOZ_LIBDIR)/webapprt/extensions/$(xpi);)

make-langpack-preinsts:: $(foreach langpack,$(MOZ_LOCALE_PKGS),debian/$(langpack).preinst)
$(patsubst %,debian/%.preinst,$(MOZ_LOCALE_PKGS)) :: debian/%.preinst : debian/$(MOZ_PKG_NAME)-locale.preinst
	cp $< $@

pre-build:: make-langpack-preinsts

clean::
	rm -f debian/$(MOZ_PKG_NAME)-locale-*.preinst
	rm -f debian/usr.bin.icecat.in

.PHONY: make-langpack-preinsts
