TRFILES = $(shell cat ./files.in)

LANGUAGES = $(shell cat ./lang.in)

OPWD = $(PWD)

INCLUDES = 

LIB =

all:
	@echo "Makes no sense to go through all in one step."

gettext:
	@cd ../../; \
	for aa in $(TRFILES); do \
		bb=$${aa##*/} \
		bb=$${bb%%.*}; \
		echo "Processing: $$bb"; \
		xgettext -L PHP -o $(OPWD)/$$bb.pot $$aa; \
	done

genmsg: gendirs
	@for aa in $(LANGUAGES); do \
		for bb in $(TRFILES); do \
			bb=$${bb##*/} \
			cc=$${bb%%.*}; \
			echo "Processing: ./$$aa/$$cc.po"; \
			if [ -e "./$$aa/$$cc.po" ]; then \
				if [ -e "./$$aa/compendium.po" ]; then \
					msgmerge --compendium ./$$aa/compendium.po ./$$aa/$$cc.po $$cc.pot -o \
					./$$aa/$$cc.pon; \
				else \
					msgmerge ./$$aa/$$cc.po $$cc.pot -o \
					./$$aa/$$cc.pon; \
				fi; \
			else \
				if [ -e "./$$aa/compendium.po" ]; then \
					msginit -l $$aa -i $$cc.pot -o ./$$aa/$$cc.po; \
				else \
					msginit -l $$aa -i $$cc.pot -o ./$$aa/$$cc.po; \
				fi; \
			fi; \
		done; \
	done


msgfmt:
	@for aa in $(LANGUAGES); do \
		rm -f ../$$aa/LC_MESSAGES/*.mo; \
		for bb in $(TRFILES); do \
			bb=$${bb##*/} \
			cc=$${bb%%.*}; \
			if [ -e "./$$aa/$$cc.pon" ]; then \
				mv ./$$aa/$$cc.pon ./$$aa/$$cc.po; \
			fi; \
			if [ -e "./$$aa/$$cc.po.inc" ]; then \
				msgcat -o ./$$aa/$$cc.po ./$$aa/$$cc.po ./$$aa/$$cc.po.inc; \
			fi; \
			msgfmt -v -o ../$$aa/LC_MESSAGES/$$cc.mo ./$$aa/$$cc.po; \
		done; \
	done

#	msgfmt -v -o de.mo de.po
#	msgfmt -v -o es.mo es.po

gendirs:
	@for aa in $(LANGUAGES); do \
		if [ ! -d "../$$aa" ]; then \
			echo "mkdir ../$$aa"; \
			mkdir ../$$aa; \
		fi; \
		if [ ! -d "../$$aa/LC_MESSAGES" ]; then \
			echo "mkdir ../$$aa/LC_MESSAGES"; \
			mkdir ../$$aa/LC_MESSAGES; \
		fi; \
		if [ ! -d "./$$aa" ]; then \
			echo "mkdir ./$$aa"; \
			mkdir ./$$aa; \
		fi; \
	done

test:
	@unset LC_LANG; unset LANG; unset LC_ALL;\
	for aa in $(LANGUAGES); do \
		echo "Lang: $$aa"; \
		for cc in $(TRFILES); do \
			cc=$${cc##*/} \
			i=$${cc%%.*}; \
			x=`msgfmt -v -o /dev/null "./$$aa/$$i.po" 2>&1 | sed -e 's/[,\.]//g' \
				-e 's,\([0-9]*\) translated messages,TR=\1,' \
				-e 's,\([0-9]*\) fuzzy translations,FZ=\1,' \
				-e 's,\([0-9]*\) untranslated messages,UT=\1,' \
				-e 's,\([0-9]*\) untranslated message,UT=\1,'`; \
			TR=0 FZ=0 UT=0; \
			eval $$x; \
			echo "./$$aa/$$i.po" `expr 100 "*" $$TR / "(" $$TR + $$FZ + $$UT ")"` \
			percent of `expr $$TR + $$FZ + $$UT` strings; \
			sed -n	-e '/Project-Id-Version/p' -e '/Revision-Date/p' \
			-e '/Last-Translator/p' -e '/^$$/{p;q;}' "./$$aa/$$i.po"; \
		done; \
	done

