CC = gcc
CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -O2 -D_GNU_SOURCE -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L -DMG_TLS=MG_TLS_BUILTIN -DNOSTR_ENABLE_NSIGNER_CLIENT=1

SRC_DIR = src
TARGET = didactyl

SRCS = \
	$(SRC_DIR)/main.c \
	$(SRC_DIR)/config.c \
	$(SRC_DIR)/context.c \
	$(SRC_DIR)/llm.c \
	$(SRC_DIR)/nostr_handler.c \
	$(SRC_DIR)/agent.c \
	$(SRC_DIR)/cashu_wallet.c \
	$(SRC_DIR)/tools/tools_common.c \
	$(SRC_DIR)/tools/tools_schema.c \
	$(SRC_DIR)/tools/tools_dispatch.c \
	$(SRC_DIR)/tools/tool_agent.c \
	$(SRC_DIR)/tools/tool_meta.c \
	$(SRC_DIR)/tools/tool_model.c \
	$(SRC_DIR)/tools/tool_nostr_query.c \
	$(SRC_DIR)/tools/tool_nostr_my_events.c \
	$(SRC_DIR)/tools/tool_nostr_identity.c \
	$(SRC_DIR)/tools/tool_nostr_social.c \
	$(SRC_DIR)/tools/tool_nostr_relay.c \
	$(SRC_DIR)/tools/tool_nostr_dm.c \
	$(SRC_DIR)/tools/tool_admin.c \
	$(SRC_DIR)/tools/tool_task.c \
	$(SRC_DIR)/tools/tool_nostr_list.c \
	$(SRC_DIR)/tools/tool_nostr_block.c \
	$(SRC_DIR)/tools/tool_local.c \
	$(SRC_DIR)/tools/tool_skill.c \
	$(SRC_DIR)/tools/tool_nostr_post.c \
	$(SRC_DIR)/tools/tool_memory.c \
	$(SRC_DIR)/tools/tool_config.c \
	$(SRC_DIR)/tools/tool_cashu_wallet.c \
	$(SRC_DIR)/tools/tool_blossom.c \
	$(SRC_DIR)/tools/tool_signer_crypto.c \
	$(SRC_DIR)/trigger_manager.c \
	$(SRC_DIR)/prompt_template.c \
	$(SRC_DIR)/http_api.c \
	$(SRC_DIR)/setup_wizard.c \
	$(SRC_DIR)/mongoose.c \
	$(SRC_DIR)/debug.c \
	$(SRC_DIR)/nostr_block_list.c \
	$(SRC_DIR)/json_to_markdown.c \
	$(SRC_DIR)/context_roles.c \
	$(SRC_DIR)/context_format.c \
	$(SRC_DIR)/signer_health.c

INCLUDES = \
	-I$(SRC_DIR) \
	-I$(SRC_DIR)/tools \
	-I./nostr_core_lib \
	-I./nostr_core_lib/cjson \
	-I./nostr_core_lib/nostr_core

NOSTR_LIB = $(firstword $(wildcard ./nostr_core_lib/libnostr_core_*.a))

LDFLAGS = -lcurl -lssl -lcrypto -lm -lpthread -ldl -lz -L/usr/local/lib -lsecp256k1

# Build directory
BUILD_DIR = build

all: $(TARGET)

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

# Build nostr_core_lib
$(NOSTR_LIB):
	@echo "Building nostr_core_lib with all NIPs..."
	cd ./nostr_core_lib && ./build.sh --nips=001,004,005,006,011,013,017,019,021,042,044,046,059,060,061

# Update main.h version information (requires main.h to exist)
src/main.h:
	@if [ ! -f src/main.h ]; then \
		echo "ERROR: src/main.h not found!"; \
		echo "Please ensure src/main.h exists with version metadata."; \
		exit 1; \
	fi; \
	if [ -d .git ]; then \
		echo "Updating main.h version information from git tags..."; \
		RAW_VERSION=$$(git describe --tags --always 2>/dev/null || echo "unknown"); \
		if echo "$$RAW_VERSION" | grep -q "^v[0-9]"; then \
			CLEAN_VERSION=$$(echo "$$RAW_VERSION" | sed 's/^v//' | cut -d- -f1); \
			VERSION="v$$CLEAN_VERSION"; \
			MAJOR=$$(echo "$$CLEAN_VERSION" | cut -d. -f1); \
			MINOR=$$(echo "$$CLEAN_VERSION" | cut -d. -f2); \
			PATCH=$$(echo "$$CLEAN_VERSION" | cut -d. -f3); \
		else \
			VERSION="v0.0.0"; \
			MAJOR=0; MINOR=0; PATCH=0; \
		fi; \
		echo "Updating version information in existing main.h..."; \
		sed -i "s/#define DIDACTYL_VERSION \".*\"/#define DIDACTYL_VERSION \"$$VERSION\"/g" src/main.h; \
		sed -i "s/#define DIDACTYL_VERSION_MAJOR [0-9]*/#define DIDACTYL_VERSION_MAJOR $$MAJOR/g" src/main.h; \
		sed -i "s/#define DIDACTYL_VERSION_MINOR [0-9]*/#define DIDACTYL_VERSION_MINOR $$MINOR/g" src/main.h; \
		sed -i "s/#define DIDACTYL_VERSION_PATCH [0-9]*/#define DIDACTYL_VERSION_PATCH $$PATCH/g" src/main.h; \
		echo "Updated main.h version to: $$VERSION"; \
	else \
		echo "Git not available, preserving existing main.h version information"; \
	fi

# Force update version
force-version:
	@echo "Force updating main.h version information..."
	@$(MAKE) src/main.h

$(TARGET): src/main.h $(SRCS) $(NOSTR_LIB)
	@if [ -z "$(NOSTR_LIB)" ]; then echo "nostr_core_lib static library not found; run 'make deps'"; exit 1; fi
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(SRCS) $(NOSTR_LIB) $(LDFLAGS)

deps:
	cd ./nostr_core_lib && ./build.sh --nips=001,004,005,006,011,013,017,019,021,042,044,046,059,060,061

clean:
	rm -f $(TARGET)
	rm -rf $(BUILD_DIR)

test_pool: $(NOSTR_LIB) test_pool.c
	@if [ -z "$(NOSTR_LIB)" ]; then echo "nostr_core_lib static library not found; run 'make deps'"; exit 1; fi
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ test_pool.c $(NOSTR_LIB) $(LDFLAGS)

.PHONY: all deps clean force-version test_pool
