# 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.

# Text Help Library Makefile for DJGPP cross-compilation
#
# Builds texthelp.lib -- shared text editing infrastructure
# (clipboard, multi-click, word boundaries, single-line editing engine).

DJGPP_PREFIX  = $(HOME)/djgpp/djgpp
CC            = $(DJGPP_PREFIX)/bin/i586-pc-msdosdjgpp-gcc
DXE3GEN       = PATH=$(DJGPP_PREFIX)/bin:$(PATH) DJDIR=$(DJGPP_PREFIX)/i586-pc-msdosdjgpp $(DJGPP_PREFIX)/i586-pc-msdosdjgpp/bin/dxe3gen
CFLAGS        = -O2 -Wall -Wextra -Werror -Wno-type-limits -Wno-sign-compare -Wno-format-truncation -march=i486 -mtune=i586 -I. -I../libdvx -I../libdvx/platform -I../libtasks -I../libdvx/thirdparty

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

SRCS   = textHelp.c
OBJS   = $(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
TARGETDIR = $(LIBSDIR)/kpunch/texthelp
TARGET    = $(TARGETDIR)/texthelp.lib

.PHONY: all clean

all: $(TARGET) $(TARGETDIR)/texthelp.dep

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

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

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

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

$(LIBSDIR):
	mkdir -p $(LIBSDIR)

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

# Dependencies
$(OBJDIR)/textHelp.o: textHelp.c textHelp.h ../libdvx/dvxWgtP.h ../libdvx/dvxWgt.h ../libdvx/dvxTypes.h ../libdvx/dvxApp.h ../libdvx/dvxDraw.h ../libdvx/dvxWm.h ../libdvx/dvxVideo.h

clean:
	rm -rf $(OBJS) $(TARGET) $(TARGETDIR)/texthelp.dep $(TARGETDIR)
