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

# Package Library Makefile for DJGPP cross-compilation
#
# Builds pkg.lib (a DXE3 module): add-on packages -- the ZIP reader and
# writer, manifests, keys and Ed25519 signatures, .DPK files, the
# installed-package records, the keyring, planning, and the stepwise
# install and remove operations.  miniz (inflate and
# deflate, no archive API) and Monocypher (with its Ed25519 file) are
# built in; SETUP.EXE and the package tool compile the same sources.
# Also SYSTEM\DVXPKG.EXE, the package tool on DOS (pkgDosMain.c over
# pkgCli.c), a program of its own like DVXRES.EXE.

include ../../../common.mk

MINIZ   = thirdparty/miniz
MONO    = thirdparty/monocypher/src
CFLAGS  = $(DOS_CFLAGS) -I. -I.. -I../libdvx -I../libdvx/platform -I../libdvx/thirdparty -I$(MINIZ) -I$(MONO) $(PKG_MINIZ_FLAGS)

OBJDIR  = ../../../../obj/pkg
LIBSDIR = ../../../../bin/libs

SRCS   = pkgBas.c pkgDb.c pkgDpk.c pkgFormat.c pkgIni.c pkgManifest.c pkgOp.c pkgPlan.c pkgSign.c pkgTrust.c pkgZip.c pkgZipWrite.c $(MINIZ)/miniz.c $(MONO)/monocypher.c $(MONO)/optional/monocypher-ed25519.c
OBJS   = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))

TARGETDIR = $(LIBSDIR)/kpunch/pkg
TARGET    = $(TARGETDIR)/pkg.lib

SYSTEMDIR  = ../../../../bin/system
TOOL       = $(SYSTEMDIR)/DVXPKG.EXE
TOOLOBJDIR = $(OBJDIR)/tool
TOOL_SRCS  = pkgDosMain.c pkgCli.c $(filter-out pkgBas.c,$(SRCS)) ../serial/security/secHash.c ../serial/security/security.c ../libdvx/platform/dvxPlatformUtil.c ../libdvx/thirdparty/stb_ds_impl.c
TOOL_OBJS  = $(patsubst %.c,$(TOOLOBJDIR)/%.o,$(notdir $(TOOL_SRCS)))

vpath %.c . $(MINIZ) $(MONO) $(MONO)/optional ../serial/security ../libdvx/platform ../libdvx/thirdparty

.PHONY: all clean

all: $(TARGET) $(TARGETDIR)/pkg.dep $(TOOL)

$(TARGETDIR)/pkg.dep: pkg.dep | $(TARGETDIR)
	sed 's/$$/\r/' $< > $@

$(TARGET): $(OBJS) | $(TARGETDIR)
	$(DXE3GEN) -o $(TARGETDIR)/pkg.dxe -U $(OBJS)
	mv $(TARGETDIR)/pkg.dxe $@

$(OBJDIR)/%.o: %.c $(DVX_MAKEFILES) | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

# DOS executable: link (stripped: it ships on the floppies), convert to
# COFF, prepend the DPMI stub
$(TOOL): $(TOOL_OBJS) | $(SYSTEMDIR)
	$(CC) -s -o $(SYSTEMDIR)/dvxpkg.exe $^ -lm
	$(EXE2COFF) $(SYSTEMDIR)/dvxpkg.exe
	cat $(CWSDSTUB) $(SYSTEMDIR)/dvxpkg > $@
	rm -f $(SYSTEMDIR)/dvxpkg $(SYSTEMDIR)/dvxpkg.exe

$(TOOLOBJDIR)/%.o: %.c $(DVX_MAKEFILES) | $(TOOLOBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(TOOLOBJDIR) $(SYSTEMDIR):
	mkdir -p $@

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

$(TARGETDIR):
	mkdir -p $(TARGETDIR)

-include $(OBJS:.o=.d) $(TOOL_OBJS:.o=.d)

clean:
	rm -rf $(OBJS) $(OBJS:.o=.d) $(TARGET) $(TARGETDIR)/pkg.dep $(TARGETDIR) $(TOOLOBJDIR) $(TOOL)
