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

# DVX Tools Makefile
#
# Builds native (host) utilities into bin/host and cross-compiled DOS
# tools into bin/system, and deploys the help sources into bin/.

include ../common.mk

LIBDVX    = ../libs/kpunch/libdvx
INCLUDES  = -I$(LIBDVX) -I$(LIBDVX)/platform

# Host tools use HOSTCC, never CC, so an analyze.sh / CI override of CC
# (to a cross/analyzer compiler) cannot cross-compile them or contaminate
# bin/host.
HOSTCFLAGS  = $(HOST_TOOL_CFLAGS) $(INCLUDES)
HOSTLDFLAGS =
DOSCFLAGS   = $(DOS_CFLAGS) $(INCLUDES)

BINDIR    = ../../bin
HOSTDIR   = ../../bin/host
SYSTEMDIR = ../../bin/system
HOBJDIR   = ../../obj/tools/host
DOBJDIR   = ../../obj/tools/dos

# COV=1 (src/test/coverage): gcov-instrumented host tools only, built
# under obj/hostcov/tools so bin/host and the DOS targets stay untouched.
ifeq ($(COV),1)
HOSTDIR     = ../../obj/hostcov/tools/bin
HOBJDIR     = ../../obj/hostcov/tools/obj
HOSTCFLAGS  = $(filter-out -O2,$(HOST_TOOL_CFLAGS)) -O1 -g $(HOST_INSTFLAGS) $(INCLUDES)
HOSTLDFLAGS = $(HOST_INSTFLAGS)
endif

# Sources of each tool (host and DOS builds share the lists)
SUPPORT_SRCS   = $(LIBDVX)/platform/dvxPlatformUtil.c $(LIBDVX)/thirdparty/stb_ds_impl.c
DVXRES_SRCS    = dvxres.c dvxResWrite.c $(LIBDVX)/dvxResource.c $(SUPPORT_SRCS)
DVXHLPC_SRCS   = dvxhlpc.c $(SUPPORT_SRCS)
MKICON_SRCS    = mkicon.c bmpDraw.c
MKTBICON_SRCS  = mktbicon.c bmpDraw.c
MKWGTICON_SRCS = mkwgticon.c bmpDraw.c
BMP2RAW_SRCS   = bmp2raw.c
MKSOUNDS_SRCS  = mkSounds.c
ALL_SRCS       = $(sort $(DVXRES_SRCS) $(DVXHLPC_SRCS) $(MKICON_SRCS) $(MKTBICON_SRCS) $(MKWGTICON_SRCS) $(BMP2RAW_SRCS) $(MKSOUNDS_SRCS))

# hostObjs / dosObjs SRCS -> objects for SRCS
hostObjs = $(patsubst %.c,$(HOBJDIR)/%.o,$(notdir $1))
dosObjs  = $(patsubst %.c,$(DOBJDIR)/%.o,$(notdir $1))

HOST_TOOLS = $(HOSTDIR)/dvxres $(HOSTDIR)/mkicon $(HOSTDIR)/mktbicon $(HOSTDIR)/mkwgticon $(HOSTDIR)/bmp2raw $(HOSTDIR)/dvxhlpc $(HOSTDIR)/mksounds
DOS_TOOLS  = $(SYSTEMDIR)/SPLASH.RAW $(SYSTEMDIR)/DVXHLPC.EXE $(SYSTEMDIR)/DVXRES.EXE

# The default desktop sounds (DVX.INI [sounds]), made by mksounds: one
# name per sound it knows.
SOUNDDIR    = $(BINDIR)/config/sounds
SOUND_NAMES = beep error exit info question startup warning winclose winopen
SOUNDS      = $(patsubst %,$(SOUNDDIR)/%.wav,$(SOUND_NAMES))

ifeq ($(COV),1)
ALL = $(HOST_TOOLS)
else
ALL = $(HOST_TOOLS) $(DOS_TOOLS) $(SOUNDS)
endif

.PHONY: all host clean deploy-helpsrc

all: $(ALL)

host: $(HOST_TOOLS)

$(HOSTDIR)/dvxres: $(call hostObjs,$(DVXRES_SRCS)) | $(HOSTDIR)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $^

$(HOSTDIR)/dvxhlpc: $(call hostObjs,$(DVXHLPC_SRCS)) | $(HOSTDIR)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $^

$(HOSTDIR)/mkicon: $(call hostObjs,$(MKICON_SRCS)) | $(HOSTDIR)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $^ -lm

$(HOSTDIR)/mktbicon: $(call hostObjs,$(MKTBICON_SRCS)) | $(HOSTDIR)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $^

$(HOSTDIR)/mkwgticon: $(call hostObjs,$(MKWGTICON_SRCS)) | $(HOSTDIR)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $^

$(HOSTDIR)/bmp2raw: $(call hostObjs,$(BMP2RAW_SRCS)) | $(HOSTDIR)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $^

$(HOSTDIR)/mksounds: $(call hostObjs,$(MKSOUNDS_SRCS)) | $(HOSTDIR)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $^ -lm

$(SOUNDDIR)/%.wav: $(HOSTDIR)/mksounds | $(SOUNDDIR)
	$(HOSTDIR)/mksounds $* $@

$(SYSTEMDIR)/SPLASH.RAW: $(HOSTDIR)/bmp2raw ../../assets/splash.bmp | $(SYSTEMDIR)
	$(HOSTDIR)/bmp2raw ../../assets/splash.bmp $@

# DOS executables: link, convert to COFF, prepend the DPMI stub.
$(SYSTEMDIR)/DVXHLPC.EXE: $(call dosObjs,$(DVXHLPC_SRCS)) | $(SYSTEMDIR)
	$(CC) -o $(SYSTEMDIR)/dvxhlpc.exe $^
	$(EXE2COFF) $(SYSTEMDIR)/dvxhlpc.exe
	cat $(CWSDSTUB) $(SYSTEMDIR)/dvxhlpc > $@
	rm -f $(SYSTEMDIR)/dvxhlpc $(SYSTEMDIR)/dvxhlpc.exe

$(SYSTEMDIR)/DVXRES.EXE: $(call dosObjs,$(DVXRES_SRCS)) | $(SYSTEMDIR)
	$(CC) -o $(SYSTEMDIR)/dvxres.exe $^
	$(EXE2COFF) $(SYSTEMDIR)/dvxres.exe
	cat $(CWSDSTUB) $(SYSTEMDIR)/dvxres > $@
	rm -f $(SYSTEMDIR)/dvxres $(SYSTEMDIR)/dvxres.exe

# One host and one DOS object rule per source (the sources live in
# several directories).
define TOOL_OBJ_RULES
$(call hostObjs,$1): $1 $$(DVX_MAKEFILES) | $$(HOBJDIR)
	$$(HOSTCC) $$(HOSTCFLAGS) -c -o $$@ $$<

$(call dosObjs,$1): $1 $$(DVX_MAKEFILES) | $$(DOBJDIR)
	$$(CC) $$(DOSCFLAGS) -c -o $$@ $$<
endef

$(foreach src,$(ALL_SRCS),$(eval $(call TOOL_OBJ_RULES,$(src))))

$(HOSTDIR) $(SYSTEMDIR) $(HOBJDIR) $(DOBJDIR) $(SOUNDDIR):
	mkdir -p $@

-include $(patsubst %.o,%.d,$(call hostObjs,$(ALL_SRCS)) $(call dosObjs,$(ALL_SRCS)))

# Copy the help sources (upper-case 8.3 names) and help configs into bin/
# so the loader can rebuild the help files on the target.  helpFiles.sh
# finds every library and app help source by itself (see its header) and
# fails on one it cannot place, instead of silently skipping it.
deploy-helpsrc:
	@echo "Deploying help source files..."
	@# Help images from libdvx (optional)
	@for f in $(wildcard $(LIBDVX)/*.png); do \
		cp "$$f" $(BINDIR)/libs/kpunch/libdvx/"$$(basename "$$f" | tr a-z A-Z)"; \
	done
	./helpFiles.sh deploy
	$(MAKE) -C ../widgets/kpunch deploy-helpsrc
	@echo "Deploying help config files..."
	@for d in ../apps/kpunch/*/; do \
		for f in "$$d"*.hcf; do \
			[ -f "$$f" ] || continue; \
			appdir=$$(basename "$$d"); \
			cp "$$f" $(BINDIR)/apps/kpunch/"$$appdir"/"$$(basename "$$f" | tr a-z A-Z)"; \
		done; \
	done

# Remove only what this Makefile produces (bin/system also holds
# dvxbasic's BASCOMP.EXE).  COV=1 touches only the instrumented tree.
clean:
ifeq ($(COV),1)
	rm -rf $(HOSTDIR) $(HOBJDIR)
else
	rm -f $(HOST_TOOLS) $(DOS_TOOLS) $(SOUNDS)
	rm -rf $(HOBJDIR) $(DOBJDIR)
endif
