CC := gcc
SRC_DIR := src

UNAME_S := $(shell uname -s)

ifeq ($(UNAME_S), Linux)
    PLATFORM := linux
else ifeq ($(OS), Windows_NT)
    PLATFORM := windows
else
    PLATFORM := unknown
endif

BUILD_DIR := build/$(PLATFORM)
CFLAGS := -g -Isrc -Isrc/game -Wextra -MMD -MP -Wall -pedantic
LDFLAGS := -flto -Wl,--gc-sections

LIB_DIR := $(abspath libs/$(PLATFORM))
CFLAGS  += -I$(LIB_DIR)
LDFLAGS += -L$(LIB_DIR)

ifeq ($(PLATFORM), linux)
    LIBS += -lraylib -lm -lraygui
endif

SRC := $(shell find -L $(SRC_DIR) -type f -name '*.c')

OBJ := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRC))
DEP := $(OBJ:.o=.d)

ifeq ($(PLATFORM), windows)
    BIN := $(BUILD_DIR)/main.exe
endif
ifeq ($(PLATFORM), linux)
    BIN := $(BUILD_DIR)/main
endif


all: $(BIN)

run: $(BIN)
	@cd $(BUILD_DIR) && ./$(notdir $(BIN))

$(BIN): $(OBJ)
	@mkdir -p $(dir $@)
	@cp -r $(SRC_DIR)/game/assets $(BUILD_DIR)/game/assets
	@cp -r libs $(BUILD_DIR)
	@$(CC) $(LDFLAGS) $(OBJ) -o $@ $(LIBS)

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	@echo "Compiling $<..."
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	@rm -rf $(BUILD_DIR)

-include $(DEP)

.PHONY: all clean