# plugin_time - SCORPIOX BOT open-source world-time MCP connector
#
# The only non-stdlib dependency is the vendored yyjson JSON parser under
# vendor/yyjson (MIT, upstream yyjson/yyjson); it is compiled into the binary,
# so every artefact stays a standalone executable with zero shared-library
# requirements on the node.
#
# The IANA timezone table lives in src/tzdata.h, generated from a compiled tz
# database by tools/gen_tzdata.py. Regenerate it only when you mean to move to
# a newer tzdata release, and re-run the suite: every epoch in the tests is
# absolute, so a tzdata change that moves a zone shows up as a failure rather
# than as a silently different answer.
#
# Targets:
#   make            native build  -> ./plugin_time
#   make musl       static musl x86_64 -> ./plugin_time.musl
#   make arm64      static arm64 musl (aarch64 cross) -> ./plugin_time.arm64
#   make win64      MinGW-w64 static -> ./plugin_time.exe
#   make macos      Apple clang -> ./plugin_time (run this on macOS)
#   make asan       ASan + UBSan + _FORTIFY_SOURCE=2 -> ./plugin_time.asan
#   make tzdata     regenerate src/tzdata.h from /usr/share/zoneinfo
#   make ui         regenerate src/ui_html.h from src/ui.html
#   make test       run tests/run.sh
#   make clean

CC      ?= cc
CFLAGS  ?= -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings
CPPFLAGS = -I src -I vendor
SRCS     = src/plugin_time.c src/timeutil.c src/tz.c vendor/yyjson.c
HDRS     = src/plugintime.h src/tz.h src/tzdata.h src/ui_html.h src/bot_theme_plugin.h

MUSL_CC  ?= musl-gcc
ARM64_CC ?= aarch64-linux-gnu-gcc
WIN64_CC ?= x86_64-w64-mingw32-gcc

all: plugin_time

plugin_time: $(SRCS) $(HDRS)
	$(CC) $(CFLAGS) $(CPPFLAGS) $(SRCS) -o $@

musl: plugin_time.musl
plugin_time.musl: $(SRCS) $(HDRS)
	$(MUSL_CC) $(CFLAGS) -static $(CPPFLAGS) $(SRCS) -o $@

arm64: plugin_time.arm64
plugin_time.arm64: $(SRCS) $(HDRS)
	$(ARM64_CC) $(CFLAGS) -static $(CPPFLAGS) $(SRCS) -o $@

win64: plugin_time.exe
plugin_time.exe: $(SRCS) $(HDRS)
	$(WIN64_CC) $(CFLAGS) -static -D_WIN32_WINNT=0x0601 $(CPPFLAGS) $(SRCS) -o $@

macos: $(SRCS) $(HDRS)
	clang $(CFLAGS) $(CPPFLAGS) $(SRCS) -o plugin_time

asan: plugin_time.asan
plugin_time.asan: $(SRCS) $(HDRS)
	$(CC) -O1 -g -std=c99 -Wall -Wextra -D_FORTIFY_SOURCE=2 \
	  -fsanitize=address,undefined -fno-omit-frame-pointer \
	  $(CPPFLAGS) $(SRCS) -o $@

tzdata:
	python3 tools/gen_tzdata.py

ui:
	python3 tools/gen_ui.py

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

tzdiff: plugin_time
	python3 tests/tz_diff.py

test: plugin_time
	sh tests/run.sh

clean:
	rm -f plugin_time plugin_time.musl plugin_time.arm64 plugin_time.exe plugin_time.asan

.PHONY: all musl arm64 win64 macos asan tzdata ui install test tzdiff clean
