# 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 Shell Applications Makefile  -- builds DXE3 modules
#
# Source tree is now one dir per app, mirroring bin/apps/kpunch/.

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/libdvx/thirdparty -I../../widgets/kpunch -I../../libs/kpunch/libtasks -I../../libs/kpunch/dvxshell

OBJDIR = ../../../obj/apps
BINDIR = ../../../bin/apps
DVXRES = ../../../bin/host/dvxres

# C apps: one directory, one .c file each
C_APPS = progman clock dvxdemo cpanel dvxhelp

BASCOMP   = ../../../bin/host/bascomp
# BASIC apps: each is a .dbp project in its own directory.
# BASIC-only notepad, imgview, etc. replace the old C versions.
BASIC_APPS = iconed notepad imgview helpedit resedit basdemo

.PHONY: all clean $(C_APPS) dvxbasic $(BASIC_APPS)

all: $(C_APPS) dvxbasic $(BASIC_APPS)

dvxbasic:
	$(MAKE) -C dvxbasic

cpanel:  $(BINDIR)/kpunch/cpanel/cpanel.app
progman: $(BINDIR)/kpunch/progman/progman.app
clock:   $(BINDIR)/kpunch/clock/clock.app
dvxdemo: $(BINDIR)/kpunch/dvxdemo/dvxdemo.app
dvxhelp: $(BINDIR)/kpunch/dvxhelp/dvxhelp.app

$(BINDIR)/kpunch/cpanel/cpanel.app: $(OBJDIR)/cpanel.o cpanel/cpanel.res cpanel/icon32.bmp | $(BINDIR)/kpunch/cpanel
	$(DXE3GEN) -o $@ -U $<
	cd cpanel && ../$(DVXRES) build ../$@ cpanel.res

$(BINDIR)/kpunch/progman/progman.app: $(OBJDIR)/progman.o | $(BINDIR)/kpunch/progman
	$(DXE3GEN) -o $@ -U $<

$(BINDIR)/kpunch/clock/clock.app: $(OBJDIR)/clock.o clock/clock.res clock/icon32.bmp | $(BINDIR)/kpunch/clock
	$(DXE3GEN) -o $@ -U $<
	cd clock && ../$(DVXRES) build ../$@ clock.res

DVXDEMO_BMPS = logo.bmp new.bmp open.bmp sample.bmp save.bmp

$(BINDIR)/kpunch/dvxdemo/dvxdemo.app: $(OBJDIR)/dvxdemo.o $(addprefix dvxdemo/,$(DVXDEMO_BMPS)) dvxdemo/dvxdemo.res dvxdemo/icon32.bmp | $(BINDIR)/kpunch/dvxdemo
	$(DXE3GEN) -o $@ -U $<
	cd dvxdemo && ../$(DVXRES) build ../$@ dvxdemo.res
	cp $(addprefix dvxdemo/,$(DVXDEMO_BMPS)) $(BINDIR)/kpunch/dvxdemo/

$(BINDIR)/kpunch/dvxhelp/dvxhelp.app: $(OBJDIR)/dvxhelp.o dvxhelp/dvxhelp.res dvxhelp/icon32.bmp | $(BINDIR)/kpunch/dvxhelp
	$(DXE3GEN) -o $@ -U $<
	cd dvxhelp && ../$(DVXRES) build ../$@ dvxhelp.res

$(OBJDIR)/cpanel.o:  cpanel/cpanel.c   | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<
$(OBJDIR)/progman.o: progman/progman.c | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<
$(OBJDIR)/clock.o:   clock/clock.c     | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<
$(OBJDIR)/dvxdemo.o: dvxdemo/dvxdemo.c | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<
$(OBJDIR)/dvxhelp.o: dvxhelp/dvxhelp.c dvxhelp/hlpformat.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

# BASIC apps (compiled from .dbp projects via bascomp). Source for each app
# is under the app's own directory.
iconed: $(BINDIR)/kpunch/iconed/iconed.app

$(BINDIR)/kpunch/iconed/iconed.app: iconed/iconed.dbp iconed/iconed.frm iconed/ICON32.BMP $(BASCOMP) | $(BINDIR)/kpunch/iconed dvxbasic
	$(BASCOMP) iconed/iconed.dbp -o $@ -release

notepad: $(BINDIR)/kpunch/notepad/notepad.app

$(BINDIR)/kpunch/notepad/notepad.app: notepad/notepad.dbp notepad/notepad.frm notepad/ICON32.BMP $(BASCOMP) | $(BINDIR)/kpunch/notepad dvxbasic
	$(BASCOMP) notepad/notepad.dbp -o $@ -release

imgview: $(BINDIR)/kpunch/imgview/imgview.app

$(BINDIR)/kpunch/imgview/imgview.app: imgview/imgview.dbp imgview/imgview.frm imgview/ICON32.BMP $(BASCOMP) | $(BINDIR)/kpunch/imgview dvxbasic
	$(BASCOMP) imgview/imgview.dbp -o $@ -release

helpedit: $(BINDIR)/kpunch/dvxhelp/helpedit.app

$(BINDIR)/kpunch/dvxhelp/helpedit.app: dvxhelp/helpedit/helpedit.dbp dvxhelp/helpedit/helpedit.frm dvxhelp/helpedit/ICON32.BMP $(BASCOMP) | $(BINDIR)/kpunch/dvxhelp dvxbasic
	$(BASCOMP) dvxhelp/helpedit/helpedit.dbp -o $@ -release
	$(DVXRES) add $@ helpfile text "dvxhelp.hlp"

resedit: $(BINDIR)/kpunch/resedit/resedit.app

$(BINDIR)/kpunch/resedit/resedit.app: resedit/resedit.dbp resedit/resedit.frm resedit/ICON32.BMP $(BASCOMP) | $(BINDIR)/kpunch/resedit dvxbasic
	$(BASCOMP) resedit/resedit.dbp -o $@ -release

basdemo: $(BINDIR)/kpunch/basdemo/basdemo.app

$(BINDIR)/kpunch/basdemo/basdemo.app: basdemo/basdemo.dbp basdemo/basdemo.frm basdemo/ICON32.BMP $(BASCOMP) | $(BINDIR)/kpunch/basdemo dvxbasic
	$(BASCOMP) basdemo/basdemo.dbp -o $@ -release

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

$(BINDIR)/kpunch/cpanel:    ; mkdir -p $@
$(BINDIR)/kpunch/progman:   ; mkdir -p $@
$(BINDIR)/kpunch/clock:     ; mkdir -p $@
$(BINDIR)/kpunch/dvxdemo:   ; mkdir -p $@
$(BINDIR)/kpunch/dvxhelp:   ; mkdir -p $@
$(BINDIR)/kpunch/iconed:    ; mkdir -p $@
$(BINDIR)/kpunch/notepad:   ; mkdir -p $@
$(BINDIR)/kpunch/imgview:   ; mkdir -p $@
$(BINDIR)/kpunch/resedit:   ; mkdir -p $@
$(BINDIR)/kpunch/basdemo: ; mkdir -p $@

# Header dependencies
COMMON_H = ../../libs/kpunch/libdvx/dvxApp.h ../../libs/kpunch/libdvx/dvxDlg.h ../../libs/kpunch/libdvx/dvxWgt.h ../../libs/kpunch/libdvx/dvxWm.h ../../libs/kpunch/libdvx/dvxVideo.h ../../libs/kpunch/dvxshell/shellApp.h
$(OBJDIR)/cpanel.o:  cpanel/cpanel.c   $(COMMON_H) ../../libs/kpunch/libdvx/dvxPrefs.h ../../libs/kpunch/libdvx/platform/dvxPlat.h
$(OBJDIR)/progman.o: progman/progman.c $(COMMON_H) ../../libs/kpunch/dvxshell/shellInf.h
$(OBJDIR)/clock.o:   clock/clock.c     ../../libs/kpunch/libdvx/dvxApp.h ../../libs/kpunch/libdvx/dvxWgt.h ../../libs/kpunch/libdvx/dvxDraw.h ../../libs/kpunch/libdvx/dvxVideo.h ../../libs/kpunch/dvxshell/shellApp.h ../../libs/kpunch/libtasks/taskSwch.h
$(OBJDIR)/dvxdemo.o: dvxdemo/dvxdemo.c $(COMMON_H)

clean:
	rm -f $(OBJDIR)/*.o
	rm -f $(BINDIR)/kpunch/cpanel/cpanel.app
	rm -f $(BINDIR)/kpunch/progman/progman.app
	rm -f $(BINDIR)/kpunch/clock/clock.app
	rm -f $(BINDIR)/kpunch/dvxdemo/dvxdemo.app $(addprefix $(BINDIR)/kpunch/dvxdemo/,$(DVXDEMO_BMPS))
	rm -f $(BINDIR)/kpunch/dvxhelp/dvxhelp.app
	rm -f $(BINDIR)/kpunch/iconed/iconed.app
	rm -f $(BINDIR)/kpunch/notepad/notepad.app
	rm -f $(BINDIR)/kpunch/imgview/imgview.app
	rm -f $(BINDIR)/kpunch/dvxhelp/helpedit.app
	rm -f $(BINDIR)/kpunch/resedit/resedit.app
	rm -f $(BINDIR)/kpunch/basdemo/basdemo.app
	$(MAKE) -C dvxbasic clean
