# plugin_webhook - SCORPIOX BOT open-source named webhook & REST trigger
#                  MCP connector
#
# Zero runtime dependencies on every artefact. The third-party C in the tree is
# vendored under vendor/: yyjson (MIT) and Mbed TLS 3.6.7 (Apache-2.0), whose
# sources sit in vendor/mbedtlslib with their public headers flattened into
# vendor/mbedtls and vendor/psa. That layout is what the SCORPIOX BOT release
# CLI (scorpiox-cli-botplugin build/publish) expects: it compiles every .c
# under src/ and vendor/ with exactly -I <repo>/src -I <repo>/vendor, so a
# vendored library must be reachable from those two include roots.
#
# On _WIN32 the HTTP and TLS legs are WinHTTP instead, so the static PE needs no
# extra DLLs and no crypto object files are reached.
#
# Targets:
#   make            native build        -> ./plugin_webhook
#   make musl       static musl x86_64  -> ./plugin_webhook.musl
#   make arm64      static arm64 musl   -> ./plugin_webhook.arm64
#   make win64      MinGW-w64 static    -> ./plugin_webhook.exe
#   make macos      Apple clang         -> ./plugin_webhook (run on macOS)
#   make asan       ASan + UBSan + _FORTIFY_SOURCE=2 -> ./plugin_webhook-asan
#   make fortify    -D_FORTIFY_SOURCE=2  -> ./plugin_webhook-fortify
#   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

CC            ?= cc
WARN          := -Wall -Wextra -Wno-overlength-strings
STD           := -std=c99
INCLUDES      := -I src -I vendor -I vendor/mbedtlslib
CFLAGS        ?= -O2 $(STD) $(WARN) $(INCLUDES)
MUSL_CC       ?= musl-gcc
MUSL_CFLAGS   := -O2 $(STD) $(WARN) $(INCLUDES) -static
ARM_CC        ?= aarch64-linux-gnu-gcc
ARM_CFLAGS    := -O2 $(STD) $(WARN) $(INCLUDES) -static
WIN_CC        ?= x86_64-w64-mingw32-gcc
WIN_CFLAGS    := -O2 $(STD) $(WARN) $(INCLUDES) -static
WIN_LIBS      := -lwinhttp -lws2_32 -lbcrypt -lcrypt32

SRC_C         := $(wildcard src/*.c)
VENDOR_C      := $(wildcard vendor/*.c) $(wildcard vendor/mbedtlslib/*.c)
UI_SRC        := src/ui.html
UI_HDR        := src/ui_html.h
BIN           := plugin_webhook
EXE           := plugin_webhook.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_C) $(VENDOR_C) $(UI_HDR)
	@echo "==> native build ($(CC))"
	$(CC) $(CFLAGS) $(SRC_C) $(VENDOR_C) -o $(BIN)

musl: $(SRC_C) $(VENDOR_C) $(UI_HDR)
	@echo "==> static musl build ($(MUSL_CC))"
	$(MUSL_CC) $(MUSL_CFLAGS) $(SRC_C) $(VENDOR_C) -o $(BIN).musl

arm64: $(SRC_C) $(VENDOR_C) $(UI_HDR)
	@echo "==> static arm64 build ($(ARM_CC))"
	$(ARM_CC) $(ARM_CFLAGS) $(SRC_C) $(VENDOR_C) -o $(BIN).arm64

win64: $(SRC_C) $(VENDOR_C) $(UI_HDR)
	@echo "==> MinGW-w64 build ($(WIN_CC))"
	$(WIN_CC) $(WIN_CFLAGS) $(SRC_C) $(VENDOR_C) -o $(EXE) $(WIN_LIBS)

macos: $(SRC_C) $(VENDOR_C) $(UI_HDR)
	@echo "==> Apple clang build"
	$(CC) $(CFLAGS) $(SRC_C) $(VENDOR_C) -o $(BIN)

asan: $(SRC_C) $(VENDOR_C) $(UI_HDR)
	$(CC) $(CFLAGS) -g -O1 -fsanitize=address,undefined \
	  -fno-omit-frame-pointer -D_FORTIFY_SOURCE=2 \
	  $(SRC_C) $(VENDOR_C) -o $(BIN)-asan

fortify: $(SRC_C) $(VENDOR_C) $(UI_HDR)
	$(CC) $(CFLAGS) -O2 -D_FORTIFY_SOURCE=2 $(SRC_C) $(VENDOR_C) -o $(BIN)-fortify

test: native
	@bash tests/run.sh

e2e: native
	@python3 tests/e2e.py

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

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