# DoorBench - Copyright (C) 2026 Michael Slater
# SPDX-License-Identifier: GPL-2.0-or-later
FPC ?= fpc
PY ?= python3
WINUNITS ?= $(HOME)/fpc-win64/lib/fpc/3.2.2/units/x86_64-win64
DOSROOT ?= $(HOME)/fpc-i8086/lib/fpc/3.2.2
VERSION := $(shell sed -n "s/.*DBVersion = '\(.*\)'.*/\1/p" engine/src/version.inc)
FLAGS = -Mobjfpc -Sh -O2 -vew -Fuengine/src -Fiengine/src
STRIP = -XX -Xs
DOSFLAGS = -Tmsdos -WmLarge -Cs32768 $(STRIP) $(foreach u,rtl rtl-objpas rtl-console rtl-extra,-Fu$(DOSROOT)/units/msdos/$(u))
PROGS = hello dbrun dbmaint dbsetup
TOOLS = dbc
TESTPROGS = dbrun dbmaint dbsetup
GAME = engine/tests/game
.PHONY: engines editor editor-win test-editor all linux windows dos test test-unit test-linux test-dos test-wine test-engine test-same testbuild clean version
all: linux windows dos sim simwin testtools testtoolsw
version:
	@echo $(VERSION)
linux:
	@mkdir -p build/linux bin/linux
	for p in $(PROGS) $(TOOLS); do $(FPC) $(FLAGS) $(STRIP) -FUbuild/linux -FEbin/linux engine/src/$$p.pas || exit 1; done
windows:
	@mkdir -p build/windows bin/windows
	for p in $(PROGS) $(TOOLS); do $(FPC) $(FLAGS) $(STRIP) -Twin64 -Px86_64 -Fu$(WINUNITS)/* -FUbuild/windows -FEbin/windows engine/src/$$p.pas || exit 1; done
dos:
	@mkdir -p build/dos bin/dos
	for p in $(PROGS); do $(DOSROOT)/ppcross8086 $(FLAGS) $(DOSFLAGS) -FUbuild/dos -FEbin/dos engine/src/$$p.pas || exit 1; done
sim: linux
	@mkdir -p build/sim
	$(FPC) $(FLAGS) $(STRIP) -dDBSIM -FUbuild/sim -FEbin/linux engine/src/dbsim.pas
simwin: windows
	@mkdir -p build/simw
	$(FPC) $(FLAGS) $(STRIP) -dDBSIM -Twin64 -Px86_64 -Fu$(WINUNITS)/* -FUbuild/simw -FEbin/windows engine/src/dbsim.pas
# what the editor uses to try a game: the door with its link, and maintenance on a made-up date
testtools: linux
	@mkdir -p build/tt/u build/tt/m
	$(FPC) $(FLAGS) $(STRIP) -dDBTESTBUILD -dDBTESTLINK -FUbuild/tt/u -obin/linux/dbtest engine/src/dbrun.pas
	$(FPC) $(FLAGS) $(STRIP) -dDBTESTBUILD -FUbuild/tt/m -obin/linux/dbtmaint engine/src/dbmaint.pas
testtoolsw: windows
	@mkdir -p build/ttw/u build/ttw/m
	$(FPC) $(FLAGS) $(STRIP) -dDBTESTBUILD -dDBTESTLINK -Twin64 -Px86_64 -Fu$(WINUNITS)/* -FUbuild/ttw/u -obin/windows/dbtest.exe engine/src/dbrun.pas
	$(FPC) $(FLAGS) $(STRIP) -dDBTESTBUILD -Twin64 -Px86_64 -Fu$(WINUNITS)/* -FUbuild/ttw/m -obin/windows/dbtmaint.exe engine/src/dbmaint.pas
# test builds: a fixed seed and date, and stopping dead at a chosen point
testbuild: linux sim
	@mkdir -p build/tl/u build/tw/u build/td/u
	for p in $(TESTPROGS); do $(FPC) $(FLAGS) -gl -dDBTESTBUILD -dDBCRASHTEST -FUbuild/tl/u -FEbuild/tl engine/src/$$p.pas || exit 1; done
	for p in $(TESTPROGS); do $(FPC) $(FLAGS) -dDBTESTBUILD -Twin64 -Px86_64 -Fu$(WINUNITS)/* -FUbuild/tw/u -FEbuild/tw engine/src/$$p.pas || exit 1; done
	for p in $(TESTPROGS); do $(DOSROOT)/ppcross8086 $(FLAGS) $(DOSFLAGS) -dDBTESTBUILD -FUbuild/td/u -FEbuild/td engine/src/$$p.pas || exit 1; done
	bin/linux/dbc $(GAME) -o build/TESTGAME.DBP
	bin/linux/dbc games/goldfield -o build/GOLDCLM.DBP
# the editor: needs Rust, which the rest does not
editor:
	cd editor && cargo build --release
editor-win:
	cd editor && cargo build --release --target x86_64-pc-windows-gnu
test-editor: testtools sim
	cd editor && DOORBENCH_TOOLS=$(CURDIR)/bin/linux cargo test
test: test-unit test-linux test-dos test-wine test-engine test-same
test-unit:
	@mkdir -p build/tests
	$(FPC) $(FLAGS) -dDBTESTIO -gl -Cr -Co -FUbuild/tests -FEbuild/tests engine/tests/testkit.pas
	build/tests/testkit
test-linux: linux
	$(PY) engine/tests/test_linux.py
test-dos: dos
	@mkdir -p build/tests/dos
	$(DOSROOT)/ppcross8086 $(FLAGS) $(DOSFLAGS) -dDBDROPTEST -FUbuild/tests/dos -FEbuild/tests/dos engine/src/hello.pas
	$(PY) engine/tests/test_dos.py
test-engine: testbuild
	$(PY) engine/tests/test_engine.py
	$(PY) engine/tests/test_game.py
test-same: testbuild
	$(PY) engine/tests/test_same.py
	$(PY) engine/tests/test_same.py gold
test-wine: windows
	@mkdir -p build/tests/win
	$(FPC) $(FLAGS) -Twin64 -Px86_64 -Fu$(WINUNITS)/* -FUbuild/tests/win -FEbuild/tests/win engine/tests/wsock.pas
	$(FPC) $(FLAGS) -dDBWINETEST -Twin64 -Px86_64 -Fu$(WINUNITS)/* -FUbuild/tests/win -FEbuild/tests/win engine/src/hello.pas
	$(PY) engine/tests/test_wine.py
clean:
	rm -rf $(CURDIR)/build $(CURDIR)/bin
# everything the packager puts in a game's ZIPs
engines: linux windows dos
	rm -rf $(CURDIR)/build/engines
	mkdir -p build/engines/linux build/engines/windows build/engines/dos
	for p in dbrun dbmaint dbsetup; do cp bin/linux/$$p build/engines/linux/ && cp bin/windows/$$p.exe build/engines/windows/ && cp bin/dos/$$p.exe build/engines/dos/ || exit 1; done
	cp COPYING NOTICE.TXT EXCEPT.TXT build/engines/
	echo $(VERSION) > build/engines/VERSION
	git archive --format=zip --prefix=doorbench-$(VERSION)/ -o build/engines/source.zip HEAD COPYING NOTICE.TXT EXCEPT.TXT README.md CHANGELOG.md LIMITATIONS.md Makefile 'docs/*.md' engine
