# Event Miner Makefile
# Build with nostr_core_lib dependency (uses system libraries)

CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2
INCLUDES = -Inostr_core_lib/nostr_core
LIBS = -lz -ldl -lpthread -lm -L/usr/local/lib -lsecp256k1 -lssl -lcrypto -L/usr/local/lib -lcurl
NOSTR_LIB = nostr_core_lib/libnostr_core_x64.a

# Default target
all: event_miner

# Main target - build event_miner
event_miner: event_miner.c $(NOSTR_LIB)
	@echo "Building event_miner..."
	$(CC) $(CFLAGS) $(INCLUDES) event_miner.c $(NOSTR_LIB) $(LIBS) -o event_miner
	@echo "✓ event_miner built successfully"

# Build nostr_core_lib dependency
$(NOSTR_LIB):
	@echo "Building nostr_core_lib dependency..."
	cd nostr_core_lib && ./build.sh x64
	@echo "✓ nostr_core_lib built successfully"

# Clean target
clean:
	@echo "Cleaning build artifacts..."
	rm -f event_miner
	cd nostr_core_lib && rm -f *.o libnostr_core_*.a

# Clean only event_miner (keep nostr_core_lib)
clean-miner:
	@echo "Cleaning event_miner..."
	rm -f event_miner

# Test target (basic compilation test)
test: event_miner
	@echo "Testing event_miner compilation..."
	./event_miner --help
	@echo "✓ Basic test passed"

# Install target (optional - install to /usr/local/bin)
install: event_miner
	@echo "Installing event_miner to /usr/local/bin..."
	sudo cp event_miner /usr/local/bin/
	@echo "✓ event_miner installed"

# Uninstall target
uninstall:
	@echo "Removing event_miner from /usr/local/bin..."
	sudo rm -f /usr/local/bin/event_miner
	@echo "✓ event_miner uninstalled"

# Force rebuild
rebuild: clean all

# Help
help:
	@echo "Event Miner Build System"
	@echo "========================"
	@echo ""
	@echo "Available targets:"
	@echo "  all          - Build event_miner (default)"
	@echo "  event_miner  - Build event_miner binary"
	@echo "  clean        - Clean all build artifacts"
	@echo "  clean-miner  - Clean only event_miner binary"
	@echo "  test         - Build and run basic test"
	@echo "  install      - Install to /usr/local/bin (requires sudo)"
	@echo "  uninstall    - Remove from /usr/local/bin (requires sudo)"
	@echo "  rebuild      - Clean and rebuild everything"
	@echo "  help         - Show this help"
	@echo ""
	@echo "Usage examples:"
	@echo "  make                    # Build event_miner"
	@echo "  make clean && make      # Clean rebuild"
	@echo "  make test               # Build and test"
	@echo ""
	@echo "The binary will be linked with system libraries (OpenSSL, curl, secp256k1)."

.PHONY: all clean clean-miner test install uninstall rebuild help
