# Makefile for Trame.
#

PACKAGE = trame
VERSION = 1.2.1
PREFIX ?= /usr
DESTDIR ?=
# Languages: po/LINGUAS, one code per line (see po/README).
LINGUAS ?= $(shell sed '/^\#/d' po/LINGUAS)
POT ?= po/$(PACKAGE).pot

all: help

help:
	@echo "Trame $(VERSION) - SliTaz system administration backend"
	@echo
	@echo "Targets:"
	@echo "  test         Run the test suite (sh, busybox-safe)"
	@echo "  install      Install into \$$DESTDIR\$$PREFIX (default /usr)"
	@echo "  tui          Build the ncurses front-end (C)"
	@echo "  install-tui  Install the trame-tui binary"
	@echo "  gui          Build the GTK3 front-end (C, needs gtk+3-dev)"
	@echo "  install-gui  Install trame-gui and its desktop file"
	@echo "  pot          Regenerate the gettext template po/trame.pot"
	@echo "  msgmerge     Refresh the .po files from the .pot"
	@echo "  msgfmt       Compile the .po files into po/mo/"
	@echo "  po-init L=xx Start a new translation po/xx.po"
	@echo "  po-stats     Translated, fuzzy and missing strings per language"
	@echo "  clean        Remove generated artifacts"

# Tests

test:
	@sh tests/runtests.sh

# TUI (ncurses C front-end)

tui:
	$(MAKE) -C tui

install-tui: install-msg
	$(MAKE) -C tui install DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)

clean-tui:
	$(MAKE) -C tui clean

# GUI (GTK3 C front-end, not built by default: the CLI needs no GTK)

gui:
	$(MAKE) -C gui

install-gui: install-msg
	$(MAKE) -C gui install DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)

clean-gui:
	$(MAKE) -C gui clean

# i18n: one text domain, "trame", for the shell backend, the TUI, the
# GUI and the GUI .desktop file. Translators: read po/README.

XGETTEXT_SH = -k_ -k_n -k_p:1,2 -kN_ -ktrame_die:2 -ktrame_kv:1
XGETTEXT_C  = -k_ -kN_
XGETTEXT_ALL = --from-code=UTF-8 --add-comments=TRANSLATORS: \
	--package-name="Trame" --package-version="$(VERSION)" \
	--copyright-holder="SliTaz GNU/Linux" \
	--msgid-bugs-address="https://forum.slitaz.org/"

pot:
	xgettext -o $(POT) -L Shell $(XGETTEXT_ALL) $(XGETTEXT_SH) \
		bin/trame lib/common.sh lib/trame-*.sh
	xgettext -j -o $(POT) -L C $(XGETTEXT_ALL) $(XGETTEXT_C) \
		tui/src/*.c gui/src/*.c
	xgettext -j -o $(POT) -L Desktop $(XGETTEXT_ALL) \
		gui/data/trame-gui.desktop.in

msgmerge:
	@for l in $(LINGUAS); do \
		echo -n "Updating $$l po file"; \
		msgmerge -U --backup=none --previous po/$$l.po $(POT); \
	done

msgfmt:
	@for l in $(LINGUAS); do \
		echo "Compiling $$l mo file..."; \
		mkdir -p po/mo/$$l/LC_MESSAGES; \
		msgfmt --check -o po/mo/$$l/LC_MESSAGES/$(PACKAGE).mo po/$$l.po; \
	done

po-init:
	@[ -n "$(L)" ] || { echo "Usage: make po-init L=<lang> (de, pt_BR...)"; exit 1; }
	@[ ! -f po/$(L).po ] || { echo "po/$(L).po already exists"; exit 1; }
	msginit --no-translator -l $(L).UTF-8 -i $(POT) -o po/$(L).po
	@grep -qx "$(L)" po/LINGUAS || echo "$(L)" >> po/LINGUAS
	@echo "Now translate po/$(L).po, then: make msgfmt po-stats"

po-stats:
	@for l in $(LINGUAS); do \
		printf '%-6s ' $$l; \
		msgfmt --statistics -o /dev/null po/$$l.po 2>&1; \
	done

# Install

install-bin:
	install -m 0755 -d $(DESTDIR)$(PREFIX)/bin
	install -m 0755 bin/trame            $(DESTDIR)$(PREFIX)/bin/trame
	install -m 0755 bin/trame-testsuite  $(DESTDIR)$(PREFIX)/bin/trame-testsuite
	install -m 0755 bin/trame-wifi-debug $(DESTDIR)$(PREFIX)/bin/trame-wifi-debug

install-lib:
	install -m 0755 -d $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/common.sh         $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-system.sh   $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-hostname.sh $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-service.sh  $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-time.sh     $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-network.sh  $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-wifi.sh     $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-bluetooth.sh $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-locale.sh   $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-log.sh      $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-user.sh     $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-storage.sh  $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-kernel.sh   $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-boot.sh     $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-power.sh    $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-display.sh  $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-audio.sh    $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-firewall.sh $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-keyboard.sh $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-mirror.sh   $(DESTDIR)$(PREFIX)/lib/slitaz/trame
	install -m 0644 lib/trame-hardware.sh $(DESTDIR)$(PREFIX)/lib/slitaz/trame

install-share:
	install -m 0755 -d $(DESTDIR)$(PREFIX)/share/trame/schemas
	install -m 0644 share/trame/schemas/*.json \
		$(DESTDIR)$(PREFIX)/share/trame/schemas

install-conf:
	install -m 0755 -d $(DESTDIR)/etc/slitaz
	install -m 0644 etc/slitaz/trame.conf $(DESTDIR)/etc/slitaz/trame.conf
	install -m 0755 -d $(DESTDIR)/etc/slitaz/trame.d/hooks

install-doc:
	install -m 0755 -d $(DESTDIR)$(PREFIX)/share/doc/trame
	install -m 0644 doc/*.md $(DESTDIR)$(PREFIX)/share/doc/trame 2>/dev/null || true

install-msg: msgfmt
	@for l in $(LINGUAS); do \
		install -m 0755 -d $(DESTDIR)$(PREFIX)/share/locale/$$l/LC_MESSAGES; \
		install -m 0644 po/mo/$$l/LC_MESSAGES/$(PACKAGE).mo \
			$(DESTDIR)$(PREFIX)/share/locale/$$l/LC_MESSAGES; \
	done

install: install-bin install-lib install-share install-conf install-doc install-msg

# Clean

clean: clean-tui clean-gui
	rm -rf po/mo
	rm -f  po/*~

.PHONY: all help test pot msgmerge msgfmt po-init po-stats install install-bin install-msg install-lib \
	install-share install-conf install-doc clean tui install-tui clean-tui \
	gui install-gui clean-gui
