CC := gcc
CFLAGS := -Wall -Wextra -std=c99 -Os -ffunction-sections -fdata-sections -Isrc -Iresources/nostr_core_lib -Iresources/nostr_core_lib/nostr_core -Iresources/nostr_core_lib/cjson -Iresources/tui_continuous
LDFLAGS := -Wl,--gc-sections resources/nostr_core_lib/libnostr_core_x64.a -lz -ldl -lpthread -lm -lssl -lcrypto -lcurl -lsecp256k1

SRC_DIR := src
BUILD_DIR := build
TEST_DIR := tests
CLIENT_DIR := client
EXAMPLES_DIR := examples

TARGET_DEV := $(BUILD_DIR)/nsigner

SOURCES := \
	$(SRC_DIR)/main.c \
	$(SRC_DIR)/secure_mem.c \
	$(SRC_DIR)/mnemonic.c \
	$(SRC_DIR)/role_table.c \
	$(SRC_DIR)/selector.c \
	$(SRC_DIR)/enforcement.c \
	$(SRC_DIR)/dispatcher.c \
	$(SRC_DIR)/policy.c \
	$(SRC_DIR)/server.c \
	$(SRC_DIR)/transport_frame.c \
	$(SRC_DIR)/key_store.c \
	$(SRC_DIR)/socket_name.c \
	$(SRC_DIR)/auth_envelope.c \
	resources/tui_continuous/tui_continuous.c

HEADERS :=


TEST_MNEMONIC_TARGET := $(BUILD_DIR)/test_mnemonic
TEST_ROLE_TARGET := $(BUILD_DIR)/test_role_table
TEST_SELECTOR_TARGET := $(BUILD_DIR)/test_selector
TEST_ENFORCEMENT_TARGET := $(BUILD_DIR)/test_enforcement
TEST_DISPATCHER_TARGET := $(BUILD_DIR)/test_dispatcher
TEST_POLICY_TARGET := $(BUILD_DIR)/test_policy
TEST_INTEGRATION_TARGET := $(BUILD_DIR)/test_integration
TEST_SOCKET_NAME_TARGET := $(BUILD_DIR)/test_socket_name
TEST_AUTH_ENVELOPE_TARGET := $(BUILD_DIR)/test_auth_envelope
TEST_QREXEC_AUTH_TARGET := $(BUILD_DIR)/test_qrexec_auth
TEST_MNEMONIC_INPUT_TARGET := $(BUILD_DIR)/test_mnemonic_input
EXAMPLE_GET_PUBLIC_KEY_TARGET := $(BUILD_DIR)/example_get_public_key_client
EXAMPLE_SIGN_EVENT_TARGET := $(BUILD_DIR)/example_sign_event_client

.PHONY: all lib dev static static-debug static-arm64 firmware-feather test test-integration test-mnemonic test-mnemonic-input test-role test-selector test-enforcement test-dispatcher test-policy test-socket-name test-auth-envelope test-qrexec-auth examples test-client clean

all: dev

lib:
	cd resources/nostr_core_lib && ./build.sh --nips=1,4,6,19,44

dev: lib $(TARGET_DEV)

$(TARGET_DEV): $(SOURCES)
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(SOURCES) -o $(TARGET_DEV) $(LDFLAGS)

static:
	chmod +x ./build_static.sh
	./build_static.sh

static-debug:
	chmod +x ./build_static.sh
	./build_static.sh --debug

static-arm64:
	chmod +x ./build_static.sh
	./build_static.sh --arch arm64

firmware-feather:
	cd firmware/feather_s3_tft && idf.py build

test: lib test-mnemonic test-mnemonic-input test-role test-selector test-enforcement test-dispatcher test-policy test-socket-name test-auth-envelope test-qrexec-auth test-client

test-integration: $(TEST_INTEGRATION_TARGET) $(TARGET_DEV)
	./$(TEST_INTEGRATION_TARGET)

test-mnemonic: $(TEST_MNEMONIC_TARGET)
	./$(TEST_MNEMONIC_TARGET)

test-mnemonic-input: $(TEST_MNEMONIC_INPUT_TARGET)
	./$(TEST_MNEMONIC_INPUT_TARGET)

test-role: $(TEST_ROLE_TARGET)
	./$(TEST_ROLE_TARGET)

test-selector: $(TEST_SELECTOR_TARGET)
	./$(TEST_SELECTOR_TARGET)

test-enforcement: $(TEST_ENFORCEMENT_TARGET)
	./$(TEST_ENFORCEMENT_TARGET)

test-dispatcher: $(TEST_DISPATCHER_TARGET)
	./$(TEST_DISPATCHER_TARGET)

test-policy: $(TEST_POLICY_TARGET)
	./$(TEST_POLICY_TARGET)

test-socket-name: $(TEST_SOCKET_NAME_TARGET)
	./$(TEST_SOCKET_NAME_TARGET)

test-auth-envelope: $(TEST_AUTH_ENVELOPE_TARGET)
	./$(TEST_AUTH_ENVELOPE_TARGET)

test-qrexec-auth: $(TEST_QREXEC_AUTH_TARGET) $(TARGET_DEV)
	./$(TEST_QREXEC_AUTH_TARGET)

test-client: examples

examples: $(EXAMPLE_GET_PUBLIC_KEY_TARGET) $(EXAMPLE_SIGN_EVENT_TARGET)

$(TEST_MNEMONIC_TARGET): $(TEST_DIR)/test_mnemonic.c $(SRC_DIR)/mnemonic.c $(SRC_DIR)/secure_mem.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_mnemonic.c $(SRC_DIR)/mnemonic.c $(SRC_DIR)/secure_mem.c -o $(TEST_MNEMONIC_TARGET) $(LDFLAGS)

$(TEST_MNEMONIC_INPUT_TARGET): $(TEST_DIR)/test_mnemonic_input.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) -I$(CLIENT_DIR) $(TEST_DIR)/test_mnemonic_input.c -o $(TEST_MNEMONIC_INPUT_TARGET) $(LDFLAGS)

$(TEST_ROLE_TARGET): $(TEST_DIR)/test_role_table.c $(SRC_DIR)/role_table.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_role_table.c $(SRC_DIR)/role_table.c -o $(TEST_ROLE_TARGET) $(LDFLAGS)

$(TEST_SELECTOR_TARGET): $(TEST_DIR)/test_selector.c $(SRC_DIR)/selector.c $(SRC_DIR)/role_table.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_selector.c $(SRC_DIR)/selector.c $(SRC_DIR)/role_table.c -o $(TEST_SELECTOR_TARGET) $(LDFLAGS)

$(TEST_ENFORCEMENT_TARGET): $(TEST_DIR)/test_enforcement.c $(SRC_DIR)/enforcement.c $(SRC_DIR)/role_table.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_enforcement.c $(SRC_DIR)/enforcement.c $(SRC_DIR)/role_table.c -o $(TEST_ENFORCEMENT_TARGET) $(LDFLAGS)

$(TEST_DISPATCHER_TARGET): $(TEST_DIR)/test_dispatcher.c $(SRC_DIR)/dispatcher.c $(SRC_DIR)/key_store.c $(SRC_DIR)/selector.c $(SRC_DIR)/enforcement.c $(SRC_DIR)/role_table.c $(SRC_DIR)/mnemonic.c $(SRC_DIR)/secure_mem.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_dispatcher.c $(SRC_DIR)/dispatcher.c $(SRC_DIR)/key_store.c $(SRC_DIR)/selector.c $(SRC_DIR)/enforcement.c $(SRC_DIR)/role_table.c $(SRC_DIR)/mnemonic.c $(SRC_DIR)/secure_mem.c -o $(TEST_DISPATCHER_TARGET) $(LDFLAGS)

$(TEST_POLICY_TARGET): $(TEST_DIR)/test_policy.c $(SRC_DIR)/policy.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_policy.c $(SRC_DIR)/policy.c -o $(TEST_POLICY_TARGET) $(LDFLAGS)

$(TEST_INTEGRATION_TARGET): $(TEST_DIR)/test_integration.c $(CLIENT_DIR)/nsigner_client.c $(SRC_DIR)/auth_envelope.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) -I$(CLIENT_DIR) $(TEST_DIR)/test_integration.c $(CLIENT_DIR)/nsigner_client.c $(SRC_DIR)/auth_envelope.c -o $(TEST_INTEGRATION_TARGET) $(LDFLAGS)

$(TEST_SOCKET_NAME_TARGET): $(TEST_DIR)/test_socket_name.c $(SRC_DIR)/socket_name.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_socket_name.c $(SRC_DIR)/socket_name.c -o $(TEST_SOCKET_NAME_TARGET) $(LDFLAGS)

$(TEST_AUTH_ENVELOPE_TARGET): $(TEST_DIR)/test_auth_envelope.c $(SRC_DIR)/auth_envelope.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_auth_envelope.c $(SRC_DIR)/auth_envelope.c -o $(TEST_AUTH_ENVELOPE_TARGET) $(LDFLAGS)

$(TEST_QREXEC_AUTH_TARGET): $(TEST_DIR)/test_qrexec_auth.c $(SRC_DIR)/auth_envelope.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $(TEST_DIR)/test_qrexec_auth.c $(SRC_DIR)/auth_envelope.c -o $(TEST_QREXEC_AUTH_TARGET) $(LDFLAGS)

$(EXAMPLE_GET_PUBLIC_KEY_TARGET): $(EXAMPLES_DIR)/get_public_key_client.c $(CLIENT_DIR)/nsigner_client.c $(SRC_DIR)/auth_envelope.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) -I$(CLIENT_DIR) $(EXAMPLES_DIR)/get_public_key_client.c $(CLIENT_DIR)/nsigner_client.c $(SRC_DIR)/auth_envelope.c -o $(EXAMPLE_GET_PUBLIC_KEY_TARGET) $(LDFLAGS)

$(EXAMPLE_SIGN_EVENT_TARGET): $(EXAMPLES_DIR)/sign_event_client.c $(CLIENT_DIR)/nsigner_client.c $(SRC_DIR)/auth_envelope.c
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) -I$(CLIENT_DIR) $(EXAMPLES_DIR)/sign_event_client.c $(CLIENT_DIR)/nsigner_client.c $(SRC_DIR)/auth_envelope.c -o $(EXAMPLE_SIGN_EVENT_TARGET) $(LDFLAGS)

clean:
	rm -rf $(BUILD_DIR)
