# 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/.

dist_dest   = $(DIST)/$(MOZ_MACBUNDLE_NAME)

AB_CD = $(MOZ_UI_LOCALE)

LICENSE_TXT_FILE = $(topsrcdir)/comm/suite/installer/license.txt

# Build a binary bootstrapping with XRE_main

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

include $(topsrcdir)/config/config.mk

# If we are trying to show an error dialog about the lack of SSE2 support,
# make sure that code itself doesn't use SSE2.
ifdef MOZ_LINUX_32_SSE2_STARTUP_ERROR
CXX := $(filter-out -march=% -msse -msse2 -mfpmath=sse,$(CXX))
CXX += -march=pentiumpro
endif

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

include $(topsrcdir)/config/rules.mk

# 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 channel-prefs.js,-Fsubstitution $(PREF_PPFLAGS) $(ACDEFINES) $^ -o $(DIST)/bin/defaults/pref/channel-prefs.js)

libs:: $(LICENSE_TXT_FILE)
ifeq ($(OS_ARCH),WINNT)
	$(EXIT_ON_ERROR) \
	perl -pe 's/(?<!\r)\n/\r\n/g;' < $^ > $(DIST)/bin/license.txt

else
	$(INSTALL) $(IFLAGS1) $^ $(DIST)/bin/
endif

ifneq ($(OS_ARCH),WINNT)

ifdef COMPILE_ENVIRONMENT
ifndef MOZ_NO_PIE_COMPAT
libs::
	cp -p $(DIST)/bin/$(MOZ_APP_NAME)$(BIN_SUFFIX) $(DIST)/bin/$(MOZ_APP_NAME)-bin$(BIN_SUFFIX)
endif
endif
endif

ifneq (,$(filter-out WINNT Darwin,$(OS_ARCH)))
$(MOZ_APP_NAME).1: $(srcdir)/seamonkey.man.in $(GLOBAL_DEPS) $(DEPTH)/config/autoconf.mk
	@sed -e "s|\@bindir\@|$(bindir)|g" -e "s|\@mozappdir\@|$(mozappdir)|g" \
		-e "s|\@MOZ_APP_DISPLAYNAME\@|$(MOZ_APP_DISPLAYNAME)|g" \
		-e "s|\@MOZ_APP_NAME\@|$(MOZ_APP_NAME)|g" \
		-e "s|\@MOZ_APP_VERSION\@|${MOZ_APP_VERSION}|g" < $< > $@

libs:: $(MOZ_APP_NAME).1
	$(INSTALL) $< $(DIST)/man/man1

GARBAGE += $(MOZ_APP_NAME).1
GARBAGE += $(addprefix $(FINAL_TARGET)/defaults/pref/, all.js mailnews.js suite-prefs.js)
endif

# 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.
# 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)..."
if test -d "$(ABS_STAGE)/$(dir)"; then \
cd $(ABS_STAGE)/$(dir)/; \
$(ZIP) -r9mX ../$(dir).xpi * -x \*/.mkdir.done; \
cd ..; rm -rf $(ABS_STAGE)/$(dir); \
fi

endef # do not remove the blank line!

# GUIDs
# Unused for now. All extensions are being packed.
DONOTPACK = {9999999%

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'

# Additional macOS build steps.
ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))

MAC_APP_NAME = $(MOZ_APP_DISPLAYNAME)

ifdef MOZ_DEBUG
MAC_APP_NAME := $(MAC_APP_NAME)Debug
endif

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_dest)'

tools:: $(DIST)/bin/$(MOZ_APP_NAME)
	rm -rf $(dist_dest)
	mkdir -p '$(dist_dest)/Contents/MacOS'
	rsync -a --exclude "*.in" $(srcdir)/macbuild/Contents '$(dist_dest)' --exclude English.lproj
	mkdir -p '$(dist_dest)/$(LPROJ)'
	rsync -a --exclude "*.in" $(srcdir)/macbuild/Contents/Resources/English.lproj/ '$(dist_dest)/$(LPROJ)'
	sed -e 's/\@MOZ_APP_NAME\@/$(MOZ_APP_NAME)/' -e 's/\@MAC_APP_NAME\@/$(MAC_APP_NAME)/' -e 's/\@MOZ_APP_DISPLAYNAME\@/$(MOZ_APP_DISPLAYNAME)/' -e 's/\@MOZ_APP_VERSION\@/$(MOZ_APP_VERSION)/' -e 's/\@MOZ_MACBUNDLE_ID\@/$(MOZ_MACBUNDLE_ID)/' -e 's|\@MOZ_DEVELOPER_REPO_PATH\@|$(topsrcdir)|' -e 's|\@MOZ_DEVELOPER_OBJ_PATH\@|$(topobjdir)|' $(srcdir)/macbuild/Contents/Info.plist.in > '$(dist_dest)/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_dest)/$(LPROJ)/InfoPlist.strings'
	rsync -a --exclude-from='$(srcdir)/macbuild/Contents/MacOS-files.in' $(DIST)/bin/ '$(dist_dest)/Contents/Resources'
	rsync -a --include-from='$(srcdir)/macbuild/Contents/MacOS-files.in' --exclude '*' $(DIST)/bin/ '$(dist_dest)/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_dest)/Contents/MacOS'
	rm -f '$(dist_dest)/Contents/MacOS/$(MOZ_APP_NAME)'
	rsync -aL $(DIST)/bin/$(MOZ_APP_NAME) '$(dist_dest)/Contents/MacOS'
	cp -RL $(topsrcdir)/$(MOZ_BRANDING_DIRECTORY)/seamonkey.icns '$(dist_dest)/Contents/Resources/seamonkey.icns'
	cp -RL $(topsrcdir)/$(MOZ_BRANDING_DIRECTORY)/document.icns '$(dist_dest)/Contents/Resources/document.icns'
	printf APPLMOZZ > '$(dist_dest)/Contents/PkgInfo'
endif
