# ==================================================================================
# ISA Project Makefile - ICMP Covert Channel File Transfer System
# Author: Roman Necas (xnecasr00)
# Date: 13 October 2025
# ==================================================================================

# Compiler and flags
CC = gcc
SRC_DIR = src
INC_DIR = include
BUILD_DIR = build
CFLAGS = -Wall -Wextra -O2 -std=c99 -D_POSIX_C_SOURCE=200809L -I$(INC_DIR)
LIBS = -lssl -lcrypto -lpthread -lm
TARGET = secret

# Main application sources
SOURCES = $(SRC_DIR)/secret.c $(SRC_DIR)/argument_parser.c $(SRC_DIR)/crypto_engine.c \
          $(SRC_DIR)/network_interface.c $(SRC_DIR)/file_manager.c $(SRC_DIR)/protocol.c \
          $(SRC_DIR)/client_engine.c $(SRC_DIR)/server_engine.c $(SRC_DIR)/utils.c \
          $(SRC_DIR)/state_machine.c $(SRC_DIR)/error.c
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)

# Headers
HEADERS = $(INC_DIR)/secret.h $(INC_DIR)/common.h $(INC_DIR)/error.h $(INC_DIR)/crypto.h \
          $(INC_DIR)/network.h $(INC_DIR)/protocol.h $(INC_DIR)/file_io.h $(INC_DIR)/state_machine.h

# Documentation files
MANUAL = secret.1
REPORT = manual.pdf

.PHONY: all clean pack help

all: $(BUILD_DIR) $(TARGET)

# Create build directory if it doesn't exist
$(BUILD_DIR):
	@mkdir -p $(BUILD_DIR)

# Build the main executable
$(TARGET): $(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $^ $(LIBS)

# Compile object files from C sources
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(HEADERS)
	$(CC) $(CFLAGS) -c $< -o $@

# Clean build artifacts and temporary files
clean:
	rm -rf $(BUILD_DIR) $(TARGET) *.log core
	rm -f *_1.txt *_1.bin 2>/dev/null || true

# Create distribution archive for submission
# Includes all required source files, headers, documentation, Makefile
pack: $(TARGET)
	tar -cf xnecasr00.tar $(SOURCES) $(HEADERS) $(MANUAL) $(REPORT) Makefile

# Display help information
help:
	@echo "==========================================================================="
	@echo "ISA Project - ICMP Covert Channel File Transfer System"
	@echo "Author: Roman Necas (xnecasr00)"
	@echo "==========================================================================="
	@echo ""
	@echo "Build Targets:"
	@echo "  all (default)  - Build the executable"
	@echo "  clean          - Remove all built files and temporary files"
	@echo "  pack           - Create distribution archive (xnecasr00.tar)"
	@echo "  help           - Show this help message"
	@echo ""
	@echo "Examples:"
	@echo "  make                 # Build project"
	@echo "  make clean && make   # Clean build from scratch"
	@echo "  make pack            # Create submission tar archive"
	@echo ""
	@echo "Usage (requires root privileges):"
	@echo "  Server: sudo ./secret -l"
	@echo "  Client: sudo ./secret -r <file> -s <ip|hostname>"
	@echo "==========================================================================="

# ==================================================================================
# Explicit dependency declarations
# Each module's dependencies are listed to ensure proper rebuild on header changes
# ==================================================================================
$(BUILD_DIR)/secret.o: $(SRC_DIR)/secret.c $(HEADERS)
$(BUILD_DIR)/argument_parser.o: $(SRC_DIR)/argument_parser.c $(INC_DIR)/common.h $(INC_DIR)/error.h
$(BUILD_DIR)/crypto_engine.o: $(SRC_DIR)/crypto_engine.c $(INC_DIR)/crypto.h $(INC_DIR)/error.h $(INC_DIR)/common.h
$(BUILD_DIR)/network_interface.o: $(SRC_DIR)/network_interface.c $(INC_DIR)/network.h $(INC_DIR)/error.h $(INC_DIR)/common.h
$(BUILD_DIR)/file_manager.o: $(SRC_DIR)/file_manager.c $(INC_DIR)/file_io.h $(INC_DIR)/error.h $(INC_DIR)/common.h
$(BUILD_DIR)/protocol.o: $(SRC_DIR)/protocol.c $(INC_DIR)/protocol.h $(INC_DIR)/error.h $(INC_DIR)/common.h $(INC_DIR)/crypto.h
$(BUILD_DIR)/client_engine.o: $(SRC_DIR)/client_engine.c $(HEADERS)
$(BUILD_DIR)/server_engine.o: $(SRC_DIR)/server_engine.c $(HEADERS)
$(BUILD_DIR)/utils.o: $(SRC_DIR)/utils.c $(INC_DIR)/common.h $(INC_DIR)/error.h
$(BUILD_DIR)/state_machine.o: $(SRC_DIR)/state_machine.c $(INC_DIR)/state_machine.h $(INC_DIR)/error.h $(INC_DIR)/common.h
$(BUILD_DIR)/error.o: $(SRC_DIR)/error.c $(INC_DIR)/error.h $(INC_DIR)/common.h
