# plugin_filesystem - SCORPIOX BOT open-source allowlisted filesystem MCP connector
#
# The only non-stdlib dependency is the vendored yyjson JSON parser under
# vendor/yyjson (MIT, upstream yyjson/yyjson); it is compiled into the binary,
# so every artefact stays a standalone executable with zero shared-library
# requirements on the node.
#
# Targets:
#   make            native build  -> ./plugin_filesystem
#   make musl       static musl x86_64 -> ./plugin_filesystem.musl
#   make arm64      static arm64 musl (aarch64 cross) -> ./plugin_filesystem.arm64
#   make win64      MinGW-w64 static -> ./plugin_filesystem.exe
#   make macos      Apple clang -> ./plugin_filesystem (run this on macOS)
#   make asan       ASan + UBSan build (tests/run.sh drives the whole tool set through it)
#   make fortify    -D_FORTIFY_SOURCE=2 build
#   make ui         regenerate src/ui_html.h from src/ui.html
#   make test       build + run tests/run.sh
#   make e2e        build + run tests/e2e.py (Playwright)
#   make clean
#
# The multi-arch dist publish lives in scorpiox-cli-botplugin (it owns the
# toolchains, the dist upload and the plugingit mirror) - see release-dist.sh.

CC            ?= cc
CFLAGS        ?= -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings -I src -I vendor/yyjson
MUSL_CC       ?= musl-gcc
MUSL_CFLAGS   := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -I vendor/yyjson -static
ARM_CC        ?= aarch64-linux-gnu-gcc
ARM_CFLAGS    := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -I vendor/yyjson -static
WIN_CC        ?= x86_64-w64-mingw32-gcc
WIN_CFLAGS    := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -I vendor/yyjson -static
SRC           := src/plugin_filesystem.c
VENDOR        := vendor/yyjson/yyjson.c
UI_SRC        := src/ui.html
UI_HDR        := src/ui_html.h
BIN           := plugin_filesystem
EXE           := plugin_filesystem.exe

.PHONY: all native musl arm64 win64 macos asan fortify ui test e2e clean help

all: native

$(UI_HDR): $(UI_SRC) tools/gen_ui.py
	@echo "==> regenerating $(UI_HDR)"
	python3 tools/gen_ui.py

ui: $(UI_HDR)

native: $(BIN)

$(BIN): $(SRC) $(UI_HDR) $(VENDOR)
	@echo "==> native build ($(CC))"
	$(CC) $(CFLAGS) -o $(BIN) $(SRC) $(VENDOR)

# Static musl build - the universal Linux artifact (Alpine, Debian, Arch, Omarchy)
musl: $(SRC) $(UI_HDR) $(VENDOR)
	@echo "==> static musl build ($(MUSL_CC))"
	$(MUSL_CC) $(MUSL_CFLAGS) -o $(BIN).musl $(SRC) $(VENDOR)

# Static aarch64 build - Raspberry Pi 5 / ARM64 servers
arm64: $(SRC) $(UI_HDR) $(VENDOR)
	@echo "==> static arm64 build ($(ARM_CC))"
	$(ARM_CC) $(ARM_CFLAGS) -o $(BIN).arm64 $(SRC) $(VENDOR)

# Native Win64 static PE - runs on any Windows 10/11 x64 node
win64: $(SRC) $(UI_HDR) $(VENDOR)
	@echo "==> MinGW-w64 build ($(WIN_CC))"
	$(WIN_CC) $(WIN_CFLAGS) -o $(EXE) $(SRC) $(VENDOR)

# Apple clang build (run this on macOS)
macos: $(SRC) $(UI_HDR) $(VENDOR)
	@echo "==> Apple clang build"
	$(CC) $(CFLAGS) -o $(BIN) $(SRC) $(VENDOR)

# Sanitizer build - tests/run.sh exercises every tool through this binary
asan: $(SRC) $(UI_HDR) $(VENDOR)
	@echo "==> ASan + UBSan build"
	$(CC) -O1 -g -std=c99 -fsanitize=address,undefined -fno-omit-frame-pointer \
	      -I src -I vendor/yyjson -o $(BIN)-asan $(SRC) $(VENDOR)

fortify: $(SRC) $(UI_HDR) $(VENDOR)
	@echo "==> _FORTIFY_SOURCE=2 build"
	$(CC) -O2 -D_FORTIFY_SOURCE=2 -std=c99 -Wall -Wextra -Wno-overlength-strings \
	      -I src -I vendor/yyjson -o $(BIN)-fortify $(SRC) $(VENDOR)

test: native
	@sh tests/run.sh

e2e: native
	@python3 tests/e2e.py

clean:
	rm -f $(BIN) $(EXE) $(BIN).musl $(BIN).arm64 $(BIN)-asan $(BIN)-fortify
	rm -rf bin

help:
	@echo "make | make musl | make arm64 | make win64 | make macos | make asan | make fortify | make ui | make test | make e2e | make clean"
