# plugin_ntfy - SCORPIOX BOT open-source ntfy push & pub/sub channel connector
#
# Targets:
#   make            native build  -> ./plugin_ntfy
#   make musl       static musl x86_64
#   make arm64      static arm64 (aarch64 cross)
#   make win64      MinGW-w64 static PE
#   make macos      Apple clang (run on macOS)
#   make ui         regenerate src/ui_html.h from src/ui.html
#   make cabundle   regenerate src/ca_bundle_pem.h
#   make asan       ASan + UBSan build under tests/asan/
#   make fortify    -D_FORTIFY_SOURCE=2 build under tests/fortify/
#   make test       build + run tests/run.sh
#   make e2e        build + run tests/e2e.py (Playwright)
#   make clean
#
# Sources: the plugin itself (src/plugin_ntfy.c + src/sx_http.c +
# src/sx_connectors.c) plus the two vendored libraries:
#   vendor/yyjson.c            (MIT, ibireme)
#   vendor/mbedtls/*.c         (Apache-2.0, Mbed TLS 3.6.6, client config
#                               pinned in src/sx_ntfy_mbedtls_config.h)
# The vendored mbedTLS public headers live in src/mbedtls + src/psa because
# the SCORPIOX BOT release CLI compiles every .c under src/ + vendor/ with a
# fixed -I src -I vendor flag set, and quoted "mbedtls/*.h" includes resolve
# through -I src.

CC            ?= cc
MBEDTLS_CFG   := -DMBEDTLS_CONFIG_FILE=\<sx_ntfy_mbedtls_config.h\>
BASE_INC      := -I src -I vendor
CFLAGS        ?= -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings $(BASE_INC) $(MBEDTLS_CFG)
MUSL_CC       ?= musl-gcc
MUSL_CFLAGS   := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings $(BASE_INC) $(MBEDTLS_CFG) -static
ARM_CC        ?= aarch64-linux-gnu-gcc
ARM_CFLAGS    := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings $(BASE_INC) $(MBEDTLS_CFG) -static
WIN_CC        ?= x86_64-w64-mingw32-gcc
WIN_CFLAGS    := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings $(BASE_INC) $(MBEDTLS_CFG) -static

PLUGINSRCS := src/plugin_ntfy.c src/sx_http.c src/sx_connectors.c
VENDORSRCS := vendor/yyjson.c $(wildcard vendor/mbedtls/*.c)
ALLSRCS    := $(PLUGINSRCS) $(VENDORSRCS)
UI_SRC     := src/ui.html
UI_HDR     := src/ui_html.h
CA_HDR     := src/ca_bundle_pem.h
BIN        := plugin_ntfy
EXE        := plugin_ntfy.exe
LIBS       := -lpthread

.PHONY: all native musl arm64 win64 macos ui cabundle asan fortify 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)

cabundle:
	python3 tools/gen_ca_bundle.py

$(CA_HDR):
	python3 tools/gen_ca_bundle.py

native: $(BIN)

$(BIN): $(ALLSRCS) $(UI_HDR) $(CA_HDR)
	@echo "==> native build ($(CC))"
	$(CC) $(CFLAGS) -o $(BIN) $(ALLSRCS) $(LIBS)

musl: $(ALLSRCS) $(UI_HDR) $(CA_HDR)
	@echo "==> static musl build ($(MUSL_CC))"
	$(MUSL_CC) $(MUSL_CFLAGS) -o $(BIN).musl $(ALLSRCS) $(LIBS)

arm64: $(ALLSRCS) $(UI_HDR) $(CA_HDR)
	@echo "==> static arm64 build ($(ARM_CC))"
	$(ARM_CC) $(ARM_CFLAGS) -o $(BIN).arm64 $(ALLSRCS) $(LIBS)

win64: $(ALLSRCS) $(UI_HDR) $(CA_HDR)
	@echo "==> MinGW-w64 build ($(WIN_CC))"
	$(WIN_CC) $(WIN_CFLAGS) -o $(EXE) $(ALLSRCS) -lws2_32 -lbcrypt

macos: $(ALLSRCS) $(UI_HDR) $(CA_HDR)
	@echo "==> Apple clang build"
	$(CC) $(CFLAGS) -o $(BIN) $(ALLSRCS) $(LIBS)

asan: $(UI_HDR) $(CA_HDR)
	@mkdir -p tests/asan
	$(CC) $(CFLAGS) -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer \
	  -o tests/asan/plugin_ntfy_asan $(ALLSRCS) $(LIBS)

fortify: $(UI_HDR) $(CA_HDR)
	@mkdir -p tests/fortify
	$(CC) $(CFLAGS) -O2 -D_FORTIFY_SOURCE=2 -o tests/fortify/plugin_ntfy_fortify $(ALLSRCS) $(LIBS)

test: native
	@bash tests/run.sh

e2e: native
	@python3 tests/e2e.py

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

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