# plugin_fetch - SCORPIOX BOT open-source URL fetch & HTML-to-Markdown MCP connector
#
# Zero runtime dependencies on every artefact. The only third-party C in the
# tree is vendored under vendor/: yyjson (MIT) and the mbedTLS 3.6.7 library
# (Apache-2.0) whose sources sit in vendor/mbedtlslib with their public
# headers flattened into vendor/mbedtls and vendor/psa. On _WIN32 the HTTP and
# TLS legs are WinHTTP instead, so the static PE needs no extra DLLs and no
# crypto objects are reached.
#
# The multi-arch dist publish lives in scorpiox-cli-botplugin (it owns the
# musl / aarch64 / MinGW / macOS toolchains, the dist upload and the plugingit
# mirror) - see release-dist.sh. That tool compiles every src/**/*.c and
# vendor/**/*.c with -I src and -I vendor, which is exactly what `make native`
# reproduces here.
#
# Targets:
#   make            native build        -> ./plugin_fetch
#   make musl       static musl x86_64  -> ./plugin_fetch.musl
#   make arm64      static arm64 musl   -> ./plugin_fetch.arm64
#   make win64      MinGW-w64 static    -> ./plugin_fetch.exe
#   make macos      Apple clang         -> ./plugin_fetch (run this on macOS)
#   make asan       ASan + UBSan + leaks -> ./plugin_fetch-asan
#   make fortify    -D_FORTIFY_SOURCE=2  -> ./plugin_fetch-fortify
#   make zlib       native build with gzip support (-DHAVE_ZLIB -lz)
#   make objs       the cached mbedTLS object set (fast incremental dev builds)
#   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)
LDFLAGS       ?=
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_fetch
EXE           := plugin_fetch.exe
OBJS          := /tmp/fetch-objs

.PHONY: all native musl arm64 win64 macos asan fortify zlib objs 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) $(UI_HDR) $(VENDOR_C)
	@echo "==> native build ($(CC))"
	$(CC) $(CFLAGS) -o $(BIN) $(SRC_C) $(VENDOR_C) $(LDFLAGS)

# Static musl build - the universal Linux artefact (Alpine, Debian, Arch, Omarchy)
musl: $(BIN).musl

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

# Static aarch64 build - Raspberry Pi 5 / ARM64 servers
arm64: $(BIN).arm64

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

# Native Win64 static PE - runs on any Windows 10/11 x64 node
win64: $(EXE)

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

# Apple clang build (run this on macOS; the CLI does it over ssh otherwise)
macos: $(SRC_C) $(UI_HDR) $(VENDOR_C)
	@echo "==> Apple clang build"
	$(CC) $(CFLAGS) -o $(BIN) $(SRC_C) $(VENDOR_C)

# Sanitizer build - tests/run.sh drives the whole surface through this binary
asan: $(BIN)-asan

$(BIN)-asan: $(SRC_C) $(UI_HDR) $(VENDOR_C)
	@echo "==> ASan + UBSan build"
	$(CC) -O1 -g $(STD) $(WARN) $(INCLUDES) -fsanitize=address,undefined \
	      -fno-omit-frame-pointer -o $(BIN)-asan $(SRC_C) $(VENDOR_C)

fortify: $(BIN)-fortify

$(BIN)-fortify: $(SRC_C) $(UI_HDR) $(VENDOR_C)
	@echo "==> _FORTIFY_SOURCE=2 build"
	$(CC) -O2 $(STD) $(WARN) $(INCLUDES) -D_FORTIFY_SOURCE=2 \
	      -o $(BIN)-fortify $(SRC_C) $(VENDOR_C)

# Optional gzip support. The published artefacts deliberately do NOT link it
# (a plugin must stay standalone); this target exists so the suite can prove
# the decoding path works when the library happens to be present.
zlib: $(SRC_C) $(UI_HDR) $(VENDOR_C)
	@echo "==> native build with gzip support"
	$(CC) $(CFLAGS) -DHAVE_ZLIB -o $(BIN)-zlib $(SRC_C) $(VENDOR_C) -lz

# Cached mbedTLS objects for the fast development loop (dev-build.sh uses them).
objs:
	@mkdir -p $(OBJS)
	@ls $(VENDOR_C) | while read -r f; do \
	  o=$(OBJS)/$$(basename "$${f%.c}").o; \
	  if [ ! -f "$$o" ] || [ "$$f" -nt "$$o" ]; then \
	    echo "$$f"; \
	  fi; \
	done | xargs -P $$(nproc) -I{} sh -c \
	  '$(CC) -O2 -w $(INCLUDES) -c "{}" -o $(OBJS)/$$(basename "{}" .c).o'
	@echo "==> cached $(words $(VENDOR_C)) vendor objects in $(OBJS)"

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 \
	      $(BIN)-zlib $(BIN).asan *.o
	rm -rf bin tests/__pycache__

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