# SPDX-License-Identifier: Apache-2.0
# Copyright 2025 Wildcard Matcher Project
#
# Makefile for Wildcard Pattern Matcher
# Converted from Sudoku Solver project structure

# Compiler settings
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
DEBUG_FLAGS = -g -DDEBUG
TARGET = wildcard_matcher
SOURCES = WildcardMatcher.cc wildcard_main.cc
OBJECTS = $(SOURCES:.cc=.o)
HEADERS = WildcardMatcher.h

# Default target
all: $(TARGET)

# Build the main executable
$(TARGET): $(OBJECTS)
	$(CXX) $(CXXFLAGS) -o $@ $^
	@echo "✅ Wildcard matcher built successfully!"

# Compile source files
%.o: %.cc $(HEADERS)
	$(CXX) $(CXXFLAGS) -c $< -o $@

# Debug build
debug: CXXFLAGS += $(DEBUG_FLAGS)
debug: $(TARGET)
	@echo "🐛 Debug build completed"

# Clean build artifacts
clean:
	rm -f $(OBJECTS) $(TARGET)
	@echo "🧹 Cleaned build artifacts"

# Run the program
run: $(TARGET)
	@echo "🚀 Running Wildcard Pattern Matcher..."
	./$(TARGET)

# Run tests only
test: $(TARGET)
	@echo "🧪 Running tests..."
	echo "" | ./$(TARGET)

# Install (copy to /usr/local/bin)
install: $(TARGET)
	sudo cp $(TARGET) /usr/local/bin/
	@echo "📦 Installed to /usr/local/bin/"

# Uninstall
uninstall:
	sudo rm -f /usr/local/bin/$(TARGET)
	@echo "🗑️  Uninstalled from /usr/local/bin/"

# Check for memory leaks (requires valgrind)
memcheck: debug
	valgrind --leak-check=full --show-leak-kinds=all ./$(TARGET)

# Static analysis (requires cppcheck)
analyze:
	cppcheck --enable=all --std=c++17 $(SOURCES) $(HEADERS)

# Format code (requires clang-format)
format:
	clang-format -i $(SOURCES) $(HEADERS) wildcard_main.cc

# Show help
help:
	@echo "Available targets:"
	@echo "  all      - Build the wildcard matcher (default)"
	@echo "  debug    - Build with debug symbols"
	@echo "  clean    - Remove build artifacts"
	@echo "  run      - Build and run the program"
	@echo "  test     - Run automated tests"
	@echo "  install  - Install to /usr/local/bin"
	@echo "  uninstall- Remove from /usr/local/bin"
	@echo "  memcheck - Run with valgrind memory checker"
	@echo "  analyze  - Run static code analysis"
	@echo "  format   - Format code with clang-format"
	@echo "  help     - Show this help message"

# Phony targets
.PHONY: all debug clean run test install uninstall memcheck analyze format help