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

# The DVX installer.
#
#   bin/setup.exe     SETUP.EXE: one static DOS program -- the DVX core,
#                     the widgets Setup uses, fileops, miniz's inflate and
#                     the Setup program -- linked with --gc-sections so
#                     only what Setup reaches is in it.  Also shipped in
#                     the package (the installed copy uninstalls).
#   bin/host/dvxpkg   the package tool `make dist` runs (src/installer/
#                     mkDist.sh).
#
# The core sources are taken as they are (every libdvx/*.c and the DOS
# platform files, into an archive so only the objects used are linked);
# the widgets are compiled with their wgtRegister renamed to
# wgtRegister<Name>, which setup.c calls.

include ../common.mk

LIBDVX     = ../libs/kpunch/libdvx
KPUNCH     = ../libs/kpunch
WIDGETS    = ../widgets/kpunch
MINIZ      = thirdparty/miniz
LICENSE    = ../../LICENSE.txt

OBJDIR     = ../../obj/installer
DOSOBJ     = $(OBJDIR)/dos
HOSTOBJ    = $(OBJDIR)/host
BINDIR     = ../../bin
HOSTBIN    = ../../bin/host
TARGET     = $(BINDIR)/setup.exe
TOOL       = $(HOSTBIN)/dvxpkg
LICENSE_H  = $(OBJDIR)/setupLicense.h

INCLUDES   = -I. -I$(MINIZ) -I$(OBJDIR) -I$(LIBDVX) -I$(LIBDVX)/platform -I$(LIBDVX)/thirdparty -I$(KPUNCH) -I$(KPUNCH)/libtasks -I$(WIDGETS)
MINIZ_DOS  = -DMINIZ_NO_ARCHIVE_APIS -DMINIZ_NO_STDIO -DMINIZ_NO_TIME -DMINIZ_NO_DEFLATE_APIS -DMINIZ_NO_ZLIB_COMPATIBLE_NAMES
CFLAGS     = $(DOS_CFLAGS) -ffunction-sections -fdata-sections $(INCLUDES) $(MINIZ_DOS)
LDFLAGS    = -s -Wl,--gc-sections -lm

# Setup's own sources; the core, fileops and text help as they are
SETUP_SRCS = setup.c setupCore.c pkgRead.c pkgFormat.c $(MINIZ)/miniz.c
CORE_SRCS  = $(wildcard $(LIBDVX)/*.c) $(LIBDVX)/platform/dvxPlatformDos.c $(LIBDVX)/platform/dvxPlatformIrq.c $(LIBDVX)/platform/dvxPlatformUtil.c $(LIBDVX)/platform/dvxMemTrack.c $(LIBDVX)/thirdparty/stb_ds_impl.c ../tools/dvxResWrite.c $(wildcard $(KPUNCH)/fileops/*.c) $(KPUNCH)/texthelp/textHelp.c

# The widgets Setup uses: <folder>/<source>:<Name> (wgtRegister -> wgtRegister<Name>)
SETUP_WGTS = box/widgetBox:Box button/widgetButton:Button checkbox/widgetCheckbox:Checkbox label/widgetLabel:Label progressBar/widgetProgressBar:Progress spacer/widgetSpacer:Spacer textInput/widgetTextInput:TextInput

SETUP_OBJS = $(patsubst %.c,$(DOSOBJ)/%.o,$(notdir $(SETUP_SRCS)))
CORE_OBJS  = $(patsubst %.c,$(DOSOBJ)/core/%.o,$(notdir $(CORE_SRCS)))
WGT_OBJS   = $(foreach w,$(SETUP_WGTS),$(DOSOBJ)/wgt/$(notdir $(firstword $(subst :, ,$w))).o)
CORE_LIB   = $(DOSOBJ)/setupCore.a

# The package tool (host)
TOOL_SRCS  = pkgTool.c pkgBuild.c pkgFormat.c $(MINIZ)/miniz.c
TOOL_OBJS  = $(patsubst %.c,$(HOSTOBJ)/%.o,$(notdir $(TOOL_SRCS)))
TOOL_FLAGS = $(HOST_TOOL_CFLAGS) -I. -I$(MINIZ)

vpath %.c . $(MINIZ)

.PHONY: all clean

all: $(TARGET) $(TOOL)

$(TARGET): $(SETUP_OBJS) $(CORE_LIB) | $(BINDIR)
	$(CC) -o $@ $(SETUP_OBJS) $(CORE_LIB) $(LDFLAGS)
	$(EXE2COFF) $@
	cat $(CWSDSTUB) $(BINDIR)/setup > $@
	rm -f $(BINDIR)/setup

$(CORE_LIB): $(CORE_OBJS) $(WGT_OBJS)
	rm -f $@
	$(DJGPP_PREFIX)/bin/$(DJGPP_TRIPLE)-ar rcs $@ $^

$(DOSOBJ)/%.o: %.c $(DVX_MAKEFILES) | $(LICENSE_H)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c -o $@ $<

define CORE_RULE
$(DOSOBJ)/core/$(notdir $(1:.c=.o)): $1 $$(DVX_MAKEFILES)
	@mkdir -p $$(dir $$@)
	$$(CC) $$(CFLAGS) -c -o $$@ $$<
endef

$(foreach s,$(CORE_SRCS),$(eval $(call CORE_RULE,$s)))

define WGT_RULE
$(DOSOBJ)/wgt/$(notdir $(firstword $(subst :, ,$1))).o: $(WIDGETS)/$(firstword $(subst :, ,$1)).c $$(DVX_MAKEFILES)
	@mkdir -p $$(dir $$@)
	$$(CC) $$(CFLAGS) -DwgtRegister=wgtRegister$(lastword $(subst :, ,$1)) -c -o $$@ $$<
endef

$(foreach w,$(SETUP_WGTS),$(eval $(call WGT_RULE,$w)))

# LICENSE.txt as a C string, in ASCII (the copyright sign becomes (C)),
# its paragraphs re-flowed to the width of Setup's text box.
LICENSE_COLS = 58

$(LICENSE_H): $(LICENSE) $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	{ echo '// Generated from LICENSE.txt by src/installer/Makefile'; \
	  echo 'static const char sSetupLicense[] ='; \
	  tr -d '\r' < $< | iconv -f UTF-8 -t ASCII//TRANSLIT | fmt -w $(LICENSE_COLS) | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/^/    "/' -e 's/$$/\\n"/'; \
	  echo ';'; } > $@.tmp && mv $@.tmp $@

$(TOOL): $(TOOL_OBJS) | $(HOSTBIN)
	$(HOSTCC) -o $@ $(TOOL_OBJS)

$(HOSTOBJ)/%.o: %.c $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	$(HOSTCC) $(TOOL_FLAGS) -c -o $@ $<

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

-include $(SETUP_OBJS:.o=.d) $(CORE_OBJS:.o=.d) $(WGT_OBJS:.o=.d) $(TOOL_OBJS:.o=.d)

clean:
	rm -rf $(OBJDIR) $(TARGET) $(TOOL)
