# 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 GUI  -- Top-level Makefile
#
# Builds the full DVX stack: core library, task switcher,
# bootstrap loader, text help library, widgets, shell, and apps.
#
# Source tree mirrors bin/ layout:
#   src/libs/kpunch/<libname>/   -> bin/libs/kpunch/<libname>/<libname>.lib
#   src/widgets/kpunch/<widget>/ -> bin/widgets/kpunch/<widget>/<widget>.wgt
#   src/apps/kpunch/<app>/       -> bin/apps/kpunch/<app>/<app>.app
#   src/loader/                  -> bin/DVX.EXE
#   src/tools/                   -> bin/host/<tool>

.PHONY: all clean libdvx libtasks loader texthelp listhelp widgets dvxshell taskmgr serial sql apps tools deploy-helpsrc compile-help deploy-sdk

all: libdvx libtasks loader texthelp listhelp tools widgets dvxshell taskmgr serial sql apps deploy-helpsrc compile-help deploy-sdk

libdvx:
	$(MAKE) -C src/libs/kpunch/libdvx

libtasks:
	$(MAKE) -C src/libs/kpunch/libtasks

loader: libdvx libtasks
	$(MAKE) -C src/loader

texthelp: libdvx libtasks
	$(MAKE) -C src/libs/kpunch/texthelp

listhelp: libdvx libtasks
	$(MAKE) -C src/libs/kpunch/listhelp

widgets: libdvx libtasks texthelp listhelp
	$(MAKE) -C src/widgets/kpunch

dvxshell: libdvx libtasks
	$(MAKE) -C src/libs/kpunch/dvxshell

taskmgr: dvxshell
	$(MAKE) -C src/libs/kpunch/taskmgr

serial: libdvx libtasks
	$(MAKE) -C src/libs/kpunch/serial

sql: libdvx libtasks
	$(MAKE) -C src/libs/kpunch/sql

tools:
	$(MAKE) -C src/tools

apps: libdvx libtasks dvxshell tools
	$(MAKE) -C src/apps/kpunch

deploy-helpsrc:
	$(MAKE) -C src/tools deploy-helpsrc

HLPC = bin/host/dvxhlpc

SYSTEM_DHS = src/libs/kpunch/libdvx/sysdoc.dhs \
	src/libs/kpunch/libdvx/arch.dhs src/libs/kpunch/libdvx/apiref.dhs \
	src/libs/kpunch/libtasks/libtasks.dhs src/libs/kpunch/dvxshell/dvxshell.dhs src/libs/kpunch/sql/dvxsql.dhs \
	src/libs/kpunch/texthelp/texthelp.dhs src/libs/kpunch/listhelp/listhelp.dhs \
	src/libs/kpunch/taskmgr/taskmgr.dhs src/libs/kpunch/serial/serial.dhs \
	src/apps/kpunch/dvxbasic/basrt.dhs \
	src/widgets/kpunch/wgtsys.dhs

BASIC_DHS = src/apps/kpunch/dvxbasic/ideguide.dhs src/apps/kpunch/dvxbasic/langref.dhs \
	src/apps/kpunch/dvxbasic/ctrlover.dhs src/apps/kpunch/dvxbasic/form.dhs

compile-help:
	@mkdir -p docs
	$(HLPC) -o bin/apps/kpunch/dvxhelp/dvxhelp.hlp \
		--html docs/dvx_help_viewer.html \
		src/apps/kpunch/dvxhelp/help.dhs
	$(HLPC) -o bin/apps/kpunch/progman/dvxhelp.hlp \
		--html docs/dvx_system_reference.html \
		-i src/libs/kpunch/libdvx \
		$(SYSTEM_DHS) \
		$$(find src/widgets/kpunch -name "*.dhs" ! -path "*/wgtsys.dhs" | sort)
	$(HLPC) -o bin/apps/kpunch/dvxbasic/dvxbasic.hlp \
		--html docs/dvx_basic_reference.html \
		$(BASIC_DHS) \
		$$(find src/widgets/kpunch -name "*.bhs" | sort)

SDKDIR = bin/sdk

deploy-sdk:
	@echo "Building SDK..."
	@mkdir -p $(SDKDIR)/include/core $(SDKDIR)/include/shell $(SDKDIR)/include/tasks $(SDKDIR)/include/sql $(SDKDIR)/include/basic $(SDKDIR)/samples/basic/basdemo $(SDKDIR)/samples/basic/widshow
	@# Core headers (libdvx public API)
	@for f in src/libs/kpunch/libdvx/dvxApp.h src/libs/kpunch/libdvx/dvxTypes.h \
		src/libs/kpunch/libdvx/dvxWgt.h src/libs/kpunch/libdvx/dvxWgtP.h \
		src/libs/kpunch/libdvx/dvxWm.h src/libs/kpunch/libdvx/dvxDraw.h \
		src/libs/kpunch/libdvx/dvxVideo.h src/libs/kpunch/libdvx/dvxComp.h \
		src/libs/kpunch/libdvx/dvxPrefs.h src/libs/kpunch/libdvx/dvxDlg.h \
		src/libs/kpunch/libdvx/dvxRes.h src/libs/kpunch/libdvx/dvxFont.h \
		src/libs/kpunch/libdvx/dvxCur.h src/libs/kpunch/libdvx/dvxPal.h \
		src/libs/kpunch/libdvx/dvxMem.h src/libs/kpunch/libdvx/platform/dvxPlat.h; do \
		[ -f "$$f" ] && cp "$$f" $(SDKDIR)/include/core/; \
	done
	@# Shell header
	@cp src/libs/kpunch/dvxshell/shellApp.h $(SDKDIR)/include/shell/
	@cp src/libs/kpunch/dvxshell/shellInf.h $(SDKDIR)/include/shell/
	@# Tasks header
	@cp src/libs/kpunch/libtasks/taskSwch.h $(SDKDIR)/include/tasks/
	@# SQL header
	@cp src/libs/kpunch/sql/dvxSql.h $(SDKDIR)/include/sql/
	@# Widget headers -- one subdir per widget
	@for d in src/widgets/kpunch/*/; do \
		for h in "$$d"*.h; do \
			[ -f "$$h" ] || continue; \
			wgt=$$(basename "$$d"); \
			mkdir -p $(SDKDIR)/include/widget/"$$wgt"; \
			cp "$$h" $(SDKDIR)/include/widget/"$$wgt"/; \
		done; \
	done
	@# BASIC include files
	@cp src/include/basic/*.bas $(SDKDIR)/include/basic/
	@# BASIC sample: basdemo (project file + form + icon)
	@cp src/apps/kpunch/basdemo/basdemo.dbp  $(SDKDIR)/samples/basic/basdemo/
	@cp src/apps/kpunch/basdemo/basdemo.frm  $(SDKDIR)/samples/basic/basdemo/
	@cp src/apps/kpunch/basdemo/ICON32.BMP   $(SDKDIR)/samples/basic/basdemo/
	@# BASIC sample: widshow (widget showcase)
	@cp src/apps/kpunch/widshow/widshow.dbp  $(SDKDIR)/samples/basic/widshow/
	@cp src/apps/kpunch/widshow/widshow.frm  $(SDKDIR)/samples/basic/widshow/
	@cp src/apps/kpunch/widshow/ICON32.BMP   $(SDKDIR)/samples/basic/widshow/
	@# README
	@printf '%s\n' \
		'DVX SDK' \
		'=======' \
		'' \
		'Headers for developing applications and widgets against the DVX GUI.' \
		'' \
		'Directory Structure' \
		'-------------------' \
		'' \
		'  include/' \
		'    core/       DVX core headers (types, drawing, windows, widgets)' \
		'    shell/      Shell/app loading API' \
		'    tasks/      Cooperative task switching API' \
		'    sql/        SQLite database wrapper API' \
		'    widget/     Per-widget public API headers' \
		'    basic/      BASIC include files (DECLARE LIBRARY modules)' \
		'  samples/' \
		'    basic/      Example BASIC projects (open in the DVX BASIC IDE)' \
		'' \
		'Requirements' \
		'------------' \
		'' \
		'  - DJGPP cross-compiler (i586-pc-msdosdjgpp-gcc)' \
		'  - dxe3gen (included with DJGPP)' \
		'  - DVX resource compiler (SYSTEM/DVXRES.EXE on the target,' \
		'    or bin/host/dvxres on the host)' \
		'  - DVX help compiler (SYSTEM/DVXHLPC.EXE on the target,' \
		'    or bin/host/dvxhlpc on the host)' \
		'' \
		'For complete working examples, see the app sources under' \
		'src/apps/kpunch/ in the DVX repository.' \
		> $(SDKDIR)/README.TXT

clean:
	$(MAKE) -C src/libs/kpunch/libdvx clean
	$(MAKE) -C src/libs/kpunch/libtasks clean
	$(MAKE) -C src/loader clean
	$(MAKE) -C src/libs/kpunch/texthelp clean
	$(MAKE) -C src/libs/kpunch/listhelp clean
	$(MAKE) -C src/widgets/kpunch clean
	$(MAKE) -C src/libs/kpunch/dvxshell clean
	$(MAKE) -C src/libs/kpunch/taskmgr clean
	$(MAKE) -C src/libs/kpunch/serial clean
	$(MAKE) -C src/libs/kpunch/sql clean
	$(MAKE) -C src/apps/kpunch clean
	$(MAKE) -C src/tools clean
	-rmdir obj 2>/dev/null
	-rm -rf bin/config bin/widgets bin/libs bin/sdk
	-rm -f docs/*.html
