# This file is part of phomemo-printer-app.
# Copyright (C) 2026 Paul Bütof, Passionate Bytes Solutions
#
# phomemo-printer-app is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# phomemo-printer-app is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with phomemo-printer-app.  If not, see <https://www.gnu.org/licenses/>.

# Build the Phomemo printer app against the system PAPPL and cJSON.
#
#   make            build phomemo-printer-app
#   make test       build and run the C unit tests
#   make install    install the binary, the system unit and its preset
#                   (PREFIX=/usr/local, UNITDIR=/etc/systemd/system; needs
#                   root; DESTDIR for package builds)

PREFIX    ?= /usr/local
DESTDIR   ?=
UNITDIR   ?= /etc/systemd/system
PRESETDIR ?= /usr/lib/systemd/system-preset
CC        ?= cc
CFLAGS    ?= -O2 -g
ALL_CFLAGS = $(CFLAGS) -std=c99 -D_GNU_SOURCE -Wall -Wextra -Werror \
             -Wno-unused-parameter -I. \
             $(shell pkg-config --cflags pappl libcjson) \
             -DPAPPL_VERSION_STR='"$(shell pkg-config --modversion pappl)"'
CJSON_LIBS = $(shell pkg-config --libs libcjson)
ALL_LDLIBS = $(LDLIBS) $(shell pkg-config --libs pappl) $(CJSON_LIBS) -pthread

APP      = phomemo-printer-app
SRCS     = main.c bridge.c device.c driver.c job.c models.c models_data.c
MODELS   = ../models
OBJS     = $(SRCS:.c=.o)
TESTS    = tests/test_bridge tests/test_driver

all: $(APP)

$(APP): $(OBJS)
	$(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(ALL_LDLIBS)

%.o: %.c phomemo.h bridge.h
	$(CC) $(ALL_CFLAGS) -c -o $@ $<

# The model manifests, built into the binary.
models_data.c: embed-models.sh $(wildcard $(MODELS)/*/model.json $(MODELS)/*/*.png)
	sh embed-models.sh $(MODELS) > $@

tests/test_bridge: tests/test_bridge.o bridge.o
	$(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ $^ $(CJSON_LIBS)

tests/test_driver: tests/test_driver.o driver.o job.o device.o bridge.o models.o models_data.o
	$(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ $^ $(ALL_LDLIBS)

test: $(TESTS)
	@for t in $(TESTS); do echo "== $$t"; ./$$t || exit 1; done

UNIT     = ../../packaging/systemd/phomemo-printer-app.service
PRESET   = ../../packaging/systemd/90-phomemo-printer-app.preset

# Installs the binary under PREFIX and the system unit (its ExecStart
# rewritten to that PREFIX) and preset; DESTDIR for package builds.
install: $(APP)
	install -d $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(UNITDIR) $(DESTDIR)$(PRESETDIR)
	install -m 755 $(APP) $(DESTDIR)$(PREFIX)/bin/$(APP)
	sed 's|/usr/bin/phomemo-printer-app|$(PREFIX)/bin/phomemo-printer-app|' $(UNIT) \
	  > $(DESTDIR)$(UNITDIR)/phomemo-printer-app.service
	install -m 644 $(PRESET) $(DESTDIR)$(PRESETDIR)/90-phomemo-printer-app.preset

clean:
	rm -f $(APP) $(OBJS) $(TESTS) tests/*.o models_data.c

.PHONY: all test install clean
