# 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 screen savers Makefile  -- builds the savers DVX ships into
# bin/shared/savers (SHARED\SAVERS, DvxKindSaversE): programs the shell
# starts after the wait with no input (dvxshell/shellSaver.c).
#
# One directory per saver.  A directory <saver>/ holding <saver>/<saver>.c
# is a C saver (all its .c files linked, <saver>.res appended by dvxres);
# one holding <saver>/<saver>.dbp is a BASIC saver (bascomp).  Nothing
# lists them.

include ../../common.mk

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

OBJDIR  = ../../../obj/savers
BINDIR  = ../../../bin/shared/savers
DVXRES  = ../../../bin/host/dvxres
BASCOMP = ../../../bin/host/bascomp

C_SAVERS     = $(sort $(foreach f,$(wildcard */*.c),$(if $(filter $(notdir $(basename $(f))),$(patsubst %/,%,$(dir $(f)))),$(patsubst %/,%,$(dir $(f))))))
BASIC_SAVERS = $(sort $(foreach f,$(wildcard */*.dbp),$(if $(filter $(notdir $(basename $(f))),$(patsubst %/,%,$(dir $(f)))),$(patsubst %/,%,$(dir $(f))))))

saverSrcs = $(wildcard $(1)/*.c)
saverObjs = $(patsubst $(1)/%.c,$(OBJDIR)/$(1)/%.o,$(call saverSrcs,$(1)))
saverRes  = $(wildcard $(1)/$(1).res)

SAVER_OUTS = $(foreach s,$(C_SAVERS) $(BASIC_SAVERS),$(BINDIR)/$(s).app)

.PHONY: all clean

all: $(SAVER_OUTS)

# $(1) = saver directory name.
define C_SAVER_RULES
$(BINDIR)/$(1).app: $(call saverObjs,$(1)) $(call saverRes,$(1))
	@mkdir -p $$(dir $$@)
	$$(DXE3GEN) -o $$@ -U $(call saverObjs,$(1))
	$(if $(call saverRes,$(1)),cd $(1) && $$(abspath $$(DVXRES)) build $$(abspath $$@) $(1).res)

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

# $(1) = saver directory name.
define BASIC_SAVER_RULES
$(BINDIR)/$(1).app: $(wildcard $(1)/*.dbp $(1)/*.bas $(1)/*.frm) $$(BASCOMP)
	@mkdir -p $$(dir $$@)
	$$(BASCOMP) $(1)/$(1).dbp -o $$@ -release
endef

$(foreach s,$(C_SAVERS),$(eval $(call C_SAVER_RULES,$(s))))
$(foreach s,$(BASIC_SAVERS),$(eval $(call BASIC_SAVER_RULES,$(s))))

C_SAVER_OBJS = $(foreach s,$(C_SAVERS),$(call saverObjs,$(s)))

-include $(C_SAVER_OBJS:.o=.d)

clean:
	rm -f $(C_SAVER_OBJS) $(C_SAVER_OBJS:.o=.d) $(SAVER_OUTS)
