# plugin_github - SCORPIOX BOT open-source GitHub MCP connector
#
# The only third-party C in the tree is vendored under vendor/:
#   vendor/yyjson.c   (MIT, ibireme)                 JSON parse/write
#   vendor/bearssl/   (MIT, Thomas Pornin)           TLS 1.2+ client
#
# The plugin runtime is the SCORPIOX BOT Connector SDK, vendored into src/:
#   src/sx_connector.{c,h}   the plugin/connector contract (CGI, auth, MCP,
#                            connector state, theming) and src/sx_http_tls.{c,h}
#                            (HTTP/1.1 + TLS over the BearSSL tree above).
# The vendored TLS stack lives flat in vendor/bearssl (sources and public
# headers side by side) because the SCORPIOX BOT release CLI compiles every .c
# under src/ + vendor/ with a fixed -I src -I vendor flag set - and a
# vendor/<lib>/ subdirectory used to fail every dist target silently. Nothing
# here needs libcurl, OpenSSL or a system CA file at runtime: the roots are
# compiled in (src/ca_roots_pem.h).
#
# Targets:
#   make            native build        -> ./plugin_github
#   make musl       static musl x86_64  -> ./plugin_github.musl
#   make arm64      static arm64 musl   -> ./plugin_github.arm64
#   make win64      MinGW-w64 static    -> ./plugin_github.exe
#   make macos      Apple clang         -> ./plugin_github (run this on macOS)
#   make asan       ASan + UBSan + _FORTIFY_SOURCE=2 -> ./plugin_github-asan
#   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 -Wno-unused-function
STD           := -std=c99
INCLUDES      := -I src -I vendor
CFLAGS        ?= -O2 $(STD) $(WARN) $(INCLUDES)
MUSL_CC       ?= musl-gcc
ARM_CC        ?= aarch64-linux-gnu-gcc
WIN_CC        ?= x86_64-w64-mingw32-gcc

SRC_C         := $(wildcard src/*.c)
# Every header is a prerequisite too: without them a one-line header edit
# would leave the previous binary in place while `make` says success.
HDR           := $(wildcard src/*.h) $(wildcard vendor/*.h) $(wildcard vendor/bearssl/*.h)
VENDOR_C      := $(wildcard vendor/*.c) $(wildcard vendor/bearssl/*.c)
UI_SRC        := src/ui.html
UI_HDR        := src/ui_html.h
BIN           := plugin_github
EXE           := plugin_github.exe
WIN_LIBS      := -lws2_32
# The vendored SDK's transport is self-contained on every platform (BearSSL
# does the TLS on Windows too), so -lws2_32 is the only import library.

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

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) $(HDR) $(UI_HDR) $(VENDOR_C)
	@echo "==> native build ($(CC))"
	$(CC) $(CFLAGS) -o $(BIN) $(SRC_C) $(VENDOR_C)

musl: $(BIN).musl
$(BIN).musl: $(SRC_C) $(HDR) $(UI_HDR) $(VENDOR_C)
	@echo "==> static musl build ($(MUSL_CC))"
	$(MUSL_CC) $(CFLAGS) -static -fno-strict-aliasing -o $@ $(SRC_C) $(VENDOR_C)

arm64: $(BIN).arm64
$(BIN).arm64: $(SRC_C) $(HDR) $(UI_HDR) $(VENDOR_C)
	@echo "==> static arm64 build ($(ARM_CC))"
	$(ARM_CC) $(CFLAGS) -static -fno-strict-aliasing -o $@ $(SRC_C) $(VENDOR_C)

win64: $(EXE)
$(EXE): $(SRC_C) $(HDR) $(UI_HDR) $(VENDOR_C)
	@echo "==> MinGW-w64 build ($(WIN_CC))"
	$(WIN_CC) $(CFLAGS) -static -D_WIN32_WINNT=0x0601 -fno-strict-aliasing \
	  -o $@ $(SRC_C) $(VENDOR_C) $(WIN_LIBS)

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

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

install: $(BIN)
	PDIR="/workspace/.scorpiox/plugins"; [ -d /workspace ] || PDIR="$$HOME/.scorpiox/plugins"; \
	mkdir -p "$$PDIR" && cp $(BIN) "$$PDIR/$(BIN)" && chmod 0755 "$$PDIR/$(BIN)" && \
	echo "installed $$PDIR/$(BIN) (no restart needed: the node rescans plugins per request)"

test: $(BIN)
	sh tests/run.sh

e2e: $(BIN)
	python3 tests/e2e.py

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

help:
	@sed -n '1,30p' Makefile
