# Date: 2011-07-19
#
# Description:
#   This script builds automatically the .orig.tar.gz by cloning
#   a specific tag of dotclear mercurial repository.
#
# Copyright: 2011,2013 Nicolas Roudaire <nikrou77@gmail.com>
#
# License: GPL-2
#
# Way to invoke it
# make "DC_VERSION=x.y.z" 

HG=$(shell which hg)
PHP=$(shell which php)
WORKDIR=/tmp/dotclear-$(DC_VERSION)
DIST=$(WORKDIR)/.dist
BUILDTOOLSDIR=$(WORKDIR)/build-tools
DIST_DOTCLEAR=$(DIST)/dotclear

default:
	@echo 'make all "DC_VERSION=x.y.z'

all:clone build

clone:;
	$(HG) clone -r $(DC_VERSION) https://hg.dotclear.org/dotclear $(WORKDIR)

build:clean config orig-tar-gz

config:;
	@test -d $(WORKDIR) || echo "make clone before make config"
	@test -d $(DIST_DOTCLEAR) || mkdir -p $(DIST_DOTCLEAR)

	## Copy needed folders and files
	cp -pr $(WORKDIR)/admin $(WORKDIR)/inc $(WORKDIR)/themes $(WORKDIR)/index.php		\
		$(WORKDIR)/CHANGELOG $(WORKDIR)/CREDITS $(WORKDIR)/LICENSE $(WORKDIR)/README.md	\
		$(DIST_DOTCLEAR)/

	## Locales directory
	mkdir $(DIST_DOTCLEAR)/locales
	cp -pr $(WORKDIR)/locales/README $(WORKDIR)/locales/en $(WORKDIR)/locales/fr $(DIST_DOTCLEAR)/locales

        ## Create cache, db, plugins and public folders
	mkdir $(DIST_DOTCLEAR)/cache		\
		$(DIST_DOTCLEAR)/db		\
		$(DIST_DOTCLEAR)/plugins	\
		$(DIST_DOTCLEAR)/public
	cp $(WORKDIR)/inc/.htaccess $(DIST_DOTCLEAR)/cache/
	cp $(WORKDIR)/inc/.htaccess $(DIST_DOTCLEAR)/db/
	cp $(WORKDIR)/inc/.htaccess $(DIST_DOTCLEAR)/plugins/

	## Remove config file if any
	rm -f $(DIST_DOTCLEAR)/inc/config.php

	# Remove swf files 
	rm -fr $(DIST_DOTCLEAR)/inc/swf

	## Copy built-in plugins
	cp -pRf					\
	$(WORKDIR)/plugins/aboutConfig		\
	$(WORKDIR)/plugins/akismet		\
	$(WORKDIR)/plugins/antispam		\
	$(WORKDIR)/plugins/blogroll		\
	$(WORKDIR)/plugins/blowupConfig		\
	$(WORKDIR)/plugins/fairTrackbacks	\
	$(WORKDIR)/plugins/importExport		\
	$(WORKDIR)/plugins/maintenance		\
	$(WORKDIR)/plugins/tags			\
	$(WORKDIR)/plugins/pages		\
	$(WORKDIR)/plugins/pings		\
	$(WORKDIR)/plugins/themeEditor		\
	$(WORKDIR)/plugins/widgets		\
	$(DIST_DOTCLEAR)/plugins/

	## Remove .scm folders
	find $(DIST_DOTCLEAR)/ -type d -name '.hg'|xargs rm -fr
	find $(DIST_DOTCLEAR)/ -type f -name '.hgignore' -exec rm -f \{\} \;
	find $(DIST_DOTCLEAR)/ -type d -name '.svn'|xargs rm -fr
	find $(DIST_DOTCLEAR)/ -type d -name '.git'|xargs rm -fr
	find $(DIST_DOTCLEAR)/ -type f -name '.gitignore' -exec rm -f \{\} \;

	## "Compile" .po files
	$(PHP) $(BUILDTOOLSDIR)/make-l10n.php $(DIST_DOTCLEAR)/

	## Debug off
	perl -pi -e "s|^//\*== DC_DEBUG|/*== DC_DEBUG|sgi;" $(DIST_DOTCLEAR)/inc/prepend.php $(DIST_DOTCLEAR)/inc/prepend.php

	## Create digest
	cd $(DIST_DOTCLEAR) && ( \
		md5sum `find . -type f -not -path "./inc/digest" -not -path "./cache/*" -not -path "./db/*" -not -path ./CHANGELOG` \
		> inc/digests \
	)

orig-tar-gz:;
	cd $(DIST) && tar cfz dotclear_$$(grep DC_VERSION dotclear/inc/prepend.php | cut -d"'" -f4)+dfsg.orig.tar.gz ./dotclear

clean:;
	@rm -fr $(DIST)

dist-clean:;
	@rm -fr $(WORKDIR)

