prefix = /usr

POFILES=$(wildcard *.po)
MOFILES=$(POFILES:.po=.mo)
PYFILES=$(shell find -L ../larpe -name '*.py' -or -name '*.ptl')
RM=rm -f

all: $(MOFILES)

install: all
	for file in $(MOFILES); do \
		lang=`echo $$file | sed 's/\.mo//'`; \
		install -d $(DESTDIR)$(prefix)/share/locale/$$lang/LC_MESSAGES/; \
		install -m 0644 $$file $(DESTDIR)$(prefix)/share/locale/$$lang/LC_MESSAGES/larpe.mo; \
	done

uninstall:
	@for file in $(MOFILES); do \
		lang=`echo $$file | sed 's/\.mo//'`; \
		$(RM) $(DESTDIR)$(prefix)/share/locale/$$lang/LC_MESSAGES/larpe.mo; \
	done

clean:
	-$(RM) messages.mo $(MOFILES)

larpe.pot: $(PYFILES)
	@echo "Rebuilding the pot file"
	$(RM) larpe.pot tmp.*.pot
	cnt=0;
	for file in $(PYFILES); do \
		cnt=$$(expr $$cnt + 1); \
		bn=$$cnt.`basename $$file`; \
		xgettext --keyword=N_ -c -L Python -o tmp.$$bn.pot $$file; \
	done
	msgcat tmp.*.pot > larpe.pot
	$(RM) tmp.*.pot

%.mo: %.po
	msgfmt -o $@ $<

%.po: larpe.pot
	@echo -n "Merging larpe.pot and $@"
	@msgmerge $@ larpe.pot -o $@.new
	@if [ "`diff $@ $@.new | grep '[<>]' | wc -l`" -ne 2 ]; then \
		mv -f $@.new $@; \
	else \
		$(RM) $@.new; \
	fi
	@msgfmt --statistics $@
