../share/locale/%/LC_MESSAGES/linuxcnc.mo: po/%.po
	$(ECHO) Compiling localized message catalog $@
	@mkdir -p $(dir $@)
	$(Q)msgfmt -o $@ $<

../share/locale/%/LC_MESSAGES/gmoccapy.mo: po/gmoccapy/%.po
	$(ECHO) Compiling localized gmoccapy message catalog $@
	@mkdir -p $(dir $@)
	$(Q)msgfmt -o $@ $<

# the touch is because for po/zh_CN.po, msgfmt --tcl creates
# objects/zh_cn.po and on the next invocation make would try again to
# create zh_CN.po
objects/%.msg: po/%.po
	$(ECHO) Compiling localized message catalog $@
	$(Q)msgfmt --tcl -d objects -l $* $< && touch $@

MO_FILES := $(patsubst po/%.po, %/LC_MESSAGES/linuxcnc.mo, $(wildcard po/*.po))
MO_FILES += $(patsubst po/gmoccapy/%.po, %/LC_MESSAGES/gmoccapy.mo, $(wildcard po/gmoccapy/*.po))

$(DESTDIR)$(localedir)/%/LC_MESSAGES/linuxcnc.mo: ../share/locale/%/LC_MESSAGES/linuxcnc.mo
	$(DIR) $(DESTDIR)$(localedir)/$*/LC_MESSAGES
	$(FILE) $< $@

$(DESTDIR)$(localedir)/%/LC_MESSAGES/gmoccapy.mo: ../share/locale/%/LC_MESSAGES/gmoccapy.mo
	$(DIR) $(DESTDIR)$(localedir)/$*/LC_MESSAGES
	$(FILE) $< $@

TARGETS += $(patsubst po/%.po, \
	../share/locale/%/LC_MESSAGES/linuxcnc.mo, \
	$(wildcard po/*.po))

TARGETS += $(patsubst po/gmoccapy/%.po, \
	../share/locale/%/LC_MESSAGES/gmoccapy.mo, \
	$(wildcard po/gmoccapy/*.po))

TCLSRCS := \
	../tcl/bin/emccalib.tcl \
	../tcl/bin/emcdebug.tcl \
	../tcl/bin/emctesting.tcl \
	../tcl/bin/emctuning.tcl \
	../tcl/bin/genedit.tcl \
	../tcl/bin/halconfig.tcl \
	../tcl/bin/halshow.tcl \
	../tcl/bin/pickconfig.tcl \
	../tcl/bin/tkbackplot.tcl \
	../tcl/show_errors.tcl \
	../tcl/tklinuxcnc.tcl \
	../tcl/scripts/Set_Coordinates.tcl \
	../tcl/scripts/balloon.tcl \
	../tcl/scripts/emchelp.tcl \
	../tcl/ngcgui.tcl \
	../tcl/ngcgui_ttt.tcl \
	../tcl/ngcgui_app.tcl \
	../tcl/tooledit.tcl

TOOLI18NSRCS := \
	../lib/python/gladevcp/iconview.py \
	../lib/python/gladevcp/hal_mdihistory.py \
	../lib/python/gladevcp/offsetpage_widget.py \
	../lib/python/gladevcp/tooledit_widget.py \
	../lib/python/gladevcp/gladevcp-test.glade \
	../lib/python/gladevcp/offsetpage.glade \
	../lib/python/gladevcp/tooledit_gtk.glade \
	../lib/python/gladevcp/calculator.glade \
	../lib/python/gladevcp/hal_mdihistory.py

po/linuxcnc.pot:
	$(ECHO) Localizing linuxcnc.pot
	$(Q)(cd ..; $(XGETTEXT) --from-code=UTF-8 -k_ -o src/$@ `$(PYTHON) src/po/fixpaths.py -j src $^`)
	$(Q)touch $@

.PHONY: pofiles
pofiles: po/linuxcnc.pot po/gmoccapy/gmoccapy.pot
	set -x; for i in po/*.po; do msgmerge -U $$i po/linuxcnc.pot; done
	set -x; for i in po/gmoccapy/*.po; do msgmerge -U $$i po/gmoccapy/gmoccapy.pot; done

po/linuxcnc.pot: emc/task/emctaskmain.cc $(LIBRS274SRCS) emc/rs274ngc/rs274ngc_return.hh
po/linuxcnc.pot: hal/utils/meter.c \
    hal/utils/scope.c \
    hal/utils/scope_horiz.c \
    hal/utils/scope_vert.c \
    hal/utils/scope_trig.c \
    hal/utils/scope_disp.c \
    hal/utils/scope_files.c \
    hal/utils/miscgtk.c
po/linuxcnc.pot: emc/motion/control.c emc/motion/command.c emc/motion/motion.c emc/motion/homing.c

po/linuxcnc.pot: hal/classicladder/arithm_eval.c \
    hal/classicladder/arithm_eval.c \
    hal/classicladder/arrays.c \
    hal/classicladder/calc.c \
    hal/classicladder/calc_sequential.c \
    hal/classicladder/classicladder.c \
    hal/classicladder/classicladder_gtk.c \
    hal/classicladder/config.c \
    hal/classicladder/config_gtk.c \
    hal/classicladder/drawing.c \
    hal/classicladder/drawing_sequential.c \
    hal/classicladder/edit.c \
    hal/classicladder/edit_gtk.c \
    hal/classicladder/edit_sequential.c \
    hal/classicladder/editproperties_gtk.c \
    hal/classicladder/emc_mods.c \
    hal/classicladder/files.c \
    hal/classicladder/files_project.c \
    hal/classicladder/files_sequential.c \
    hal/classicladder/manager.c \
    hal/classicladder/manager_gtk.c \
    hal/classicladder/protocol_modbus_master.c \
    hal/classicladder/protocol_modbus_slave.c \
    hal/classicladder/serial_linux.c \
    hal/classicladder/socket_modbus_master.c \
    hal/classicladder/socket_server.c \
    hal/classicladder/spy_vars_gtk.c \
    hal/classicladder/symbols.c \
    hal/classicladder/symbols_gtk.c \
    hal/classicladder/vars_names.c \
    hal/classicladder/vars_access.c \

po/linuxcnc.pot: $(TCLSRCS)
po/linuxcnc.pot: $(PYI18NSRCS)
po/linuxcnc.pot: $(TOOLI18NSRCS)

.PHONY: install-locale
install-locale: $(addprefix $(DESTDIR)$(localedir)/, $(MO_FILES))
install-kernel-indep: install-locale

GMOCCAPY_GLADE := \
	emc/usr_intf/gmoccapy/gmoccapy.glade.h

$(GMOCCAPY_GLADE): %.glade.h: %.glade
	intltool-extract --type=gettext/glade $<
GMOCCAPY_I18N_SRCS := \
	$(patsubst %,emc/usr_intf/gmoccapy/%, \
		gmoccapy.py dialogs.py getiniinfo.py notification.py player.py \
		preferences.py widgets.py gmoccapy.glade.h) \

po/gmoccapy/gmoccapy.pot: $(GMOCCAPY_I18N_SRCS)
	$(ECHO) Localizing gmoccapy.pot
	$(Q)(cd ..; $(XGETTEXT) --from-code=UTF-8 --language=Python \
		--keyword=_ --keyword=N_ \
		--output=src/$@ `$(PYTHON) src/po/fixpaths.py -j src $^`)
	$(Q)touch $@

clean: gmoccapy_i18n_clean
gmoccapy_i18n_clean:
	@rm -f $(GMOCCAPY_GLADE)

postatus::
	@echo "info: Program POT and PO file status"
	@for p in po/*.pot po/*.po; do \
	    echo -n "$$p ";  msgfmt --statistics -o /dev/null $$p; \
	done
	@for p in po/gmoccapy/*.pot po/gmoccapy/*.po; do \
	    echo -n "$$p ";  msgfmt --statistics -o /dev/null $$p; \
	done
