# The MIT License (MIT)
#
# Copyright (C) 2026 Scott Duensing
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# Printing library suite: the library (print.lib's sources) with a fake
# printer port, the BASIC compiler and VM (obj/host/basrt.a) for LPRINT
# and OPEN "LPT1:", and the test GUI for the dialogs.  Golden printer
# byte streams live in golden/ (UPDATE_GOLDENS=1 rewrites them).

include ../dvxtest/dvxtest.mk

.DEFAULT_GOAL := all

PRINT_DIR   = $(DVXTEST_ROOT)/src/libs/kpunch/print
BINDIR      = $(DVXTEST_BINDIR)/print
PRINT_TEST  = $(BINDIR)/printTest
PRINT_SRCS  = $(wildcard $(PRINT_DIR)/*.c)

# Fonts as the build makes them (src/tools/Makefile), for the text at
# printer sizes: DejaVu Sans, with its print sizes, and DejaVu Sans
# Condensed, which has none
PRINT_FONT_BASE   = $(DVXTEST_ROOT)/bin/shared/fonts/kpunch/DJVSANS.DFN
PRINT_FONT_SCREEN = $(DVXTEST_ROOT)/bin/shared/fonts/kpunch/DJVSCND.DFN

# Ghostscript renders the PostScript drivers' output back to dots and text
GS ?= gs
PRINT_OBJS  = $(patsubst $(PRINT_DIR)/%.c,$(BINDIR)/print/%.o,$(PRINT_SRCS))
LIBS_A      = $(DVXTEST_ARCHIVES) $(DVXTEST_TASKS_A) $(DVXTEST_BASRT_A)

CFLAGS      = $(DVXTEST_CFLAGS) -I$(DVXTEST_ROOT)/src/libs/kpunch -I$(PRINT_DIR) -I$(DVXTEST_BASICDIR) -DPRINT_GOLDEN_DIR='"$(abspath golden)"' -DBASIC_INCLUDE_DIR='"$(DVXTEST_ROOT)/src/include/basic"' -DPRINT_FONT_BASE='"$(PRINT_FONT_BASE)"' -DPRINT_FONT_SCREEN='"$(PRINT_FONT_SCREEN)"' -DPRINT_GS='"$(GS)"'

.PHONY: all run clean

all: run

$(BINDIR)/print/%.o: $(PRINT_DIR)/%.c $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

$(PRINT_TEST): printTest.c $(PRINT_OBJS) $(DVX_MAKEFILES) $(LIBS_A) | dvxtest-libs
	@mkdir -p $(BINDIR)
	$(HOSTCC) $(CFLAGS) -o $@ $< $(PRINT_OBJS) $(DVXTEST_BASRT_A) $(DVXTEST_LDFLAGS) -Wl,--whole-archive $(DVXTEST_LIBDVX_A) $(DVXTEST_TASKS_A) $(DVXTEST_LIB) -Wl,--no-whole-archive -ldl -lm

-include $(PRINT_OBJS:.o=.d) $(PRINT_TEST).d

# Runs from its object directory: the jobs, lptN.prn and USER/DVX.INI
# it writes never land in the source tree.
run: $(PRINT_TEST)
	cd $(BINDIR) && ASAN_OPTIONS=detect_leaks=1 ./printTest

clean:
	rm -rf $(BINDIR)
