#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Mozilla Communicator client code, released
# March 31, 1998.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

DEPTH		= ../..
topsrcdir	= ../..
srcdir		= .
VPATH		= .

include $(DEPTH)/config/autoconf.mk

#
# This is how we create the Unix binary packages we release to the public.
# Currently the only format is tar.gz (TGZ), but it should be fairly easy
# to add .rpm (RPM) and .deb (DEB) later.
#
ifndef MOZ_PKG_FORMAT
ifneq (,$(filter mac cocoa,$(MOZ_WIDGET_TOOLKIT)))
MOZ_PKG_FORMAT	= DMG
else
ifeq (,$(filter-out OS2 WINNT, $(OS_ARCH)))
MOZ_PKG_FORMAT  = ZIP
INSTALLER_DIR   = windows
else
ifeq (,$(filter-out SunOS, $(OS_ARCH)))
MOZ_PKG_FORMAT	= BZ2
else
MOZ_PKG_FORMAT	= TGZ
endif
INSTALLER_DIR   = unix
endif
endif
endif # MOZ_PKG_FORMAT
ifndef MOZ_PKG_APPNAME
MOZ_PKG_APPNAME = $(MOZ_APP_NAME)
endif
ifndef SDK_PKG_APPNAME
SDK_PKG_APPNAME = gecko
endif

MAKE_PACKAGE	= echo "Sorry, don't know how to build a $(MOZ_PKG_FORMAT) file yet...."
MAKE_SDK	=

PKG_BASENAME	:= $(MOZ_PKG_APPNAME)-$(TARGET_CPU)-$(TARGET_VENDOR)-$(TARGET_OS)
PACKAGE		= $(PKG_BASENAME)$(PKG_SUFFIX)

SDK_DIR		= sdk
SDK		= $(SDK_PKG_APPNAME)-$(SDK_DIR)-$(TARGET_CPU)-$(TARGET_VENDOR)-$(TARGET_OS)$(PKG_SUFFIX)

TAR_CREATE_FLAGS = -cvhf

ifeq ($(OS_ARCH),BSD_OS)
TAR_CREATE_FLAGS = -cvLf
endif

CREATE_FINAL_TAR = tar -c --owner=0 --group=0 --numeric-owner --mode="go-w" -f

ifeq ($(MOZ_PKG_FORMAT),TAR)
PKG_SUFFIX	= .tar
MAKE_PACKAGE 	= $(CREATE_FINAL_TAR) - $(MOZ_PKG_APPNAME) > $(PACKAGE)
MAKE_SDK	= $(CREATE_FINAL_TAR) - $(SDK_PKG_APPNAME)-$(SDK_DIR) > $(SDK)
endif
ifeq ($(MOZ_PKG_FORMAT),TGZ)
PKG_SUFFIX	= .tar.gz
MAKE_PACKAGE 	= $(CREATE_FINAL_TAR) - $(MOZ_PKG_APPNAME) | gzip -vf9 > $(PACKAGE)
MAKE_SDK        = $(CREATE_FINAL_TAR) - $(SDK_PKG_APPNAME)-$(SDK_DIR) | gzip -vf9 > $(SDK)
endif
ifeq ($(MOZ_PKG_FORMAT),BZ2)
PKG_SUFFIX	= .tar.bz2
MAKE_PACKAGE 	= $(CREATE_FINAL_TAR) - $(MOZ_PKG_APPNAME) | bzip2 -vf > $(PACKAGE)
MAKE_SDK        = $(CREATE_FINAL_TAR) - $(SDK_PKG_APPNAME)-$(SDK_DIR) | bzip2 -vf > $(SDK)
endif
ifeq ($(MOZ_PKG_FORMAT),ZIP)
PKG_SUFFIX	= .zip
MAKE_PACKAGE	= $(ZIP) -r9D $(PACKAGE) $(MOZ_PKG_APPNAME)
MAKE_SDK	= $(ZIP) -r9D $(SDK) $(SDK_PKG_APPNAME)-$(SDK_DIR)
endif
ifeq ($(MOZ_PKG_FORMAT),DMG)
ifndef MOZ_PKG_APPNAME
MOZ_PKG_APPNAME = Mozilla
endif
ifdef MOZ_DEBUG
_APPNAME	= $(MOZ_PKG_APPNAME)Debug.app
else
_APPNAME	= $(MOZ_PKG_APPNAME).app
endif
PKG_SUFFIX	= .dmg.gz
_ABS_TOPSRCDIR	= $(shell cd $(topsrcdir) && pwd)
MAKE_PACKAGE	= $(_ABS_TOPSRCDIR)/build/package/mac_osx/make-diskimage $(PKG_BASENAME).dmg $(MOZ_PKG_APPNAME) $(MOZ_PKG_APPNAME) && gzip -vf9 $(PKG_BASENAME).dmg
MAKE_SDK	=
endif
ifeq ($(MOZ_PKG_FORMAT),RPM)
PKG_SUFFIX	= .rpm
endif
ifeq ($(MOZ_PKG_FORMAT),DEB)
PKG_SUFFIX	= .deb
endif

# dummy macro if we don't have PSM built
SIGN_NSS		=
ifndef CROSS_COMPILE
ifdef MOZ_PSM
SIGN_NSS		= @echo signing nss libraries;

SIGN_ENV	= LD_LIBRARY_PATH=$(DIST)/bin:${LD_LIBRARY_PATH} \
		LD_LIBRARYN32_PATH=$(DIST)/bin:${LD_LIBRARYN32_PATH} \
		LD_LIBRARYN64_PATH=$(DIST)/bin:${LD_LIBRARYN64_PATH} \
		LD_LIBRARY_PATH_64=$(DIST)/bin:${LD_LIBRARY_PATH_64} \
		SHLIB_PATH=$(DIST)/bin:${SHLIB_PATH} LIBPATH=$(DIST)/bin:${LIBPATH} \
		DYLD_LIBRARY_PATH=$(DIST)/bin:${DYLD_LIBRARY_PATH} \
		LIBRARY_PATH=$(DIST)/bin:${LIBRARY_PATH}

SIGN_CMD	= cd $(DIST)/$(MOZ_PKG_APPNAME) && $(SIGN_ENV) $(DIST)/bin/shlibsign -v -i

SOFTOKN		= $(DIST)/$(MOZ_PKG_APPNAME)/$(DLL_PREFIX)softokn3$(DLL_SUFFIX)
FREEBL_HYBRID	= $(DIST)/$(MOZ_PKG_APPNAME)/$(DLL_PREFIX)freebl_hybrid_3$(DLL_SUFFIX)
FREEBL_PURE	= $(DIST)/$(MOZ_PKG_APPNAME)/$(DLL_PREFIX)freebl_pure32_3$(DLL_SUFFIX)

SIGN_NSS	+= $(SIGN_CMD) $(SOFTOKN); \
        if test -f $(FREEBL_HYBRID); then $(SIGN_CMD) $(FREEBL_HYBRID); fi; \
        if test -f $(FREEBL_PURE); then $(SIGN_CMD) $(FREEBL_PURE); fi;

endif # MOZ_PSM
endif # !CROSS_COMPILE

TARGETS		= $(PACKAGE) $(SDK)

NSPR_LDIR	= $(findstring -L,$(NSPR_LIBS))
ifneq ($(NSPR_LDIR),)
NSPR_LDIR	= $(subst -L,,$(word 1,$(NSPR_LIBS)))
endif

EXCLUDE_LIST	= \
		$(DIST)/bin/core \
		$(DIST)/bin/bsdecho \
		$(DIST)/bin/gtscc \
		$(DIST)/bin/jscpucfg \
		$(DIST)/bin/nsinstall \
		$(DIST)/bin/viewer \
		$(NULL)

libs:: $(TARGETS)

include $(topsrcdir)/config/rules.mk

GARBAGE		+= $(DIST)/$(PACKAGE) $(PACKAGE) $(SDK)

OSPACKAGE	= unix
ifdef USE_SHORT_LIBNAME
MOZILLA_BIN	= $(DIST)/bin/$(MOZ_PKG_APPNAME)$(BIN_SUFFIX)
else
MOZILLA_BIN	= $(DIST)/bin/$(MOZ_PKG_APPNAME)-bin
endif

ifeq ($(OS_ARCH),IRIX)
STRIP_FLAGS	= -f
endif
ifeq ($(OS_ARCH),BeOS)
STRIP_FLAGS	= -g
PLATFORM_EXCLUDE_LIST = ! -name "*.stub" ! -name "$(MOZ_PKG_APPNAME)-bin"
endif
ifeq ($(OS_ARCH),OS2)
STRIP		= $(srcdir)/os2/strip.cmd
STRIP_FLAGS	=
OSPACKAGE	= os2
TAR_CREATE_FLAGS = -cvf
PLATFORM_EXCLUDE_LIST = ! -name "*.ico"
endif

$(SDK):
ifndef NO_GECKO_SDK
ifdef MAKE_SDK
	@rm -rf $(DIST)/$(SDK_PKG_APPNAME)-$(SDK_DIR) $@
	@mkdir $(DIST)/$(SDK_PKG_APPNAME)-$(SDK_DIR)
	@cd $(DIST)/$(SDK_DIR) && tar $(TAR_CREATE_FLAGS) - * | (cd ../$(SDK_PKG_APPNAME)-$(SDK_DIR); tar -xf -)
	cd $(DIST) && $(MAKE_SDK)
endif
endif

$(PACKAGE): $(MOZILLA_BIN)
	@rm -rf $(DIST)/$(MOZ_PKG_APPNAME) $(DIST)/$(PKG_BASENAME).tar $@ $(EXCLUDE_LIST)
# NOTE: this must be a tar now that dist links into the tree so that we
# do not strip the binaries actually in the tree.
	@echo "Creating package directory..."
	@mkdir $(DIST)/$(MOZ_PKG_APPNAME)
ifeq ($(MOZ_PKG_FORMAT),DMG)
	@cd $(DIST) && rsync -auvL $(_APPNAME) $(MOZ_PKG_APPNAME)
else
	@cd $(DIST)/bin && tar $(TAR_CREATE_FLAGS) - * | (cd ../$(MOZ_PKG_APPNAME); tar -xf -)
ifdef MOZ_NATIVE_NSPR
ifndef EXCLUDE_NSPR_LIBS
	@echo "Copying NSPR libs..."
	@cp -p $(NSPR_LDIR)/*$(DLL_SUFFIX) $(DIST)/$(MOZ_PKG_APPNAME)
	@chmod 755 $(DIST)/$(MOZ_PKG_APPNAME)/*$(DLL_SUFFIX)
endif
endif
endif # DMG
	@echo "Stripping package directory..."
	@cd $(DIST)/$(MOZ_PKG_APPNAME); find . ! -type d \
			! -name "*.js" \
			! -name "*.xpt" \
			! -name "*.gif" \
			! -name "*.jpg" \
			! -name "*.png" \
			! -name "*.xpm" \
			! -name "*.txt" \
			! -name "*.rdf" \
			! -name "*.sh" \
			! -name "*.properties" \
			! -name "*.dtd" \
			! -name "*.html" \
			! -name "*.xul" \
			! -name "*.css" \
			! -name "*.xml" \
			! -name "*.jar" \
			! -name "*.dat" \
			! -name "*.tbl" \
			! -name "*.src" \
			! -name "*.reg" \
			$(PLATFORM_EXCLUDE_LIST) \
			-exec $(STRIP) $(STRIP_FLAGS) {} >/dev/null 2>&1 \;
	$(SIGN_NSS)
ifeq (,$(filter mozilla firefox,$(MOZ_PKG_APPNAME)))
	@echo "Creating start script $(MOZ_PKG_APPNAME)..."
	cd $(DIST)/$(MOZ_PKG_APPNAME); if [ -f mozilla ]; then cp mozilla $(MOZ_PKG_APPNAME); fi
endif
ifdef NO_PKG_FILES
	@echo "Removing unpackaged files..."
ifeq ($(MOZ_PKG_FORMAT),DMG)
	cd $(DIST)/$(MOZ_PKG_APPNAME)/$(_APPNAME)/Contents/MacOS; rm -rf $(NO_PKG_FILES)
else
	cd $(DIST)/$(MOZ_PKG_APPNAME); rm -rf $(NO_PKG_FILES)
endif
endif
	@echo "Compressing..."
	cd $(DIST); $(MAKE_PACKAGE)

dist: $(MOZILLA_BIN)
ifndef MOZ_PKG_DEST
	@echo "Please define MOZ_PKG_DEST first. It must be an absolute path to a dir outside the source tree. It should be empty (files might get deleted without warning)."
	@echo "Other options (not required):"
	@echo "MOZ_PKG_FORMAT: Either TGZ for .tar.gz or BZ2 for .tar.bz2. Default TGZ."
	@echo "MOZ_PKG_APPNAME: Application name, used for tarball filename, top-level dir in tarball and application start script. Default mozilla."
else
	@if [ ! -d $(MOZ_PKG_DEST) ]; then mkdir $(MOZ_PKG_DEST); fi
	@cd $(MOZ_PKG_DEST); rm -rf bin $(MOZ_PKG_APPNAME) $(PACKAGE)
	@echo "Creating distribution tarball in $(MOZ_PKG_DEST)..."
	$(PERL) $(srcdir)/pkgcp.pl --source $(DEPTH)/dist --destination $(MOZ_PKG_DEST) --file $(srcdir)/packages-$(OSPACKAGE) --os $(OSPACKAGE) --flat
	@cd $(MOZ_PKG_DEST); mv bin $(MOZ_PKG_APPNAME)
ifneq ($(MOZ_PKG_APPNAME),mozilla)
	@echo "Creating start script $(MOZ_PKG_APPNAME)..."
	cd $(MOZ_PKG_DEST)/$(MOZ_PKG_APPNAME); cp mozilla $(MOZ_PKG_APPNAME)
endif
	@echo "Compressing..."
	cd $(MOZ_PKG_DEST); $(MAKE_PACKAGE)
	@echo "Deleting work files..."
	@cd $(MOZ_PKG_DEST); rm -rf $(MOZ_PKG_APPNAME)
	@echo "Done creating $(PACKAGE)."
endif

installer::
ifdef INSTALLER_DIR
	make -C $(INSTALLER_DIR) installer
endif
