# Makefile for Spk.
#

PACKAGE="spk"
PREFIX?=/usr
LINGUAS?=

# spk-tui (ncurses TUI) build settings
CC ?= gcc
# The TUI header shows the toolset version: read it from the spk script
# (single source of truth) instead of a hardcoded define.
SPK_VERSION := $(shell sed -n 's/^VERSION=//p' spk)
CFLAGS ?= -O2 -Wall
# Appended, not part of the default: cook exports its own CFLAGS, which
# used to drop the define and left the TUI/GUI on their stale fallback.
CFLAGS += -DAPP_VERSION='"$(SPK_VERSION)"'
TUI_BIN = tui/spk-tui
TUI_SRC = tui/spk-tui.c tui/backend.c
TUI_HDR = tui/theme.h tui/backend.h
TUI_LIBS = -lncursesw

# spk-gui (GTK3 GUI) build settings. Shares tui/backend.c (TSV bridge).
GUI_BIN = gui/spk-gui
GUI_SRC = gui/spk-gui.c gui/window.c gui/catalog.c gui/pkgview.c \
          gui/sidebar.c gui/details.c gui/apply.c gui/updates.c \
          gui/style.c tui/backend.c
GUI_HDR = gui/window.h gui/catalog.h gui/pkgview.h gui/sidebar.h \
          gui/details.h gui/apply.h gui/updates.h gui/style.h tui/backend.h
GUI_CFLAGS = $(shell pkg-config --cflags gtk+-3.0)
GUI_LIBS = $(shell pkg-config --libs gtk+-3.0)

all: help

help:
	@echo "Targets: tui, test-tui, gui, install, install-tui, install-gui,"
	@echo "         clean, clean-tui, clean-gui, pot, msgfmt"

# spk-mv - STATIC rename helper: a live glibc upgrade renames ld+libc in
# one process; nothing dynamic must exec in between (see tools/spk-mv.c).
MV_BIN = tools/spk-mv
$(MV_BIN): tools/spk-mv.c
	$(CC) -static -Os -o $(MV_BIN) tools/spk-mv.c

# spk-tui - ncurses TUI (SliTaz 2026 dark theme)
tui: $(TUI_BIN)

$(TUI_BIN): $(TUI_SRC) $(TUI_HDR) spk
	$(CC) $(CFLAGS) -o $(TUI_BIN) $(TUI_SRC) $(TUI_LIBS)

# Standalone backend test harness (no ncurses): verifies TSV parsing.
test-tui: tui/test-backend.c tui/backend.c tui/backend.h
	$(CC) $(CFLAGS) -o tui/test-backend tui/test-backend.c tui/backend.c

install-tui: tui
	install -m 0755 -d $(DESTDIR)$(PREFIX)/bin
	install -m 0755 $(TUI_BIN) $(DESTDIR)$(PREFIX)/bin

clean-tui:
	rm -f $(TUI_BIN) tui/test-backend

# spk-gui - GTK3 front-end (see gui/README)
gui: $(GUI_BIN)

$(GUI_BIN): $(GUI_SRC) $(GUI_HDR) spk
	$(CC) $(CFLAGS) $(GUI_CFLAGS) -o $(GUI_BIN) $(GUI_SRC) $(GUI_LIBS)

install-gui: gui
	install -m 0755 -d $(DESTDIR)$(PREFIX)/bin
	install -m 0755 -d $(DESTDIR)$(PREFIX)/share/applications
	install -m 0755 $(GUI_BIN) $(DESTDIR)$(PREFIX)/bin
	install -m 0644 data/spk-gui.desktop $(DESTDIR)$(PREFIX)/share/applications

clean-gui:
	rm -f $(GUI_BIN)

# i18n

pot:
	xgettext -o po/$(PACKAGE).pot -L Shell --package-name="Spk" \
		./spk ./spk-add ./spk-rm ./spk-up ./spk-ls ./spk-search \
		./spk-mirror ./spk-convert ./spk-doctor
	xgettext -j -o po/$(PACKAGE).pot -L C -k_ -kN_ --package-name="Spk" \
		gui/*.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 -o po/mo/$$l/LC_MESSAGES/$(PACKAGE).mo po/$$l.po; \
	done;

# Install

install-msg: msgfmt
	install -m 0755 -d $(DESTDIR)$(PREFIX)/share/locale
	cp -a po/mo/* $(DESTDIR)$(PREFIX)/share/locale

install-lib:
	install -m 0755 -d $(DESTDIR)$(PREFIX)/lib/slitaz
	install -m 0755 lib/libspk.sh $(DESTDIR)$(PREFIX)/lib/slitaz
	
install-doc:
	install -m 0755 -d $(DESTDIR)$(PREFIX)/share/doc/spk
	install -m 0755 -d $(DESTDIR)$(PREFIX)/share/applications
	install -m 0644 data/spk-doc.desktop $(DESTDIR)$(PREFIX)/share/applications
	install -m 0644 doc/spk.*.html $(DESTDIR)$(PREFIX)/share/doc/spk
	cp -a doc/spk.html $(DESTDIR)$(PREFIX)/share/doc/spk

install: install-lib install-doc $(MV_BIN)
	install -m 0755 -d $(DESTDIR)$(PREFIX)/bin
	install -m 0755 -d $(DESTDIR)$(PREFIX)/sbin
	install -m 0755 -d $(DESTDIR)$(PREFIX)/libexec/spk
	install -m 0755 $(MV_BIN) $(DESTDIR)$(PREFIX)/libexec/spk
	install -m 0755 -d $(DESTDIR)$(PREFIX)/share/pixmaps
	install -m 0755 spk $(DESTDIR)$(PREFIX)/bin
	install -m 0755 spk-ls $(DESTDIR)$(PREFIX)/bin
	install -m 0755 spk-search $(DESTDIR)$(PREFIX)/bin
	install -m 0755 spk-rm $(DESTDIR)$(PREFIX)/sbin
	install -m 0755 spk-add $(DESTDIR)$(PREFIX)/sbin
	install -m 0755 spk-mirror $(DESTDIR)$(PREFIX)/sbin
	install -m 0755 spk-up $(DESTDIR)$(PREFIX)/sbin
	install -m 0755 spk-convert $(DESTDIR)$(PREFIX)/sbin
	install -m 0755 spk-doctor $(DESTDIR)$(PREFIX)/sbin
	install -m 0755 spk-sql $(DESTDIR)$(PREFIX)/sbin
	cp data/spk.png $(DESTDIR)$(PREFIX)/share/pixmaps

# Clean source

clean:
	rm -rf po/mo
	rm -f po/*~ $(MV_BIN)

