# ============================================================================
# ViBe3 Physics - Resource Packer Tool Makefile
# ============================================================================

# Directorios
DIR_ROOT       := $(dir $(abspath $(dir $(MAKEFILE_LIST))))
DIR_SOURCES    := $(DIR_ROOT)source/
DIR_TOOLS      := $(DIR_ROOT)tools/
DIR_DATA       := $(DIR_ROOT)data/

# Archivos fuente
PACK_SOURCES   := $(DIR_TOOLS)pack_resources.cpp $(DIR_SOURCES)resource_pack.cpp
PACK_INCLUDES  := -I$(DIR_ROOT)

# Compilador y flags
CXX            := g++
CXXFLAGS       := -std=c++17 -Wall -Os -ffunction-sections -fdata-sections

# Variables específicas por sistema operativo
ifeq ($(OS),Windows_NT)
    # Windows
    PACK_TOOL      := pack_resources.exe
    RESOURCE_PACK  := ../resources.pack
    TEST_PACK      := test_resources.pack
    LDFLAGS        := -Wl,--gc-sections -static-libstdc++ -static-libgcc
    RMFILE         := del /Q
    MKDIR          := mkdir
    FixPath        = $(subst /,\,$1)
else
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        # Linux
        PACK_TOOL      := pack_resources
        RESOURCE_PACK  := ../resources.pack
        TEST_PACK      := test_resources.pack
        LDFLAGS        := -Wl,--gc-sections
        RMFILE         := rm -f
        MKDIR          := mkdir -p
        FixPath        = $1
    endif
    ifeq ($(UNAME_S),Darwin)
        # macOS
        PACK_TOOL      := pack_resources
        RESOURCE_PACK  := ../resources.pack
        TEST_PACK      := test_resources.pack
        LDFLAGS        :=
        RMFILE         := rm -f
        MKDIR          := mkdir -p
        FixPath        = $1
    endif
endif

# Detectar todos los archivos en data/ como dependencias
DATA_FILES := $(shell find ../data -type f 2>/dev/null)

# ============================================================================
# Targets principales
# ============================================================================

.PHONY: all pack_tool resource_pack test_pack clean help

# Target por defecto: compilar herramienta
all: pack_tool

# Compilar herramienta de empaquetado
pack_tool: $(PACK_TOOL)

$(PACK_TOOL): $(PACK_SOURCES)
	@echo "=========================================="
	@echo "Compilando herramienta de empaquetado..."
	@echo "=========================================="
	$(CXX) $(CXXFLAGS) $(PACK_INCLUDES) $(PACK_SOURCES) $(LDFLAGS) -o $(PACK_TOOL)
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	@echo "✓ Herramienta compilada: $(PACK_TOOL)"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif

# Crear pack de recursos final
resource_pack: $(PACK_TOOL)
	@echo "=========================================="
	@echo "Generando resources.pack..."
	@echo "=========================================="
	@echo "Directorio de datos: ../data"
	@echo "Archivo de salida: $(RESOURCE_PACK)"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	./$(PACK_TOOL) ../data $(RESOURCE_PACK)
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	@echo "✓ Pack de recursos creado exitosamente"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif

# Crear pack de recursos de prueba
test_pack: $(PACK_TOOL)
	@echo "=========================================="
	@echo "Generando pack de prueba..."
	@echo "=========================================="
	@echo "Directorio de datos: ../data"
	@echo "Archivo de salida: $(TEST_PACK)"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	./$(PACK_TOOL) ../data $(TEST_PACK)
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	@echo "✓ Pack de prueba creado: $(TEST_PACK)"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif

# Limpiar archivos generados
clean:
	@echo "Limpiando archivos generados..."
ifeq ($(OS),Windows_NT)
	@if exist "$(PACK_TOOL)" $(RMFILE) "$(PACK_TOOL)"
	@if exist "$(TEST_PACK)" $(RMFILE) "$(TEST_PACK)"
else
	@$(RMFILE) $(PACK_TOOL) $(TEST_PACK)
endif
	@echo "✓ Limpieza completada"

# Mostrar ayuda
help:
	@echo "=========================================="
	@echo "ViBe3 Physics - Resource Packer Makefile"
	@echo "=========================================="
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	@echo "Comandos disponibles:"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	@echo "  make              - Compilar herramienta (equivalente a 'make pack_tool')"
	@echo "  make pack_tool    - Compilar herramienta de empaquetado"
	@echo "  make resource_pack - Crear ../resources.pack desde ../data"
	@echo "  make test_pack    - Crear pack de prueba (test_resources.pack)"
	@echo "  make clean        - Limpiar archivos generados"
	@echo "  make help         - Mostrar esta ayuda"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	@echo "Ejemplos de uso:"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
	@echo "  cd tools"
	@echo "  make                    # Compilar herramienta"
	@echo "  make resource_pack      # Crear pack de recursos"
	@echo "  ./$(PACK_TOOL) --help   # Ver ayuda de la herramienta"
ifeq ($(OS),Windows_NT)
	@echo.
else
	@echo ""
endif
