#
# ***** 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 Weave code.
#
# The Initial Developer of the Original Code is Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Dan Mills <thunder@mozilla.com> (original author)
#   Justin Dolske <dolske@mozilla.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either 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 *****

ifeq ($(TOPSRCDIR),)
  export TOPSRCDIR = $(shell pwd)
endif

objdir=$(TOPSRCDIR)/dist
export stage_dir=$(objdir)/stage
xpi_dir=$(objdir)/xpi

weave_version := 1.4.3
weave_id := {340c2bbc-ce74-4362-90b5-7c26312808ef}
overlay_fennec_maxver := 1.999
overlay_firefox_maxver := 4.0b3pre

ifeq ($(release_build),)
  xpi_type := dev
  update_url := https://services.mozilla.com/sync/update.rdf
else
  xpi_type := rel
  update_url :=
endif

ifeq ($(update_url),)
  update_url_tag :=
else
  update_url_tag := <em:updateURL>$(update_url)</em:updateURL>
endif

buildid ?= ${WEAVE_BUILDID}
buildid_short := $(buildid)
ifeq ($(buildid),)
  date    := $(shell date -u +%Y%m%d%H%M)
  revid   := $(shell hg tip --template '{node|short}')
  buildid := $(date)-$(revid)
  buildid_short := $(date)
endif
ifeq ($(buildid),)
  $(warning Could not determine build id)
  $(warning Install hg or set WEAVE_BUILDID the checkout id)
  $(error)
endif

ifeq ($(MAKECMDGOALS),xpi)
  unpacked =\#
  jar=
  chrometarget=xpi
else
  unpacked=
  jar=\#
  chrometarget=
endif

subst_names := \
  xpi_type \
  weave_version \
  weave_id \
  buildid \
  buildid_short \
  overlay_fennec_maxver \
  overlay_firefox_maxver \
  update_url \
  update_url_tag \
  unpacked \
  jar
export $(subst_names)
SUBST = perl -pe 's/@([^@]+)@/defined $$ENV{$$1} ? $$ENV{$$1} : $$&/ge'
MKDIR = mkdir -p
SLINK = ln -sf
ifneq ($(findstring MINGW,$(shell uname -s)),)
  SLINK = cp
endif

all: build

.PHONY: setup build test xpi clean $(xpi_files)

test: build
	$(MAKE) -k -C services/sync/tests/unit

setup:
	$(MKDIR) $(objdir)
	$(MKDIR) $(stage_dir)
	$(MKDIR) $(xpi_dir)

crypto: setup
	$(MAKE) -C services/crypto stage

# stage precompiled binaries
crypto-obsolete-binaries: setup
	$(MAKE) -C crypto-obsolete stage

# build from source
crypto-obsolete-build: setup
	$(MAKE) -C crypto-obsolete build

build: crypto crypto-obsolete-binaries
	$(MKDIR) $(stage_dir)
	$(SUBST) $(TOPSRCDIR)/addon/chrome.manifest.in > $(stage_dir)/chrome.manifest
	$(SUBST) $(TOPSRCDIR)/addon/install.rdf.in > $(stage_dir)/install.rdf
	$(MKDIR) $(stage_dir)/chrome/content
	$(SLINK) $(TOPSRCDIR)/addon/options.xul $(stage_dir)/chrome/content/options.xul
	$(MKDIR) $(stage_dir)/chrome/skin
	$(SLINK) $(TOPSRCDIR)/addon/sync-32x32.png $(stage_dir)/chrome/skin/sync-32x32.png
	
	$(MKDIR) $(stage_dir)/components
	$(SLINK) $(TOPSRCDIR)/addon/AddonGlue.js $(stage_dir)/components/AddonGlue.js
	$(SLINK) $(TOPSRCDIR)/services/sync/FormNotifier.js $(stage_dir)/components/FormNotifier.js
	$(SLINK) $(TOPSRCDIR)/services/sync/Weave.js $(stage_dir)/components/Weave.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/AboutWeaveTabs.js $(stage_dir)/components/AboutWeaveTabs.js
	
	$(MKDIR) $(stage_dir)/chrome/locale/en-US/services
	$(SLINK) $(TOPSRCDIR)/services/sync/locales/en-US/errors.properties $(stage_dir)/chrome/locale/en-US/services/errors.properties
	$(SLINK) $(TOPSRCDIR)/services/sync/locales/en-US/sync.properties $(stage_dir)/chrome/locale/en-US/services/sync.properties
	
	$(MKDIR) $(stage_dir)/modules
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/auth.js $(stage_dir)/modules/auth.js
	$(MKDIR) $(stage_dir)/modules/base_records
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/base_records/collection.js $(stage_dir)/modules/base_records/collection.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/base_records/crypto.js $(stage_dir)/modules/base_records/crypto.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/base_records/keys.js $(stage_dir)/modules/base_records/keys.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/base_records/wbo.js $(stage_dir)/modules/base_records/wbo.js
	$(SUBST) $(TOPSRCDIR)/services/sync/modules/constants.js > $(stage_dir)/modules/constants.js
	$(MKDIR) $(stage_dir)/modules/engines
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/engines/bookmarks.js $(stage_dir)/modules/engines/bookmarks.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/engines/clients.js $(stage_dir)/modules/engines/clients.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/engines/forms.js $(stage_dir)/modules/engines/forms.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/engines/history.js $(stage_dir)/modules/engines/history.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/engines/passwords.js $(stage_dir)/modules/engines/passwords.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/engines/prefs.js $(stage_dir)/modules/engines/prefs.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/engines/tabs.js $(stage_dir)/modules/engines/tabs.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/engines.js $(stage_dir)/modules/engines.js
	$(MKDIR) $(stage_dir)/modules/ext
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/ext/Observers.js $(stage_dir)/modules/ext/Observers.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/ext/Preferences.js $(stage_dir)/modules/ext/Preferences.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/ext/StringBundle.js $(stage_dir)/modules/ext/StringBundle.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/ext/Sync.js $(stage_dir)/modules/ext/Sync.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/identity.js $(stage_dir)/modules/identity.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/log4moz.js $(stage_dir)/modules/log4moz.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/notifications.js $(stage_dir)/modules/notifications.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/resource.js $(stage_dir)/modules/resource.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/service.js $(stage_dir)/modules/service.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/status.js $(stage_dir)/modules/status.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/stores.js $(stage_dir)/modules/stores.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/trackers.js $(stage_dir)/modules/trackers.js
	$(MKDIR) $(stage_dir)/modules/type_records
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/type_records/bookmark.js $(stage_dir)/modules/type_records/bookmark.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/type_records/clients.js $(stage_dir)/modules/type_records/clients.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/type_records/forms.js $(stage_dir)/modules/type_records/forms.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/type_records/history.js $(stage_dir)/modules/type_records/history.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/type_records/passwords.js $(stage_dir)/modules/type_records/passwords.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/type_records/prefs.js $(stage_dir)/modules/type_records/prefs.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/type_records/tabs.js $(stage_dir)/modules/type_records/tabs.js
	$(SLINK) $(TOPSRCDIR)/services/sync/modules/util.js $(stage_dir)/modules/util.js
	
	$(MKDIR) $(stage_dir)/defaults/preferences
	$(SLINK) $(TOPSRCDIR)/services/sync/services-sync.js $(stage_dir)/defaults/preferences/services-sync.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/firefox-sync.js $(stage_dir)/defaults/preferences/firefox-sync.js
	
	$(MKDIR) $(stage_dir)/chrome/content/fennec
	$(SLINK) $(TOPSRCDIR)/ui/fennec/content/firstrun.xhtml $(stage_dir)/chrome/content/fennec/firstrun.xhtml
	$(SLINK) $(TOPSRCDIR)/ui/fennec/content/overlay.js $(stage_dir)/chrome/content/fennec/overlay.js
	$(SLINK) $(TOPSRCDIR)/ui/fennec/content/overlay.xul $(stage_dir)/chrome/content/fennec/overlay.xul
	$(SLINK) $(TOPSRCDIR)/ui/fennec/content/tabs.js $(stage_dir)/chrome/content/fennec/tabs.js
	$(SLINK) $(TOPSRCDIR)/ui/fennec/content/tabs.xhtml $(stage_dir)/chrome/content/fennec/tabs.xhtml
	
	$(MKDIR) $(stage_dir)/chrome/locale/en-US/fennec
	$(SLINK) $(TOPSRCDIR)/ui/fennec/locale/firstrun.dtd $(stage_dir)/chrome/locale/en-US/fennec/firstrun.dtd
	$(SLINK) $(TOPSRCDIR)/ui/fennec/locale/sync.dtd $(stage_dir)/chrome/locale/en-US/fennec/sync.dtd
	$(SLINK) $(TOPSRCDIR)/ui/fennec/locale/tabs.dtd $(stage_dir)/chrome/locale/en-US/fennec/tabs.dtd
	
	$(MKDIR) $(stage_dir)/chrome/skin/fennec
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/background.png $(stage_dir)/chrome/skin/fennec/background.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/firstrun.css $(stage_dir)/chrome/skin/fennec/firstrun.css
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/light-tab.png $(stage_dir)/chrome/skin/fennec/light-tab.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/logo.png $(stage_dir)/chrome/skin/fennec/logo.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/overlay.css $(stage_dir)/chrome/skin/fennec/overlay.css
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/step-bg.png $(stage_dir)/chrome/skin/fennec/step-bg.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/sync-32x32.png $(stage_dir)/chrome/skin/fennec/sync-32x32.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/tab.png $(stage_dir)/chrome/skin/fennec/tab.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/tabs-active-64.png $(stage_dir)/chrome/skin/fennec/tabs-active-64.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/tabs-default-64.png $(stage_dir)/chrome/skin/fennec/tabs-default-64.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/tabs-disabled-64.png $(stage_dir)/chrome/skin/fennec/tabs-disabled-64.png
	$(SLINK) $(TOPSRCDIR)/ui/fennec/skin/tabs.css $(stage_dir)/chrome/skin/fennec/tabs.css
	
	$(MKDIR) $(stage_dir)/chrome/content/firefox
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/generic-change.js $(stage_dir)/chrome/content/firefox/generic-change.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/generic-change.xul $(stage_dir)/chrome/content/firefox/generic-change.xul
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/load-weave.js $(stage_dir)/chrome/content/firefox/load-weave.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/notification.xml $(stage_dir)/chrome/content/firefox/notification.xml
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/overlay.js $(stage_dir)/chrome/content/firefox/overlay.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/overlay.xul $(stage_dir)/chrome/content/firefox/overlay.xul
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/prefs-common.js $(stage_dir)/chrome/content/firefox/prefs-common.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/prefs.js $(stage_dir)/chrome/content/firefox/prefs.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/prefs.xul $(stage_dir)/chrome/content/firefox/prefs.xul
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/setup.js $(stage_dir)/chrome/content/firefox/setup.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/setup.xul $(stage_dir)/chrome/content/firefox/setup.xul
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/sync.js $(stage_dir)/chrome/content/firefox/sync.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/sync.xul $(stage_dir)/chrome/content/firefox/sync.xul
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/tabs-bindings.xml $(stage_dir)/chrome/content/firefox/tabs-bindings.xml
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/tabs.js $(stage_dir)/chrome/content/firefox/tabs.js
	$(SLINK) $(TOPSRCDIR)/ui/firefox/content/tabs.xul $(stage_dir)/chrome/content/firefox/tabs.xul
	
	$(MKDIR) $(stage_dir)/chrome/locale/en-US/firefox
	$(SLINK) $(TOPSRCDIR)/ui/firefox/locale/generic-change.properties $(stage_dir)/chrome/locale/en-US/firefox/generic-change.properties
	$(SLINK) $(TOPSRCDIR)/ui/firefox/locale/prefs.dtd $(stage_dir)/chrome/locale/en-US/firefox/prefs.dtd
	$(SLINK) $(TOPSRCDIR)/ui/firefox/locale/prefs.properties $(stage_dir)/chrome/locale/en-US/firefox/prefs.properties
	$(SLINK) $(TOPSRCDIR)/ui/firefox/locale/sync.dtd $(stage_dir)/chrome/locale/en-US/firefox/sync.dtd
	$(SLINK) $(TOPSRCDIR)/ui/firefox/locale/tabs.dtd $(stage_dir)/chrome/locale/en-US/firefox/tabs.dtd
	
	$(MKDIR) $(stage_dir)/chrome/skin/firefox
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/bg.png $(stage_dir)/chrome/skin/firefox/bg.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/desktop-icon.png $(stage_dir)/chrome/skin/firefox/desktop-icon.png
	$(MKDIR) $(stage_dir)/chrome/skin/firefox/lin
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/lin/prefs.css $(stage_dir)/chrome/skin/firefox/lin/prefs.css
	$(MKDIR) $(stage_dir)/chrome/skin/firefox/mac
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/mac/sync.css $(stage_dir)/chrome/skin/firefox/mac/sync.css
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/mac/prefs.css $(stage_dir)/chrome/skin/firefox/mac/prefs.css
	$(MKDIR) $(stage_dir)/chrome/skin/firefox/win
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/win/sync.css $(stage_dir)/chrome/skin/firefox/win/sync.css
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/win/prefs.css $(stage_dir)/chrome/skin/firefox/win/prefs.css
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/mobile-icon.png $(stage_dir)/chrome/skin/firefox/mobile-icon.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/notification.css $(stage_dir)/chrome/skin/firefox/notification.css
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/prefs.css $(stage_dir)/chrome/skin/firefox/prefs.css
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/sync-16x16.png $(stage_dir)/chrome/skin/firefox/sync-16x16.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/sync-throbber-16x16-active.apng $(stage_dir)/chrome/skin/firefox/sync-throbber-16x16-active.apng
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/sync-32x32-prefs.png $(stage_dir)/chrome/skin/firefox/sync-32x32-prefs.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/sync-32x32.png $(stage_dir)/chrome/skin/firefox/sync-32x32.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/sync-merge.png $(stage_dir)/chrome/skin/firefox/sync-merge.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/sync-wipeClient.png $(stage_dir)/chrome/skin/firefox/sync-wipeClient.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/sync-wipeServer.png $(stage_dir)/chrome/skin/firefox/sync-wipeServer.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/tabs.css $(stage_dir)/chrome/skin/firefox/tabs.css
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/used-before.png $(stage_dir)/chrome/skin/firefox/used-before.png
	$(SLINK) $(TOPSRCDIR)/ui/firefox/skin/used-never.png $(stage_dir)/chrome/skin/firefox/used-never.png
	
	$(MKDIR) $(stage_dir)/chrome/content/seamonkey
	$(SLINK) $(TOPSRCDIR)/ui/seamonkey/content/prefs.xul $(stage_dir)/chrome/content/seamonkey/prefs.xul

xpi_name := weave-$(weave_version)-$(xpi_type).xpi
xpi_files := chrome/sync.jar defaults components modules platform \
             install.rdf chrome.manifest

xpi: build
	cd $(stage_dir)/chrome;rm -f sync.jar;zip -0r sync.jar *
	rm -f $(xpi_dir)/$(xpi_name)
	cd $(stage_dir);zip -9r $(xpi_name) $(xpi_files)
	mv $(stage_dir)/$(xpi_name) $(xpi_dir)/$(xpi_name)

clean:
	rm -rf $(objdir)
	$(MAKE) -C services/sync/tests/unit clean
	$(MAKE) -C services/crypto clean

help:
	@echo Targets:
	@echo build
	@echo "crypto (stage the js-ctypes component)"
	@echo "crypto-obsolete-binaries (stage the precompiled binary components)"
	@echo "crypto-obsolete-build (recompile the binary component)"
	@echo "test (runs tests, runs a build first)"
	@echo "xpi (sets manifest to use jars, make build to undo)"
	@echo clean
	@echo
	@echo Variables:
	@echo sdkdir
	@echo "release_build (set to 1 when not building a snapshot)"
	@echo "platform_target (takes a space-separated list of platforms to package):"
	@echo "    make xpi platform_target='Linux_x86-gcc3'"
	@echo "  this also supports * as a wildcard:"
	@echo "    make xpi platform_target='Linux_x86* WINNT* Darwin'"
	@echo
	@echo Substitutions for .in files:
	@echo $(subst_names)
