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

STB_PATH = $(HOME)/workdir/stb
LINUX_LIB_EXT          = so

# Version configuration (can be overridden)
DPP_VERSION       ?= $(shell find ../DPP/build/library -name "libdpp.so.*.*.*" -type f | head -1 | sed 's/.*libdpp\.so\.//')
DPP_PATH          = $(HOME)/workdir/DPP
DPP_BUILD_DIR      = $(DPP_PATH)/build/library
LIB_PATHS          = $(DPP_BUILD_DIR)
INC_DIRS = -Iinclude -I$(DPP_PATH)/include/ -I$(STB_PATH)
#endif

DPP_VERSION       := $(or $(DPP_VERSION),10.1.4)# Fallback version if auto-detection fails
DPP_LINUX_LIB_VERSION_FILE	= libdpp.$(LINUX_LIB_EXT).$(DPP_VERSION)
DPP_LIB_NAME         = libdpp.$(LINUX_LIB_EXT)

# 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='$$ORIGIN' # search for lib in the same place as the executable file

# 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)
	@if test -f token.txt; then \
		echo "Copying token.txt to $(@D)"; \
		cp token.txt $(@D); \
		if test -f $(DPP_BUILD_DIR)/$(DPP_LINUX_LIB_VERSION_FILE); then \
			cp $(DPP_BUILD_DIR)/$(DPP_LINUX_LIB_VERSION_FILE) $(BIN_DIR)/$(DPP_LINUX_LIB_VERSION_FILE); \
			ln -sf $(DPP_LINUX_LDPP_LINUX_LIB_VERSION_FILEIB_FILE) $(BIN_DIR)/$(DPP_LIB_NAME); \
		else \
			cp $(DPP_BUILD_DIR)/$(DPP_LIB_NAME) $(BIN_DIR)/$(DPP_LIB_NAME); \
		fi \
	else \
		echo "Error: token.txt not found! Please create it in the project root.  Copy the token when you create your discord bot and paste it in token.txt"; \
		exit 1; \
	fi
	
	

# 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