# Makefile for Trame.
#

PACKAGE = trame
VERSION = 1.0
PREFIX ?= /usr
DESTDIR ?=
LINGUAS ?= fr

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"
	@echo "  msgmerge     Refresh .po files from the .pot"
	@echo "  msgfmt       Compile .po files into po/mo/"
	@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 and the GUI.

XGETTEXT_SH = -k_ -k_n -kN_ -ktrame_die:2 -ktrame_kv:1
XGETTEXT_C  = -k_ -kN_

pot:
	xgettext -o po/$(PACKAGE).pot -L Shell --from-code=UTF-8 \
		--package-name="Trame" --package-version="$(VERSION)" \
		$(XGETTEXT_SH) bin/trame lib/common.sh lib/trame-*.sh
	xgettext -j -o po/$(PACKAGE).pot -L C --from-code=UTF-8 \
		$(XGETTEXT_C) tui/src/*.c gui/src/*.c

msgmerge:
	@for l in $(LINGUAS); do \
		echo -n "Updating $$l po file."; \
		msgmerge -U po/$$l.po po/$(PACKAGE).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

# 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 install install-bin install-msg install-lib \
	install-share install-conf install-doc clean tui install-tui clean-tui \
	gui install-gui clean-gui
