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

# DOS test-script module (bin/test/script.dxe), the test programs
# (bin/test/gametest.app for gamepad.scr, joytest.app for the joystick
# scripts, ...) and the host screenshot
# comparer (obj/test/dos/imgCmp) for the emulator harness.
#
# Built only for the DOS test run (make test-dos / runDos.sh); the loader
# opens TEST/SCRIPT.DXE only when DVX is started with /SCRIPT=<file>, and
# mkcd.sh leaves bin/test off the CD image.  The parser and runner sources
# are shared with the host suite in src/test/script.

include ../../common.mk

ROOT      = ../../..
LIBDVX    = $(ROOT)/src/libs/kpunch/libdvx
CFLAGS    = $(DOS_CFLAGS) -I$(LIBDVX) -I$(LIBDVX)/platform -I$(LIBDVX)/thirdparty -I$(ROOT)/src/libs/kpunch -I$(ROOT)/src/libs/kpunch/libtasks -I$(ROOT)/src/libs/kpunch/dvxshell -I$(ROOT)/src/widgets/kpunch -Iscript

OBJDIR    = $(ROOT)/obj/test/dos
BINDIR    = $(ROOT)/bin/test
SRCS      = script/scriptLang.c script/scriptRun.c script/scriptDos.c
OBJS      = $(patsubst script/%.c,$(OBJDIR)/%.o,$(SRCS))
TARGET    = $(BINDIR)/script.dxe

# Test programs: every <name>/<name>.c here (gameTest, joyTest, ...) is
# built to bin/test/<name in lower case>.app (TEST\GAMETEST.APP); nothing
# lists them.
TEST_APP_NAMES = $(patsubst %/,%,$(dir $(wildcard *Test/*Test.c)))
TEST_APP_OBJS  = $(patsubst %,$(OBJDIR)/%.o,$(TEST_APP_NAMES))
lowerCase      = $(shell echo '$(1)' | tr A-Z a-z)
TEST_APPS      = $(foreach n,$(TEST_APP_NAMES),$(BINDIR)/$(call lowerCase,$(n)).app)

# Host screenshot comparer used by runDos.sh
IMGCMP    = $(OBJDIR)/imgCmp

.PHONY: all clean

all: $(TARGET) $(TEST_APPS) $(IMGCMP)

$(IMGCMP): imgCmp.c $(DVX_MAKEFILES) | $(OBJDIR)
	$(HOSTCC) $(HOST_TOOL_CFLAGS) -I$(LIBDVX)/thirdparty -o $@ imgCmp.c -lm

$(TARGET): $(OBJS) | $(BINDIR)
	$(DXE3GEN) -o $@ -U $(OBJS)

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

# $(1) = the program's directory and source name
define TEST_APP_RULES
$(BINDIR)/$(call lowerCase,$(1)).app: $(OBJDIR)/$(1).o | $(BINDIR)
	$$(DXE3GEN) -o $$@ -U $$<

$(OBJDIR)/$(1).o: $(1)/$(1).c $$(DVX_MAKEFILES) | $(OBJDIR)
	$$(CC) $$(CFLAGS) -c -o $$@ $$<
endef

$(foreach n,$(TEST_APP_NAMES),$(eval $(call TEST_APP_RULES,$(n))))

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

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

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

clean:
	rm -f $(OBJS) $(OBJS:.o=.d) $(TEST_APP_OBJS) $(TEST_APP_OBJS:.o=.d) $(TARGET) $(TEST_APPS) $(IMGCMP) $(IMGCMP).d
