ROPTS=-zav --no-p --chmod=u=rw,g=rw,o=r --delete --exclude=www/.htaccess --exclude=.svn --exclude=.git --exclude=*.mtt --exclude=tpl/css --exclude=www/file --exclude=*node_modules* --exclude=*.php --exclude=tests.n
LANG=fr
PLUGINS?=0
ENV?="dev"

#To compile project with plugins, add PLUGINS=1 or type `export PLUGINS=1`

install:
	#copy config file from template first time only
	@if [ ! -f config.xml ]; then \
	cp config.xml.dist config.xml; \
	fi

	@if [ $(ENV) = "dev" ]; then \
	make install_dev; \
	fi

#setup dev environnement from source
install_dev:
	#set config file to debug=1
	#@sed -e 's?debug=.*?debug="1"?g' --in-place config.xml
	#install haxe dependencies in .haxelib
	haxelib newrepo
	haxelib -always install cagette.hxml
	haxelib -always install cagetteJs.hxml
	#template tools
	haxelib run templo
	sudo mv temploc2 /usr/bin
	#install npm dependencies
	npm install
	npm run libs:dev
	npm run build:js
	#compile
	@make css
	@make compile
	@make frontend
	@make templates
	@echo "Well, it looks like everything is fine : librairies are installed, backend and frontend has been compiled !"


#compile backend to Neko
compile:
	@if [ $(PLUGINS) = 1 ]; then \
	echo "compile CagettePĂ©i plugins"; \
	haxe cagetteAllPlugins.hxml; \
	else \
	echo "compile CagettePĂ©i core"; \
	haxe cagette.hxml; \
	fi


#compile SASS files to CSS
css:
	npm run build:sass

#compile frontend to JS
frontend:
	@if [ $(PLUGINS) = 1 ]; then \
	haxe cagetteAllPluginsJs.hxml; \
	else \
	haxe cagetteJs.hxml; \
	fi
	mkdir -p tmp
	chown www-data:www-data tmp

#update POT file from source
i18n:
	haxe potGeneration.hxml

#compile templates in each language, required for production env.
templates:
	mkdir -p lang/master/tmp
	haxe cagette.hxml -D i18n_generation
	@make LANG=fr ctemplates
	msgfmt www/lang/texts_fr.po -o www/lang/texts_fr.mo

	chown www-data:www-data -R www
	chown www-data:www-data -R lang

ctemplates:
	(cd lang/$(LANG)/tpl; temploc2 -macros macros.mtt -output ../tmp/ *.mtt */*.mtt */*/*.mtt)