# C-Relay Makefile

CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -g -O2
INCLUDES = -I. -Inostr_core_lib -Inostr_core_lib/nostr_core -Inostr_core_lib/cjson -Inostr_core_lib/nostr_websocket
LIBS = -lsqlite3 -lwebsockets -lz -ldl -lpthread -lm -L/usr/local/lib -lsecp256k1 -lssl -lcrypto -L/usr/local/lib -lcurl

# Build directory
BUILD_DIR = build

# Source files
MAIN_SRC = src/main.c src/config.c
NOSTR_CORE_LIB = nostr_core_lib/libnostr_core_x64.a

# Architecture detection
ARCH = $(shell uname -m)
ifeq ($(ARCH),x86_64)
    TARGET = $(BUILD_DIR)/c_relay_x86
else ifeq ($(ARCH),aarch64)
    TARGET = $(BUILD_DIR)/c_relay_arm64
else ifeq ($(ARCH),arm64)
    TARGET = $(BUILD_DIR)/c_relay_arm64
else
    TARGET = $(BUILD_DIR)/c_relay_$(ARCH)
endif

# Default target
all: $(TARGET)

# Create build directory
$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

# Check if nostr_core_lib is built
$(NOSTR_CORE_LIB):
	@echo "Building nostr_core_lib..."
	cd nostr_core_lib && ./build.sh

# Build the relay
$(TARGET): $(BUILD_DIR) $(MAIN_SRC) $(NOSTR_CORE_LIB)
	@echo "Compiling C-Relay for architecture: $(ARCH)"
	$(CC) $(CFLAGS) $(INCLUDES) $(MAIN_SRC) -o $(TARGET) $(NOSTR_CORE_LIB) $(LIBS)
	@echo "Build complete: $(TARGET)"

# Build for specific architectures
x86: $(BUILD_DIR) $(MAIN_SRC) $(NOSTR_CORE_LIB)
	@echo "Building C-Relay for x86_64..."
	$(CC) $(CFLAGS) $(INCLUDES) $(MAIN_SRC) -o $(BUILD_DIR)/c_relay_x86 $(NOSTR_CORE_LIB) $(LIBS)
	@echo "Build complete: $(BUILD_DIR)/c_relay_x86"

arm64: $(BUILD_DIR) $(MAIN_SRC) $(NOSTR_CORE_LIB)
	@echo "Cross-compiling C-Relay for ARM64..."
	@if ! command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then \
		echo "ERROR: ARM64 cross-compiler not found."; \
		echo "Install with: make install-cross-tools"; \
		echo "Or install manually: sudo apt install gcc-aarch64-linux-gnu"; \
		exit 1; \
	fi
	@echo "Checking for ARM64 development libraries..."
	@if ! dpkg -l | grep -q "libssl-dev:arm64\|libsqlite3-dev:arm64"; then \
		echo "ERROR: ARM64 libraries not found. Cross-compilation requires ARM64 versions of:"; \
		echo "  - libssl-dev:arm64"; \
		echo "  - libsqlite3-dev:arm64"; \
		echo "  - libwebsockets-dev:arm64"; \
		echo "  - libsecp256k1-dev:arm64"; \
		echo "  - zlib1g-dev:arm64"; \
		echo "  - libcurl4-openssl-dev:arm64"; \
		echo ""; \
		echo "Install ARM64 libraries with: make install-arm64-deps"; \
		echo "Or use Docker for cross-platform builds."; \
		exit 1; \
	fi
	@echo "Using aarch64-linux-gnu-gcc with ARM64 libraries..."
	PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
	aarch64-linux-gnu-gcc $(CFLAGS) $(INCLUDES) $(MAIN_SRC) -o $(BUILD_DIR)/c_relay_arm64 $(NOSTR_CORE_LIB) \
		-L/usr/lib/aarch64-linux-gnu $(LIBS)
	@echo "Build complete: $(BUILD_DIR)/c_relay_arm64"

# Install ARM64 cross-compilation dependencies
install-arm64-deps:
	@echo "Installing ARM64 cross-compilation dependencies..."
	@echo "This requires adding ARM64 architecture and installing cross-libraries..."
	sudo dpkg --add-architecture arm64
	sudo apt update
	sudo apt install -y \
		gcc-aarch64-linux-gnu \
		libc6-dev-arm64-cross \
		libssl-dev:arm64 \
		libsqlite3-dev:arm64 \
		zlib1g-dev:arm64 \
		libcurl4-openssl-dev:arm64
	@echo "Note: libwebsockets-dev:arm64 and libsecp256k1-dev:arm64 may need manual building"

# Install cross-compilation tools
install-cross-tools:
	@echo "Installing cross-compilation tools..."
	sudo apt update
	sudo apt install -y gcc-aarch64-linux-gnu libc6-dev-arm64-cross

# Check what architectures we can actually build
check-toolchain:
	@echo "Checking available toolchains:"
	@echo "Native compiler: $(shell $(CC) --version | head -1)"
	@if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then \
		echo "ARM64 cross-compiler: $(shell aarch64-linux-gnu-gcc --version | head -1)"; \
	else \
		echo "ARM64 cross-compiler: NOT INSTALLED (install with 'make install-cross-tools')"; \
	fi

# Run tests
test: $(TARGET)
	@echo "Running tests..."
	./tests/1_nip_test.sh

# Initialize database
init-db:
	@echo "Initializing database..."
	./db/init.sh --force

# Clean build artifacts
clean:
	rm -rf $(BUILD_DIR)
	@echo "Clean complete"

# Clean everything including nostr_core_lib
clean-all: clean
	cd nostr_core_lib && make clean 2>/dev/null || true

# Install dependencies (Ubuntu/Debian)
install-deps:
	@echo "Installing dependencies..."
	sudo apt update
	sudo apt install -y build-essential libsqlite3-dev libssl-dev libcurl4-openssl-dev libsecp256k1-dev zlib1g-dev jq curl

# Help
help:
	@echo "C-Relay Build System"
	@echo ""
	@echo "Targets:"
	@echo "  all              Build the relay for current architecture (default)"
	@echo "  x86              Build specifically for x86_64"
	@echo "  arm64            Build for ARM64 (requires cross-compilation setup)"
	@echo "  test             Build and run tests"
	@echo "  init-db          Initialize the database"
	@echo "  clean            Clean build artifacts"
	@echo "  clean-all        Clean everything including dependencies"
	@echo "  install-deps     Install system dependencies"
	@echo "  install-cross-tools Install basic ARM64 cross-compiler"
	@echo "  install-arm64-deps Install ARM64 cross-compilation libraries"
	@echo "  check-toolchain  Check available compilers"
	@echo "  help             Show this help"
	@echo ""
	@echo "Usage:"
	@echo "  make                     # Build the relay for current arch"
	@echo "  make x86                 # Build for x86_64"
	@echo "  make arm64               # Build for ARM64 (fails if cross-compilation not set up)"
	@echo "  make install-arm64-deps  # Install full ARM64 cross-compilation setup"
	@echo "  make check-toolchain     # Check what compilers are available"
	@echo "  make test                # Run tests"
	@echo "  make init-db             # Set up database"

.PHONY: all x86 arm64 test init-db clean clean-all install-deps install-cross-tools install-arm64-deps check-toolchain help