# 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 Makefile for DJGPP cross-compilation
#
# Printing library (print.lib): printer ports and jobs, text pages,
# picture drivers (ESC/P, PCL, ImageWriter), the Print and Page Setup
# dialogs and the BASIC functions.  See print.h.  Every drivers/<name>/
# directory is a driver module built into drivers/<name>.drv beside the
# library (the PostScript printers', drivers/ps: printPsMod.c loads it
# when one is first used).

include ../../../common.mk

CFLAGS        = $(DOS_CFLAGS) -I. -I../libdvx -I../libdvx/platform -I../libdvx/thirdparty -I../libtasks -I../../../widgets/kpunch

OBJDIR  = ../../../../obj/print
LIBSDIR = ../../../../bin/libs

SRCS   = $(wildcard *.c)
OBJS   = $(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
TARGETDIR = $(LIBSDIR)/kpunch/print
TARGET    = $(TARGETDIR)/print.lib

DRVDIR      = $(TARGETDIR)/drivers
DRV_OBJS    = $(patsubst %.c,$(OBJDIR)/%.o,$(wildcard drivers/*/*.c))
DRV_TARGETS = $(patsubst %,$(DRVDIR)/%.drv,$(DVX_DRIVERS))

.PHONY: all clean

all: $(TARGET) $(TARGETDIR)/print.dep $(DRV_TARGETS)

$(TARGETDIR)/print.dep: print.dep | $(TARGETDIR)
	sed 's/$$/\r/' $< > $@

$(TARGET): $(OBJS) | $(TARGETDIR)
	$(DXE3GEN) -o $(TARGETDIR)/print.dxe -U $(OBJS)
	mv $(TARGETDIR)/print.dxe $@

$(foreach d,$(DVX_DRIVERS),$(eval $(call DVX_DRIVER_RULE,$d)))

$(OBJDIR)/%.o: %.c $(DVX_MAKEFILES) | $(OBJDIR)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(TARGETDIR):
	mkdir -p $(TARGETDIR)

$(DRVDIR):
	mkdir -p $(DRVDIR)

-include $(OBJS:.o=.d) $(DRV_OBJS:.o=.d)

clean:
	rm -rf $(OBJDIR) $(TARGETDIR)
