# lightgui_rust — generated data tables
# The widget registry, palette, property/event tables and indicator
# catalog are DATA extracted from the C++ sources in /opt/lightgui
# (repo https://github.com/cnmcdee/lightgui, HEAD eb0b5fc). They are
# never hand-edited: regenerate them with `make regen` and verify them
# against the sources with `make check`.
#
#   make regen   — re-run every extractor, rewriting data/*.json
#   make check   — re-derive every table from the C++ sources and fail
#                  if any data/*.json disagrees (drift detection)
#   make check-<name> — check one table (registry|palette|props|events|indicators)

LIGHTGUI_SRC ?= /opt/lightgui
PY ?= python3

.PHONY: regen check check-registry check-palette check-props check-events check-indicators install designer

# Install the XFrame widget headers (header-only) so generated apps
# build on any machine where LightGUI is installed. The headers live
# in the C repo ($(LIGHTGUI_SRC)/widgets); this just copies them to
# $(PREFIX)/include/lightgui. The Rust generator emits the same
# default include path as the C generator. Also installs the designer
# binary ($(BINARY), default target/debug/lightgui-designer), the
# palette icons, and a desktop entry + icon. Run from the designer via
# Tools -> Install Libraries…, which prompts for the root password
# (pkexec, sudo fallback). `make designer` builds the binary first
# (as the user, so no root-owned build artefacts land in the checkout).
PREFIX ?= /usr/local
INCLUDEDIR ?= $(PREFIX)/include/lightgui
BINDIR ?= $(PREFIX)/bin
SHAREDIR ?= $(PREFIX)/share/lightgui
BINARY ?= target/debug/lightgui-designer

designer:
	cargo build -p lightgui-designer

install:
	@echo "Installing LightGUI widget headers to $(INCLUDEDIR)"
	@if [ -d "$(LIGHTGUI_SRC)/widgets" ]; then \
		mkdir -p $(INCLUDEDIR); \
		cd $(LIGHTGUI_SRC)/widgets && find common core components -name '*.h' | while read f; do \
			install -D -m 644 "$$f" "$(INCLUDEDIR)/$$f"; \
		done; \
		echo "Installed $$(find $(LIGHTGUI_SRC)/widgets/common $(LIGHTGUI_SRC)/widgets/core $(LIGHTGUI_SRC)/widgets/components -name '*.h' | wc -l) headers to $(INCLUDEDIR)"; \
	else \
		echo "NOTE: no C sources at $(LIGHTGUI_SRC) — skipping headers (install them from the C designer instead)"; \
	fi
	@echo "Installing designer binary to $(BINDIR)"
	@mkdir -p $(BINDIR)
	@install -m 755 $(BINARY) $(BINDIR)/lightgui-designer
	@if [ -d "$(LIGHTGUI_SRC)/widgets/icons" ]; then \
		mkdir -p $(SHAREDIR)/icons; \
		cd $(LIGHTGUI_SRC)/widgets/icons && find . -name '*.ico' | while read f; do \
			install -D -m 644 "$$f" "$(SHAREDIR)/icons/$$f"; \
		done; \
		echo "Installed $$(find $(LIGHTGUI_SRC)/widgets/icons -name '*.ico' | wc -l) palette icons to $(SHAREDIR)/icons"; \
	else \
		echo "NOTE: no palette icons at $(LIGHTGUI_SRC)/widgets/icons — skipping"; \
	fi
	@echo "Installing desktop entry + icon"
	@mkdir -p $(PREFIX)/share/applications $(PREFIX)/share/icons/hicolor/512x512/apps
	@install -D -m 644 lightgui-designer.desktop $(PREFIX)/share/applications/lightgui-designer.desktop
	@install -D -m 644 flatpak/icon.png $(PREFIX)/share/icons/hicolor/512x512/apps/lightgui-designer.png
	@-update-desktop-database $(PREFIX)/share/applications 2>/dev/null; true
	@-gtk-update-icon-cache -q $(PREFIX)/share/icons/hicolor 2>/dev/null; true
	@echo "Installed LightGUI (Rust): binary $(BINDIR)/lightgui-designer, headers $(INCLUDEDIR), data $(SHAREDIR)"

regen:
	LIGHTGUI_SRC=$(LIGHTGUI_SRC) $(PY) tools/extract_all.py

check:
	LIGHTGUI_SRC=$(LIGHTGUI_SRC) $(PY) tools/check.py

check-registry:
	LIGHTGUI_SRC=$(LIGHTGUI_SRC) $(PY) tools/check.py registry

check-palette:
	LIGHTGUI_SRC=$(LIGHTGUI_SRC) $(PY) tools/check.py palette

check-props:
	LIGHTGUI_SRC=$(LIGHTGUI_SRC) $(PY) tools/check.py props

check-events:
	LIGHTGUI_SRC=$(LIGHTGUI_SRC) $(PY) tools/check.py events

check-indicators:
	LIGHTGUI_SRC=$(LIGHTGUI_SRC) $(PY) tools/check.py indicators
