# plugin_websearch - SCORPIOX BOT open-source multi-provider web search 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 vendored TLS stack lives in vendor/bearssl (sources flattened, public
# headers beside them) because the SCORPIOX BOT release CLI compiles every .c
# under src/ + vendor/ with a fixed -I src -I vendor flag set. 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_websearch
#   make musl       static musl x86_64  -> ./plugin_websearch.musl
#   make arm64      static arm64 musl   -> ./plugin_websearch.arm64
#   make win64      MinGW-w64 static    -> ./plugin_websearch.exe
#   make macos      Apple clang         -> ./plugin_websearch (run this on macOS)
#   make asan       ASan + UBSan + _FORTIFY_SOURCE=2 -> ./plugin_websearch-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 edit to
# src/ws.h (a version bump, a provider id, a buffer size) left `make`
# reporting "Nothing to be done" and the *previous* binary in place - the
# build says success and the artefact is stale, which is the failure mode
# this project keeps paying for. vendor/ headers are included because the
# TLS and JSON sources are compiled straight from that tree.
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_websearch
EXE           := plugin_websearch.exe
WIN_LIBS      := -lws2_32
# macOS needs the Security framework only for the notarisation-free build; the
# TLS stack itself is self-contained, so no extra library is required there.

.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
