# 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 Widget Modules Makefile for DJGPP cross-compilation
#
# Each widget lives in its own subdirectory with its .c, .res, and .bmp.
# Each .wgt is a DXE loaded by the loader at startup.

DJGPP_PREFIX  = $(HOME)/djgpp/djgpp
CC            = $(DJGPP_PREFIX)/bin/i586-pc-msdosdjgpp-gcc
DXE3GEN       = PATH=$(DJGPP_PREFIX)/bin:$(PATH) DJDIR=$(DJGPP_PREFIX)/i586-pc-msdosdjgpp $(DJGPP_PREFIX)/i586-pc-msdosdjgpp/bin/dxe3gen
CFLAGS        = -O2 -Wall -Wextra -Werror -Wno-type-limits -Wno-sign-compare -Wno-format-truncation -march=i486 -mtune=i586 -I../../libs/kpunch/libdvx -I../../libs/kpunch/libdvx/platform -I../../libs/kpunch/libtasks -I../../libs/kpunch/libdvx/thirdparty
DVXRES        = ../../../bin/host/dvxres

OBJDIR = ../../../obj/widgets
WGTDIR = ../../../bin/widgets/kpunch

# Widget name -> directory, source, short name for .wgt/.res
# Format: wgtShortName:dirName:srcFile:resFile
WIDGETS = \
	box:box:widgetBox:box \
	button:button:widgetButton:button \
	canvas:canvas:widgetCanvas:canvas \
	checkbox:checkbox:widgetCheckbox:checkbox \
	combobox:comboBox:widgetComboBox:combobox \
	dropdown:dropdown:widgetDropdown:dropdown \
	imgbtn:imageButton:widgetImageButton:imgbtn \
	image:image:widgetImage:image \
	label:label:widgetLabel:label \
	listbox:listBox:widgetListBox:listbox \
	listview:listView:widgetListView:listview \
	progress:progressBar:widgetProgressBar:progress \
	radio:radio:widgetRadio:radio \
	scrlpane:scrollPane:widgetScrollPane:scrlpane \
	separatr:separator:widgetSeparator:separatr \
	slider:slider:widgetSlider:slider \
	spacer:spacer:widgetSpacer:spacer \
	spinner:spinner:widgetSpinner:spinner \
	splitter:splitter:widgetSplitter:splitter \
	statbar:statusBar:widgetStatusBar:statbar \
	tabctrl:tabControl:widgetTabControl:tabctrl \
	terminal:ansiTerm:widgetAnsiTerm:terminal \
	textinpt:textInput:widgetTextInput:textinpt \
	timer:timer:widgetTimer:timer \
	datactrl:dataCtrl:widgetDataCtrl:datactr \
	dbgrid:dbGrid:widgetDbGrid:dbgrid \
	toolbar:toolbar:widgetToolbar:toolbar \
	treeview:treeView:widgetTreeView:treeview \
	wrapbox:wrapBox:widgetWrapBox:wrapbox

# Extract lists
WGT_NAMES = $(foreach w,$(WIDGETS),$(word 1,$(subst :, ,$w)))
WGT_MODS  = $(foreach n,$(WGT_NAMES),$(WGTDIR)/$(n)/$(n).wgt)
OBJS      = $(foreach w,$(WIDGETS),$(OBJDIR)/$(word 3,$(subst :, ,$w)).o)

# Per-widget extra DXE3GEN flags (e.g. additional -E exports for dlsym)

# shortName:srcDir pairs -- srcDir holds the .dep source file
DEPFILES = textinpt:textInput combobox:comboBox spinner:spinner terminal:ansiTerm listbox:listBox dropdown:dropdown listview:listView treeview:treeView
WGT_DEPS = $(foreach d,$(DEPFILES),$(WGTDIR)/$(word 1,$(subst :, ,$(d)))/$(word 1,$(subst :, ,$(d))).dep)

.PHONY: all clean

all: $(WGT_MODS) $(WGT_DEPS)

define DEP_RULE
$(WGTDIR)/$(word 1,$(subst :, ,$(1)))/$(word 1,$(subst :, ,$(1))).dep: $(word 2,$(subst :, ,$(1)))/$(word 1,$(subst :, ,$(1))).dep | $(WGTDIR)/$(word 1,$(subst :, ,$(1)))
	cp $$< $$@
endef

$(foreach d,$(DEPFILES),$(eval $(call DEP_RULE,$(d))))

# Compile: source is in subdirectory
WIDGET_DEPS_H = ../../libs/kpunch/libdvx/dvxWgtP.h ../../libs/kpunch/libdvx/dvxWgt.h ../../libs/kpunch/libdvx/dvxTypes.h ../../libs/kpunch/libdvx/dvxApp.h ../../libs/kpunch/libdvx/dvxDraw.h ../../libs/kpunch/libdvx/dvxWm.h ../../libs/kpunch/libdvx/dvxVideo.h ../../libs/kpunch/libdvx/platform/dvxPlat.h

define WIDGET_RULES
$(OBJDIR)/$(word 3,$(subst :, ,$1)).o: $(word 2,$(subst :, ,$1))/$(word 3,$(subst :, ,$1)).c $$(WIDGET_DEPS_H) | $(OBJDIR)
	$$(CC) $$(CFLAGS) -c -o $$@ $$<

$(WGTDIR)/$(word 1,$(subst :, ,$1))/$(word 1,$(subst :, ,$1)).wgt: $(OBJDIR)/$(word 3,$(subst :, ,$1)).o $$(wildcard $(word 2,$(subst :, ,$1))/$(word 4,$(subst :, ,$1)).res) $$(wildcard $(word 2,$(subst :, ,$1))/*.bmp) | $(WGTDIR)/$(word 1,$(subst :, ,$1))
	$$(DXE3GEN) -o $(WGTDIR)/$(word 1,$(subst :, ,$1))/$(word 1,$(subst :, ,$1)).dxe -U $$<
	mv $(WGTDIR)/$(word 1,$(subst :, ,$1))/$(word 1,$(subst :, ,$1)).dxe $$@
	@if [ -f $(word 2,$(subst :, ,$1))/$(word 4,$(subst :, ,$1)).res ]; then \
		cd $(word 2,$(subst :, ,$1)) && ../$(DVXRES) build ../$$@ $(word 4,$(subst :, ,$1)).res; \
	fi

$(WGTDIR)/$(word 1,$(subst :, ,$1)):
	mkdir -p $$@
endef

$(foreach w,$(WIDGETS),$(eval $(call WIDGET_RULES,$w)))

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

$(WGTDIR):
	mkdir -p $(WGTDIR)

clean:
	rm -f $(OBJS) $(WGT_MODS) $(WGT_DEPS)
	-rmdir $(WGTDIR) 2>/dev/null
