# 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 and cross-compiled DOS tools.

CC      = gcc
CFLAGS  = -O2 -Wall -Wextra -Werror -Wno-format-truncation -I../libs/kpunch/libdvx -I../libs/kpunch/libdvx/platform

DJGPP_PREFIX = $(HOME)/djgpp/djgpp
DOSCC        = $(DJGPP_PREFIX)/bin/i586-pc-msdosdjgpp-gcc
EXE2COFF     = $(DJGPP_PREFIX)/i586-pc-msdosdjgpp/bin/exe2coff
CWSDSTUB     = $(DJGPP_PREFIX)/i586-pc-msdosdjgpp/bin/CWSDSTUB.EXE
DOSCFLAGS    = -O2 -Wall -Wextra -Werror -Wno-format-truncation -march=i486 -mtune=i586 -I../libs/kpunch/libdvx -I../libs/kpunch/libdvx/platform

BINDIR    = ../../bin
HOSTDIR   = ../../bin/host
CONFIGDIR = ../../bin/config
SYSTEMDIR = ../../bin/system

.PHONY: all clean deploy-helpsrc

all: $(HOSTDIR)/dvxres $(HOSTDIR)/mkicon $(HOSTDIR)/mktbicon $(HOSTDIR)/mkwgticon $(HOSTDIR)/bmp2raw $(HOSTDIR)/dvxhlpc $(SYSTEMDIR)/SPLASH.RAW $(SYSTEMDIR)/DVXHLPC.EXE $(SYSTEMDIR)/DVXRES.EXE

PLATFORM_UTIL = ../libs/kpunch/libdvx/platform/dvxPlatformUtil.c
STB_DS_IMPL   = ../libs/kpunch/libdvx/thirdparty/stb_ds_impl.c

$(HOSTDIR)/dvxres: dvxres.c ../libs/kpunch/libdvx/dvxResource.c ../libs/kpunch/libdvx/dvxRes.h $(PLATFORM_UTIL) $(STB_DS_IMPL) | $(HOSTDIR)
	$(CC) $(CFLAGS) -o $@ dvxres.c ../libs/kpunch/libdvx/dvxResource.c $(PLATFORM_UTIL) $(STB_DS_IMPL)

$(HOSTDIR)/mkicon: mkicon.c bmpDraw.c bmpDraw.h | $(HOSTDIR)
	$(CC) $(CFLAGS) -o $@ mkicon.c bmpDraw.c -lm

$(HOSTDIR)/mktbicon: mktbicon.c bmpDraw.c bmpDraw.h | $(HOSTDIR)
	$(CC) $(CFLAGS) -o $@ mktbicon.c bmpDraw.c

$(HOSTDIR)/mkwgticon: mkwgticon.c bmpDraw.c bmpDraw.h | $(HOSTDIR)
	$(CC) $(CFLAGS) -o $@ mkwgticon.c bmpDraw.c

$(HOSTDIR)/bmp2raw: bmp2raw.c | $(HOSTDIR)
	$(CC) $(CFLAGS) -o $@ bmp2raw.c

$(HOSTDIR)/dvxhlpc: dvxhlpc.c ../apps/kpunch/dvxhelp/hlpformat.h $(PLATFORM_UTIL) $(STB_DS_IMPL) | $(HOSTDIR)
	$(CC) $(CFLAGS) -o $@ dvxhlpc.c $(PLATFORM_UTIL) $(STB_DS_IMPL)

$(HOSTDIR):
	mkdir -p $(HOSTDIR)

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

$(BINDIR):
	mkdir -p $(BINDIR)

$(CONFIGDIR):
	mkdir -p $(CONFIGDIR)

$(SYSTEMDIR)/DVXHLPC.EXE: dvxhlpc.c hlpcCompile.h ../apps/kpunch/dvxhelp/hlpformat.h $(PLATFORM_UTIL) $(STB_DS_IMPL) | $(SYSTEMDIR)
	$(DOSCC) $(DOSCFLAGS) -o $(SYSTEMDIR)/dvxhlpc.exe dvxhlpc.c $(PLATFORM_UTIL) $(STB_DS_IMPL)
	$(EXE2COFF) $(SYSTEMDIR)/dvxhlpc.exe
	cat $(CWSDSTUB) $(SYSTEMDIR)/dvxhlpc > $@
	rm -f $(SYSTEMDIR)/dvxhlpc $(SYSTEMDIR)/dvxhlpc.exe

# Object file for linking into the loader (no main)
../../obj/loader/dvxhlpc.o: dvxhlpc.c hlpcCompile.h ../apps/kpunch/dvxhelp/hlpformat.h | ../../obj/loader
	$(DOSCC) $(DOSCFLAGS) -DHLPC_NO_MAIN -c -o $@ dvxhlpc.c

../../obj/loader:
	mkdir -p ../../obj/loader

$(SYSTEMDIR)/DVXRES.EXE: dvxres.c ../libs/kpunch/libdvx/dvxResource.c ../libs/kpunch/libdvx/dvxRes.h $(PLATFORM_UTIL) $(STB_DS_IMPL) | $(SYSTEMDIR)
	$(DOSCC) $(DOSCFLAGS) -o $(SYSTEMDIR)/dvxres.exe dvxres.c ../libs/kpunch/libdvx/dvxResource.c $(PLATFORM_UTIL) $(STB_DS_IMPL)
	$(EXE2COFF) $(SYSTEMDIR)/dvxres.exe
	cat $(CWSDSTUB) $(SYSTEMDIR)/dvxres > $@
	rm -f $(SYSTEMDIR)/dvxres $(SYSTEMDIR)/dvxres.exe

$(SYSTEMDIR):
	mkdir -p $(SYSTEMDIR)

deploy-helpsrc:
	@echo "Deploying help source files..."
	@# Deploy help images from core/
	@for f in ../libs/kpunch/libdvx/*.png; do \
		[ -f "$$f" ] || continue; \
		name=$$(basename "$$f" | tr a-z A-Z); \
		cp "$$f" $(BINDIR)/libs/kpunch/libdvx/"$$name"; \
	done
	@for pair in \
		"../libs/kpunch/libdvx/*.dhs:$(BINDIR)/libs/kpunch/libdvx" \
		"../libs/kpunch/libtasks/*.dhs:$(BINDIR)/libs/kpunch/libtasks" \
		"../libs/kpunch/dvxshell/*.dhs:$(BINDIR)/libs/kpunch/dvxshell" \
		"../libs/kpunch/sql/*.dhs:$(BINDIR)/libs/kpunch/dvxsql" \
		"../libs/kpunch/texthelp/*.dhs:$(BINDIR)/libs/kpunch/texthelp" \
		"../libs/kpunch/listhelp/*.dhs:$(BINDIR)/libs/kpunch/listhelp" \
		"../libs/kpunch/taskmgr/*.dhs:$(BINDIR)/libs/kpunch/taskmgr" \
		"../libs/kpunch/serial/*.dhs:$(BINDIR)/libs/kpunch/serial" \
		"../apps/kpunch/dvxbasic/basrt.dhs:$(BINDIR)/libs/kpunch/basrt" \
	; do \
		src=$${pair%%:*}; dst=$${pair##*:}; \
		for f in $$src; do \
			[ -f "$$f" ] || continue; \
			name=$$(basename "$$f" | tr a-z A-Z); \
			cp "$$f" "$$dst"/"$$name"; \
		done; \
	done
	@for f in ../widgets/kpunch/kpunch/*.dhs; do \
		[ -f "$$f" ] || continue; \
		name=$$(basename "$$f" | tr a-z A-Z); \
		cp "$$f" $(BINDIR)/widgets/kpunch/"$$name"; \
	done
	@for d in ../widgets/kpunch/kpunch/*/; do \
		for f in "$$d"*.dhs "$$d"*.bhs; do \
			[ -f "$$f" ] || continue; \
			name=$$(basename "$$f" | tr a-z A-Z); \
			base=$$(basename "$$f" | tr A-Z a-z); \
			base=$${base%.dhs}; \
			base=$${base%.bhs}; \
			if [ -d $(BINDIR)/widgets/kpunch/"$$base" ]; then \
				cp "$$f" $(BINDIR)/widgets/kpunch/"$$base"/"$$name"; \
			fi; \
		done; \
	done
	@for f in ../apps/kpunch/dvxbasic/*.dhs; do \
		[ -f "$$f" ] || continue; \
		name=$$(basename "$$f" | tr a-z A-Z); \
		cp "$$f" $(BINDIR)/apps/kpunch/dvxbasic/"$$name"; \
	done
	@for f in ../apps/kpunch/dvxhelp/*.dhs; do \
		[ -f "$$f" ] || continue; \
		name=$$(basename "$$f" | tr a-z A-Z); \
		cp "$$f" $(BINDIR)/apps/kpunch/dvxhelp/"$$name"; \
	done
	@echo "Deploying help config files..."
	@for d in ../apps/kpunch/*/; do \
		for f in "$$d"*.hcf; do \
			[ -f "$$f" ] || continue; \
			appdir=$$(basename "$$d"); \
			name=$$(basename "$$f" | tr a-z A-Z); \
			cp "$$f" $(BINDIR)/apps/kpunch/"$$appdir"/"$$name"; \
		done; \
	done

clean:
	rm -rf $(HOSTDIR)
	rm -rf $(SYSTEMDIR)
