# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

AB_CD = $(MOZ_UI_LOCALE)

GRE_MILESTONE = $(shell $(PYTHON) $(MOZILLA_SRCDIR)/config/printconfigsetting.py $(DIST)/bin/platform.ini Build Milestone)
MOZ_BUILDID = $(shell $(PYTHON) $(MOZILLA_SRCDIR)/config/printconfigsetting.py $(DIST)/bin/platform.ini Build BuildID)

DEFINES += \
  -DTHUNDERBIRD_ICO='"$(DIST)/branding/thunderbird.ico"' \
   $(NULL)

# Build a binary bootstrapping with XRE_main

ifndef MOZ_WINCONSOLE
ifdef MOZ_DEBUG
MOZ_WINCONSOLE = 1
else
MOZ_WINCONSOLE = 0
endif
endif

# This switches $(INSTALL) to copy mode, like $(SYSINSTALL), so things that
# shouldn't get 755 perms need $(IFLAGS1) for either way of calling nsinstall.
NSDISTMODE = copy

include $(topsrcdir)/config/config.mk

libs::
ifeq ($(OS_ARCH),WINNT)
	$(EXIT_ON_ERROR) \
	$(PERL) -pe 's/(?<!\r)\n/\r\n/g;' < $(DIST)/branding/license.txt > $(DIST)/bin/license.txt
else
	$(INSTALL) $(IFLAGS1) $(DIST)/branding/license.txt $(DIST)/bin/
endif

ifeq ($(OS_ARCH),WINNT)
# Extract the icons we care about embedding into the EXE
mailtoolbar = $(MOZILLA_SRCDIR)/$(MOZ_BUILD_APP)/themes/jumplist.png

# png to ico converter. The function takes 5 arguments, in order: source png
# file, left, top, size, output ico file.
png2ico = $(PYTHON) $(srcdir)/png2ico.py $(1) $(2) $(3) $(4) $(5)

# Each icon is 18x18 in the toolbar, and we want a 16x16 icon here, so we cut
# off a pixel at each end.
write-message.ico: $(mailtoolbar) $(srcdir)/png2ico.py
	$(call png2ico,$(mailtoolbar),0,0,16,write-message.ico)

address-book.ico: $(mailtoolbar) $(srcdir)/png2ico.py
	$(call png2ico,$(mailtoolbar),16,0,16,address-book.ico)

embedded-icons:: write-message.ico address-book.ico
GARBAGE += write-message.ico address-book.ico

# Rebuild thunderbird.exe if the manifest changes - it's included by splash.rc.
# (this dependency should really be just for thunderbird.exe, not other targets)
# Note the manifest file exists in the tree, so we use the explicit filename
# here.
EXTRA_DEPS += thunderbird.exe.manifest
endif

include $(topsrcdir)/config/rules.mk

ifeq ($(OS_ARCH),WINNT)

$(RESFILE): embedded-icons

endif

ifneq ($(OS_ARCH),WINNT)
libs::
	cp -p $(MOZ_APP_NAME)$(BIN_SUFFIX) $(DIST)/bin/$(MOZ_APP_NAME)-bin$(BIN_SUFFIX)

GARBAGE += $(addprefix $(DIST)/bin/defaults/pref/, all.js all-thunderbird.js mailnews.js)
endif # ! WinNT

ifneq (,$(filter windows,$(MOZ_WIDGET_TOOLKIT)))
ICON_SUFFIX=.ico

DESKTOP_ICONS = \
	abcardWindow \
	addressbookWindow \
	msgcomposeWindow \
	$(NULL)

BRANDED_ICONS = \
	messengerWindow \
	$(NULL)


DESKTOP_ICON_FILES = $(addsuffix $(ICON_SUFFIX), $(DESKTOP_ICONS) $(DESKTOP_ICONS_SMALL))
BRANDED_ICON_FILES = $(addsuffix $(ICON_SUFFIX), $(BRANDED_ICONS) $(BRANDED_ICONS_SMALL))

libs:: $(addprefix icons/$(MOZ_WIDGET_TOOLKIT)/,$(DESKTOP_ICON_FILES))
	$(INSTALL) $(IFLAGS1) $^ $(DIST)/bin/chrome/icons/default

libs:: $(addprefix $(DIST)/branding/,$(BRANDED_ICON_FILES))
	$(INSTALL) $(IFLAGS1) $^ $(DIST)/bin/chrome/icons/default
endif

ifneq (,$(filter gtk%,$(MOZ_WIDGET_TOOLKIT)))
libs::
	$(INSTALL) $(IFLAGS1) $(DIST)/branding/default* $(DIST)/bin/chrome/icons/default
endif

libs:: $(srcdir)/profile/prefs.js
	$(INSTALL) $(IFLAGS1) $^ $(DIST)/bin/defaults/profile

# channel-prefs.js is handled separate from other prefs due to bug 756325
libs:: $(srcdir)/profile/channel-prefs.js
	$(NSINSTALL) -D $(DIST)/bin/defaults/pref
	$(call py_action,preprocessor,-Fsubstitution $(PREF_PPFLAGS) $(ACDEFINES) $^ -o $(DIST)/bin/defaults/pref/channel-prefs.js)

ifeq ($(MOZ_PACKAGER_FORMAT),omni)
# Make extensions end up as XPIs instead of flat chrome when doing omni.jar.
# Extensions that appear in dist/bin/extensions will get bundled with the
# application, ones that appear in dist/bin/distribution/extensions will be
# copied to the profile in installed builds.
# NOTE: This is a hack to run this at the end of compilation, would be nicer
# if this was done right away for built-in extensions in omnijar mode.

ABS_STAGE = $(abspath $(STAGEDIST))

define _PACKAGE_EXTENSIONS
@echo "Packaging $(dir).xpi..."
if test -d "$(ABS_STAGE)/$(dir)"; then \
cd $(ABS_STAGE)/$(dir)/; \
$(ZIP) -Dr9mX ../$(dir).xpi * -x \*/.mkdir.done; \
cd ..; rm -rf $(ABS_STAGE)/$(dir); \
fi

endef # do not remove the blank line!

# GUIDs
# {972ce4c6-7e08-4474-a285-3208198ce6fd} Default theme
DONOTPACK = {972ce4c6-7e08-4474-a285-3208198ce6fd} {e2fda1a4-762b-4020-b5ad-a41df1933103}

pack-ext: $(STAGEDIST)
	@echo "Packaging extensions..."
	$(foreach dir,$(filter-out $(DONOTPACK),$(subst $(STAGEDIST)/,,$(wildcard $(STAGEDIST)/*))),$(_PACKAGE_EXTENSIONS))

tools::
	@$(MAKE) pack-ext STAGEDIST="$(DIST)/bin/extensions"

endif

ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))

MAC_APP_NAME = $(MOZ_APP_DISPLAYNAME)

ifdef MOZ_DEBUG
MAC_APP_NAME := $(MAC_APP_NAME)Debug
endif

LOWER_MAC_APP_NAME = $(shell echo $(MAC_APP_NAME) | tr '[A-Z]' '[a-z]')

AB_CD = $(MOZ_UI_LOCALE)

ifeq (zh-TW,$(AB_CD))
LPROJ_ROOT := $(subst -,_,$(AB_CD))
else
LPROJ_ROOT := $(firstword $(subst -, ,$(AB_CD)))
endif
LPROJ := Contents/Resources/$(LPROJ_ROOT).lproj

clean clobber repackage::
	$(RM) -r $(DIST)/$(MOZ_MACBUNDLE_NAME)

tools repackage:: $(PROGRAM)
	$(MKDIR) -p $(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/MacOS
	rsync -a --exclude '*.in' $(srcdir)/macbuild/Contents $(DIST)/$(MOZ_MACBUNDLE_NAME) --exclude English.lproj
	$(MKDIR) -p $(DIST)/$(MOZ_MACBUNDLE_NAME)/$(LPROJ)
	rsync -a --exclude '*.in' $(srcdir)/macbuild/Contents/Resources/English.lproj/ $(DIST)/$(MOZ_MACBUNDLE_NAME)/$(LPROJ)
	sed -e 's/%APP_VERSION%/$(MOZ_APP_VERSION)/' -e 's/%MAC_APP_NAME%/$(MAC_APP_NAME)/' -e 's/%LOWER_MAC_APP_NAME%/$(LOWER_MAC_APP_NAME)/' $(srcdir)/macbuild/Contents/Info.plist.in > $(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/Info.plist
	sed -e 's/%MAC_APP_NAME%/$(MAC_APP_NAME)/' $(srcdir)/macbuild/Contents/Resources/English.lproj/InfoPlist.strings.in | iconv -f UTF-8 -t UTF-16 > $(DIST)/$(MOZ_MACBUNDLE_NAME)/$(LPROJ)/InfoPlist.strings
	rsync -a --exclude-from='$(srcdir)/macbuild/Contents/MacOS-files.in' $(DIST)/bin/ $(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/Resources
	rsync -a --include-from='$(srcdir)/macbuild/Contents/MacOS-files.in' --exclude '*' $(DIST)/bin/ $(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/MacOS
	# MacOS-files-copy.in is a list of files that should be copies rather
	# than symlinks and placed in .app/Contents/MacOS.
	rsync -aL --include-from='$(srcdir)/macbuild/Contents/MacOS-files-copy.in' --exclude '*' $(DIST)/bin/ '$(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/MacOS'
	$(RM) $(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/MacOS/$(PROGRAM)
	rsync -aL $(PROGRAM) $(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/MacOS
	cp -RL $(DIST)/branding/thunderbird.icns $(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/Resources/thunderbird.icns
	printf APPLMOZM > $(DIST)/$(MOZ_MACBUNDLE_NAME)/Contents/PkgInfo
endif

# Note that anything you do to dist/ down here isn't going to make it into the
# Mac build, since it's already been copied over to the .app, above.
