L10NVERSION=$(shell date +%Y-%m-%d -r $(LANGUAGE_ARCHIVE))
LABEL=$(PACKAGE)-$(SVNVERSION)
LANGUAGE_ARCHIVE=launchpad-export.tar.gz
LANGUAGES=en_US $(shell tar tf $(LANGUAGE_ARCHIVE) | grep /LC_MESSAGES/webtrees.mo | grep -v ^en/ | cut -d / -f 1)
LANGUAGES_RELEASE=en_US en_GB pl it es fi fr de da pt he et ca tr nl sk nn sl hu sv ru
MO=$(patsubst %,$(LABEL)/language/%.mo,$(LANGUAGES))
PACKAGE=webtrees
POT=$(PACKAGE).pot
PHP=$(shell find .. -path ../library -prune -o -path ../build -prune -o -name '*.php' -print)
SHELL=bash
SVNVERSION=$(shell svnversion .. | tr -d 'M')

################################################################################
# Default action is to build everything
################################################################################
default: clean $(LABEL).zip webtrees.pot
	@echo
	@echo "Build successful. Now upload the files to launchpad.net:"
	@echo
	@echo "$(LABEL).zip => https://launchpad.net/webtrees/trunk/snapshot"
	@echo "language.zip => https://webtrees.net/, addons"
	@echo "language-all.zip => https://webtrees.net/, addons"
	@echo "webtrees.pot => https://translations.launchpad.net/webtrees/trunk/+translations-upload"
	@echo

clean:
	rm -Rf webtrees* language* tmp_reports

################################################################################
# Create a ZIP archives
################################################################################
$(LABEL).zip: $(LABEL) $(MO)
	zip -q -j language-all.zip $(LABEL)/language/*.mo
	for file in `find $(LABEL)/language -name "*.mo"`; do echo $(LANGUAGES_RELEASE) | grep -q `basename $$file .mo` || rm $$file; done
	zip -q -j language.zip $(LABEL)/language/*.mo
	zip -qr $@ $(LABEL)
	rm -R $(LABEL)

################################################################################
# Extract code from the current SVN working copy and strip unused libraries
################################################################################
$(LABEL): $(PHP)
	svn export .. $(LABEL)
	rm -R $(LABEL)/library/Zend/{Acl,Amf,Application,Auth,Barcode,Captcha,CodeGenerator,Console,Controller,Config,Crypt,Currency,Date,Db,Debug,Dojo,Dom,Feed,File,Filter,Form,Gdata,Http,InfoCard,Json,Layout,Ldap,Log,Mail,Markup,Measure,Memory,Mime,Navigation,Oauth,OpenId,Paginator,Pdf,ProgressBar,Queue,Reflection,Rest,Search,Serializer,Service,Soap,Test,Text,TimeSync,Tool,Uri,Validate,Version,View,Wildfire,XmlRpc}*
	rm -R $(LABEL)/build
	rm -Rf $(LABEL)/language/*
	for file in `find . -path ./library -prune -o -name '*.php' -print`; do php -l $$file; done

################################################################################
# Extract a language file from the downloaded archive file, and rename it
################################################################################
%.mo: $(LANGUAGE_ARCHIVE)
	# en_US is maintained locally - others are maintained in launchpad
	if [ $(@F) = en_US.mo ]; then msgfmt -o $(LABEL)/language/en_US.mo en_US.po; else tar xfzO $(LANGUAGE_ARCHIVE) $(*F)/LC_MESSAGES/webtrees.mo > $@; fi

################################################################################
# We cannot make this file - we download it from launchpad.net
################################################################################
$(LANGUAGE_ARCHIVE):
	@echo
	@echo "------------------------------------------------------------------"
	@echo "You must download the compiled language files (in .MO format) from"
	@echo "https://translations.launchpad.net/webtrees/trunk/+export"
	@echo "Save the file ($(@)) to the build directory."
	@echo "Then run make again."
	@echo "------------------------------------------------------------------"
	@echo
	@false

################################################################################
# Update the PO Template file from the sources.
################################################################################
$(POT): $(PHP)
	rm -Rf tmp_reports
	mkdir tmp_reports
	for file in ../modules/*/*.xml; do cat $$file | php -r "echo preg_replace('/(i18n::[^)]+\))/', '<'.'?php echo \\1; ?>', file_get_contents('php://stdin'));" > tmp_reports/$$(echo $$file.php | cut -c 12- | tr / _); done
	@xgettext --package-name=$(PACKAGE) --package-version=1.0 --msgid-bugs-address=i18n@webtrees.net --output=$@ --no-wrap --language=PHP --add-comments=I18N --from-code=utf-8 --keyword --keyword=translate:1 --keyword=translate_c:1c,2 --keyword=plural:1,2 --keyword=noop:1 $(PHP) tmp_reports/*.php
	rm -Rf tmp_reports
