
# Project configuration
EXECUTABLE_NAME   = discord-bot
PLATFORM          = $(shell uname)
BUILD_TYPE        ?= release# Default to release build if not specified

# Version configuration (can be overridden)
DPP_VERSION       ?= $(shell find ../DPP/build/library -name "libdpp.so.*.*.*" -type f | head -1 | sed 's/.*libdpp\.so\.//')
# Conditional assignment for DPP_PATH based on whether DPP_VERSION was found or is empty
# This logic assumes that if DPP_VERSION is found (not empty), it's a custom build (../DPP).
# If DPP_VERSION is empty (meaning libdpp.so.*.*.* was not found in the custom build path),
# we default to 'lib', assuming a system-wide or standard install location.
ifeq ($(strip $(DPP_VERSION)),) # Check if DPP_VERSION is empty (no version found)
    DPP_PATH          = ./lib # use .so file provided instead of the dpp source files
	# Define include directories for headers
	# -I<path> tells the compiler where to look for #include <file.h> or #include <dir/file.h>
	# We assume:
	# - Your own headers (like ImageProcessor.h) are in 'include/'
	# - STB headers are in 'include/stb/'
	# - D++ headers are in 'include/dpp/' (so dpp.h is found via include/dpp/dpp.h)
	INC_DIRS = -Iinclude -Idpp/ 
	LIB_PATHS          = $(DPP_PATH)
else
    # If DPP_VERSION has a value (a version was found), assume custom build path
    DPP_PATH          = ../DPP
	DPP_BUILD_DIR      = $(DPP_PATH)/build/library
	LIB_PATHS          = $(DPP_BUILD_DIR)
	INC_DIRS = -Iinclude -I../dpp/ 
endif

DPP_VERSION       := $(or $(DPP_VERSION),10.1.4)# Fallback version if auto-detection fails

# Common paths
OBJ_DIR           = obj/$(PLATFORM)/$(BUILD_TYPE)
BIN_DIR           = bin/$(PLATFORM)/$(BUILD_TYPE)
OUTPUT_FILE       = $(BIN_DIR)/$(EXECUTABLE_NAME)



# Build type specific flags
ifeq ($(BUILD_TYPE), debug)
    CPP_DEFINES = -D_DEBUG -DDL_USE_SHARED
    COMMON_FLAGS = $(ARCH_FLAGS) -fvisibility=hidden -g -O0 $(INCLUDE_PATHS)
else
    CPP_DEFINES = -DNDEBUG=1 -DDL_USE_SHARED
    COMMON_FLAGS = $(ARCH_FLAGS) -fvisibility=hidden -O3 $(INCLUDE_PATHS)
endif

# Language-specific flags
C_FLAGS            = $(COMMON_FLAGS) -std=c17
CXX_FLAGS          = $(COMMON_FLAGS) -std=c++17 -Wall -g $(INC_DIRS)

# Linker directive flags (L for library search path, l for library)
# Need to link against the D++ library and pthread (common for C++ applications with threading)
#LDFLAGS            = -L$(LIB_PATHS) -ldpp -lpthread -Wl,-rpath=../../../$(LIB_PATHS)
LDFLAGS            = -L$(LIB_PATHS) -ldpp -lpthread -Wl,-rpath=../../../$(LIB_PATHS)

# List of object files for your executable
# We've changed this back to use discord-bot.o as the source of the executable
OBJECTS = $(OBJ_DIR)/discord-bot.o

# --- Main Target ---
# 'all' is the default target that builds your executable
all: $(OUTPUT_FILE)
	@echo "DPP_PATH: "$(DPP_PATH)
	@echo "Build complete."	

# Rule to link the executable:
# It depends on the object files and uses CXX to link them with specified libraries.
$(OUTPUT_FILE): $(OBJECTS)
	@echo "Linking $(OUTPUT_FILE)..."
	@echo "LDFLAGS: $(LDFLAGS)"
	@mkdir -p $(@D) # Ensure the output binary directory exists (e.g., bin/Linux/release/)
	$(CXX) -o $@ $(OBJECTS) $(LDFLAGS)
	cp token.txt $(@D)

# Rule to compile the object file:
# This takes your .cpp source file and compiles it into an object file (.o).
$(OBJ_DIR)/$(EXECUTABLE_NAME).o: $(EXECUTABLE_NAME).cpp
	@echo "Compiling $< to $@"
	@mkdir -p $(@D) # Ensure the object directory exists (e.g., obj/Linux/release/)
	$(CXX) -c -o $@ $< $(CXX_FLAGS) $(CPP_DEFINES)	

	# --- Clean Target ---
# Removes all generated build files and directories
clean:
	@echo "Cleaning build directory..."
	$(RM) -r $(BIN_DIR)
	$(RM) -r $(OBJ_DIR)

# .PHONY specifies targets that are not actual files to prevent conflicts with file names
.PHONY: all clean