# Directorios
DIR_ROOT       := $(dir $(abspath $(MAKEFILE_LIST)))
DIR_SOURCES    := $(addsuffix /, $(DIR_ROOT)source)
DIR_BIN        := $(addsuffix /, $(DIR_ROOT))
DIR_BUILD      := $(addsuffix /, $(DIR_ROOT)build)
DIR_TOOLS      := $(addsuffix /, $(DIR_ROOT)tools)

# Variables
TARGET_NAME    := vibe3_physics
TARGET_FILE    := $(DIR_BIN)$(TARGET_NAME)
APP_NAME       := ViBe3 Physics
RELEASE_FOLDER := vibe3_release
RELEASE_FILE   := $(RELEASE_FOLDER)/$(TARGET_NAME)
RESOURCE_FILE  := release/vibe3.res

# Variables para herramienta de empaquetado
ifeq ($(OS),Windows_NT)
    PACK_TOOL      := $(DIR_TOOLS)pack_resources.exe
    PACK_CXX       := $(CXX)
else
    PACK_TOOL      := $(DIR_TOOLS)pack_resources
    PACK_CXX       := $(CXX)
endif
PACK_SOURCES   := $(DIR_TOOLS)pack_resources.cpp $(DIR_SOURCES)resource_pack.cpp
PACK_INCLUDES  := -I$(DIR_ROOT)

# Versión automática basada en la fecha actual (específica por SO)
ifeq ($(OS),Windows_NT)
    VERSION := $(shell powershell -Command "Get-Date -Format 'yyyy-MM-dd'")
else
    VERSION := $(shell date +%Y-%m-%d)
endif

# Variables específicas para Windows (usando APP_NAME)
ifeq ($(OS),Windows_NT)
    WIN_TARGET_FILE    := $(DIR_BIN)$(APP_NAME)
    WIN_RELEASE_FILE   := $(RELEASE_FOLDER)/$(APP_NAME)
else
    WIN_TARGET_FILE    := $(TARGET_FILE)
    WIN_RELEASE_FILE   := $(RELEASE_FILE)
endif

# Nombres para los ficheros de lanzamiento
WINDOWS_RELEASE             := $(TARGET_NAME)-$(VERSION)-win32-x64.zip
MACOS_INTEL_RELEASE         := $(TARGET_FILE)-$(VERSION)-macos-intel.dmg
MACOS_APPLE_SILICON_RELEASE := $(TARGET_FILE)-$(VERSION)-macos-apple-silicon.dmg
LINUX_RELEASE               := $(TARGET_FILE)-$(VERSION)-linux.tar.gz
RASPI_RELEASE               := $(TARGET_FILE)-$(VERSION)-raspberry.tar.gz

# Lista completa de archivos fuente (basada en estructura de ViBe3)
APP_SOURCES := \
    source/ball.cpp \
    source/engine.cpp \
    source/main.cpp \
    source/resource_pack.cpp \
    source/external/mouse.cpp \
    source/external/sprite.cpp \
    source/external/texture.cpp \
    source/shapes/atom_shape.cpp \
    source/shapes/cube_shape.cpp \
    source/shapes/cylinder_shape.cpp \
    source/shapes/helix_shape.cpp \
    source/shapes/icosahedron_shape.cpp \
    source/shapes/png_shape.cpp \
    source/shapes/sphere_shape.cpp \
    source/shapes/torus_shape.cpp \
    source/shapes/wave_grid_shape.cpp

# Includes
INCLUDES := -Isource -Isource/external

# Variables según el sistema operativo
ifeq ($(OS),Windows_NT)
    FixPath         = $(subst /,\\,$1)
    CXXFLAGS        := -std=c++20 -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -static-libgcc -Wl,-Bstatic -lpthread -Wl,-Bdynamic -Wl,-subsystem,windows -DWINDOWS_BUILD
    CXXFLAGS_DEBUG  := -std=c++20 -Wall -g -D_DEBUG -DWINDOWS_BUILD
    LDFLAGS         := -lmingw32 -lws2_32 -lSDL3 -lopengl32
    RM              := del /Q
    MKDIR           := mkdir
else
    FixPath         = $1
    CXXFLAGS        := -std=c++20 -Wall -Os -ffunction-sections -fdata-sections
    CXXFLAGS_DEBUG  := -std=c++20 -Wall -g -D_DEBUG
    LDFLAGS         := -lSDL3
    RMFILE          := rm -f
    RMDIR           := rm -rdf
    MKDIR           := mkdir -p
    UNAME_S         := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        CXXFLAGS    += -DLINUX_BUILD
        LDFLAGS     += -lGL
    endif
    ifeq ($(UNAME_S),Darwin)
        CXXFLAGS       += -Wno-deprecated -DMACOS_BUILD
        CXXFLAGS_DEBUG += -Wno-deprecated -DMACOS_BUILD
        LDFLAGS        += -framework OpenGL
        # Configurar arquitectura (por defecto arm64, como en CMake)
        CXXFLAGS       += -arch arm64
        CXXFLAGS_DEBUG += -arch arm64
    endif
endif

# Reglas para herramienta de empaquetado y resources.pack
$(PACK_TOOL): $(PACK_SOURCES)
	@echo "Compilando herramienta de empaquetado..."
	$(PACK_CXX) -std=c++17 -Wall -Os $(PACK_INCLUDES) $(PACK_SOURCES) -o $(PACK_TOOL)
	@echo "✓ Herramienta de empaquetado lista: $(PACK_TOOL)"

pack_tool: $(PACK_TOOL)

resources.pack: $(PACK_TOOL)
	@echo "Generando resources.pack desde directorio data/..."
	$(PACK_TOOL) data resources.pack
	@echo "✓ resources.pack generado exitosamente"

# Reglas para compilación
windows:
	@echo off
	@echo Compilando para Windows con nombre: "$(APP_NAME).exe"
	windres release/vibe3.rc -O coff -o $(RESOURCE_FILE)
	$(CXX) $(APP_SOURCES) $(RESOURCE_FILE) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_TARGET_FILE).exe"
	strip -s -R .comment -R .gnu.version "$(WIN_TARGET_FILE).exe" --strip-unneeded

windows_rec:
	@echo off
	@echo Compilando version de grabacion para Windows: "$(APP_NAME)_rec.exe"
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DRECORDING $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_TARGET_FILE)_rec.exe"

windows_debug:
	@echo off
	@echo Compilando version debug para Windows: "$(APP_NAME)_debug.exe"
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(WIN_TARGET_FILE)_debug.exe"

windows_release: resources.pack
	@echo "Creando release para Windows - Version: $(VERSION)"

# Crea carpeta temporal 'RELEASE_FOLDER'
	@rm -rf "$(RELEASE_FOLDER)"
	@mkdir -p "$(RELEASE_FOLDER)"

# Copia el archivo 'resources.pack'
	@cp -f "resources.pack" "$(RELEASE_FOLDER)/"

# Copia los ficheros que estan en la raíz del proyecto
	@cp -f "LICENSE" "$(RELEASE_FOLDER)/" 2>/dev/null || echo "LICENSE not found (optional)"
	@cp -f "README.md" "$(RELEASE_FOLDER)/"
	@cp -f release/*.dll "$(RELEASE_FOLDER)/" 2>/dev/null || echo "No DLL files found (optional)"

# Compila
	@windres release/vibe3.rc -O coff -o $(RESOURCE_FILE)
	@$(CXX) $(APP_SOURCES) $(RESOURCE_FILE) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_RELEASE_FILE).exe"
	@strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded

# Crea el fichero .zip
	@rm -f "$(WINDOWS_RELEASE)"
	@powershell.exe -Command "Compress-Archive -Path '$(RELEASE_FOLDER)/*' -DestinationPath '$(WINDOWS_RELEASE)' -Force"
	@echo "Release creado: $(WINDOWS_RELEASE)"

# Elimina la carpeta temporal 'RELEASE_FOLDER'
	@rm -rf "$(RELEASE_FOLDER)"

macos:
	@echo "Compilando para macOS: $(TARGET_NAME)"
	$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"

macos_debug:
	@echo "Compilando version debug para macOS: $(TARGET_NAME)_debug"
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"

macos_release: resources.pack
	@echo "Creando release para macOS - Version: $(VERSION)"
# Elimina datos de compilaciones anteriores
	$(RMDIR) "$(RELEASE_FOLDER)"
	$(RMDIR) Frameworks
	$(RMFILE) tmp.dmg
	$(RMFILE) "$(MACOS_INTEL_RELEASE)"
	$(RMFILE) "$(MACOS_APPLE_SILICON_RELEASE)"

# Crea la carpeta temporal para hacer el trabajo y las carpetas obligatorias para crear una app de macos
	$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
	$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS"
	$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
	$(MKDIR) Frameworks

# Copia carpetas y ficheros
	cp resources.pack "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
	cp -R release/frameworks/SDL3.xcframework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
	cp -R release/frameworks/SDL3.xcframework Frameworks
	cp release/*.icns "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
	cp release/Info.plist "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents"
	cp LICENSE "$(RELEASE_FOLDER)"
	cp README.md "$(RELEASE_FOLDER)"

# Crea enlaces
	ln -s /Applications "$(RELEASE_FOLDER)"/Applications

# Compila la versión para procesadores Intel
ifdef ENABLE_MACOS_X86_64
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DMACOS_BUNDLE $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS/$(TARGET_NAME)" -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos10.15

# Firma la aplicación
	codesign --deep --force --sign - --timestamp=none "$(RELEASE_FOLDER)/$(APP_NAME).app"

# Empaqueta el .dmg de la versión Intel
	hdiutil create tmp.dmg -ov -volname "$(APP_NAME)" -fs HFS+ -srcfolder "$(RELEASE_FOLDER)" 
	hdiutil convert tmp.dmg -format UDZO -o "$(MACOS_INTEL_RELEASE)"
	$(RMFILE) tmp.dmg
	@echo "Release Intel creado: $(MACOS_INTEL_RELEASE)"
endif

# Compila la versión para procesadores Apple Silicon
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DMACOS_BUNDLE -DSDL_DISABLE_IMMINTRIN_H $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS/$(TARGET_NAME)" -rpath @executable_path/../Frameworks/ -target arm64-apple-macos11

# Firma la aplicación
	codesign --deep --force --sign - --timestamp=none "$(RELEASE_FOLDER)/$(APP_NAME).app"

# Empaqueta el .dmg de la versión Apple Silicon
	hdiutil create tmp.dmg -ov -volname "$(APP_NAME)" -fs HFS+ -srcfolder "$(RELEASE_FOLDER)" 
	hdiutil convert tmp.dmg -format UDZO -o "$(MACOS_APPLE_SILICON_RELEASE)"
	$(RMFILE) tmp.dmg
	@echo "Release Apple Silicon creado: $(MACOS_APPLE_SILICON_RELEASE)"

# Elimina las carpetas temporales
	$(RMDIR) Frameworks
	$(RMDIR) "$(RELEASE_FOLDER)"

linux:
	@echo "Compilando para Linux: $(TARGET_NAME)"
	$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"
	strip -s -R .comment -R .gnu.version "$(TARGET_FILE)" --strip-unneeded

linux_debug:
	@echo "Compilando version debug para Linux: $(TARGET_NAME)_debug"
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"

linux_release: resources.pack
	@echo "Creando release para Linux - Version: $(VERSION)"
# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"

# Crea la carpeta temporal para realizar el lanzamiento
	$(MKDIR) "$(RELEASE_FOLDER)"

# Copia ficheros
	cp resources.pack "$(RELEASE_FOLDER)"
	cp LICENSE "$(RELEASE_FOLDER)"
	cp README.md "$(RELEASE_FOLDER)"

# Compila
	$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FILE)"
	strip -s -R .comment -R .gnu.version "$(RELEASE_FILE)" --strip-unneeded

# Empaqueta ficheros
	$(RMFILE) "$(LINUX_RELEASE)"
	tar -czvf "$(LINUX_RELEASE)" -C "$(RELEASE_FOLDER)" .
	@echo "Release creado: $(LINUX_RELEASE)"

# Elimina la carpeta temporal
	$(RMDIR) "$(RELEASE_FOLDER)"

linux_release_desktop: resources.pack
	@echo "Creando release con integracion desktop para Linux - Version: $(VERSION)"
# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"

# Crea la estructura de directorios estándar para Linux
	$(MKDIR) "$(RELEASE_FOLDER)/$(TARGET_NAME)"
	$(MKDIR) "$(RELEASE_FOLDER)/$(TARGET_NAME)/bin"
	$(MKDIR) "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications"
	$(MKDIR) "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/icons/hicolor/256x256/apps"
	$(MKDIR) "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/$(TARGET_NAME)"

# Copia ficheros del juego
	cp resources.pack "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/$(TARGET_NAME)/"
	cp LICENSE "$(RELEASE_FOLDER)/$(TARGET_NAME)/"
	cp README.md "$(RELEASE_FOLDER)/$(TARGET_NAME)/"

# Compila el ejecutable
	$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FOLDER)/$(TARGET_NAME)/bin/$(TARGET_NAME)"
	strip -s -R .comment -R .gnu.version "$(RELEASE_FOLDER)/$(TARGET_NAME)/bin/$(TARGET_NAME)" --strip-unneeded

# Crea el archivo .desktop
	@echo '[Desktop Entry]' > "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Version=1.0' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Type=Application' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Name=$(APP_NAME)' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Comment=Arcade action game - defend Earth from alien invasion!' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Exec=/opt/$(TARGET_NAME)/bin/$(TARGET_NAME)' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Icon=$(TARGET_NAME)' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Path=/opt/$(TARGET_NAME)/share/$(TARGET_NAME)' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Terminal=false' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'StartupNotify=true' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Categories=Game;ArcadeGame;' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"
	@echo 'Keywords=arcade;action;shooter;retro;' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop"

# Copia el icono (si existe) y lo redimensiona si es necesario
	@if [ -f "release/icon.png" ]; then \
		if command -v magick >/dev/null 2>&1; then \
			magick "release/icon.png" -resize 256x256 "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png"; \
			echo "Icono redimensionado de release/icon.png (usando ImageMagick)"; \
		elif command -v convert >/dev/null 2>&1; then \
			convert "release/icon.png" -resize 256x256 "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png"; \
			echo "Icono redimensionado de release/icon.png (usando ImageMagick legacy)"; \
		elif command -v ffmpeg >/dev/null 2>&1; then \
			ffmpeg -i "release/icon.png" -vf scale=256:256 "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png" -y -loglevel quiet; \
			echo "Icono redimensionado de release/icon.png (usando ffmpeg)"; \
		else \
			cp "release/icon.png" "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png"; \
			echo "Icono copiado sin redimensionar (instalar ImageMagick o ffmpeg para redimensionado automatico)"; \
		fi; \
	elif [ -f "release/coffee.png" ]; then \
		cp "release/coffee.png" "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png"; \
		echo "Icono copiado desde release/coffee.png"; \
	else \
		echo "Advertencia: No se encontró release/icon.png ni release/coffee.png - crear icono manualmente"; \
	fi

# Crea script de instalación
	@echo '#!/bin/bash' > "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'echo "Instalando $(APP_NAME)..."' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo mkdir -p /opt/$(TARGET_NAME)' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo cp -R bin /opt/$(TARGET_NAME)/' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo cp -R share /opt/$(TARGET_NAME)/' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo cp LICENSE /opt/$(TARGET_NAME)/' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo cp README.md /opt/$(TARGET_NAME)/' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo mkdir -p /usr/share/applications' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo mkdir -p /usr/share/icons/hicolor/256x256/apps' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo cp /opt/$(TARGET_NAME)/share/applications/$(TARGET_NAME).desktop /usr/share/applications/' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo cp /opt/$(TARGET_NAME)/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png /usr/share/icons/hicolor/256x256/apps/' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo update-desktop-database /usr/share/applications 2>/dev/null || true' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'sudo gtk-update-icon-cache /usr/share/icons/hicolor 2>/dev/null || true' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'echo "$(APP_NAME) instalado correctamente!"' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	@echo 'echo "Ya puedes encontrarlo en el menu de aplicaciones en la categoria Juegos."' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"
	chmod +x "$(RELEASE_FOLDER)/$(TARGET_NAME)/install.sh"

# Crea script de desinstalación
	@echo '#!/bin/bash' > "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"
	@echo 'echo "Desinstalando $(APP_NAME)..."' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"
	@echo 'sudo rm -rf /opt/$(TARGET_NAME)' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"
	@echo 'sudo rm -f /usr/share/applications/$(TARGET_NAME).desktop' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"
	@echo 'sudo rm -f /usr/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"
	@echo 'sudo update-desktop-database /usr/share/applications 2>/dev/null || true' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"
	@echo 'sudo gtk-update-icon-cache /usr/share/icons/hicolor 2>/dev/null || true' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"
	@echo 'echo "$(APP_NAME) desinstalado correctamente."' >> "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"
	chmod +x "$(RELEASE_FOLDER)/$(TARGET_NAME)/uninstall.sh"

# Empaqueta ficheros
	$(RMFILE) "$(TARGET_NAME)-$(VERSION)-linux-desktop.tar.gz"
	tar -czvf "$(TARGET_NAME)-$(VERSION)-linux-desktop.tar.gz" -C "$(RELEASE_FOLDER)" .
	@echo "Release con integracion desktop creado: $(TARGET_NAME)-$(VERSION)-linux-desktop.tar.gz"
	@echo "Para instalar: extraer y ejecutar ./$(TARGET_NAME)/install.sh"

# Elimina la carpeta temporal
	$(RMDIR) "$(RELEASE_FOLDER)"

raspi:
	@echo "Compilando para Raspberry Pi: $(TARGET_NAME)"
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DVERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(TARGET_FILE)
	strip -s -R .comment -R .gnu.version $(TARGET_FILE) --strip-unneeded

raspi_debug:
	@echo "Compilando version debug para Raspberry Pi: $(TARGET_NAME)_debug"
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DVERBOSE -DDEBUG $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"

raspi_release: resources.pack
	@echo "Creando release para Raspberry Pi - Version: $(VERSION)"
# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"

# Crea la carpeta temporal para realizar el lanzamiento
	$(MKDIR) "$(RELEASE_FOLDER)"

# Copia ficheros
	cp resources.pack "$(RELEASE_FOLDER)"
	cp LICENSE "$(RELEASE_FOLDER)"
	cp README.md "$(RELEASE_FOLDER)"

# Compila
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DVERBOSE $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FILE)"
	strip -s -R .comment -R .gnu.version "$(RELEASE_FILE)" --strip-unneeded

# Empaqueta ficheros
	$(RMFILE) "$(RASPI_RELEASE)"
	tar -czvf "$(RASPI_RELEASE)" -C "$(RELEASE_FOLDER)" .
	@echo "Release creado: $(RASPI_RELEASE)"

# Elimina la carpeta temporal
	$(RMDIR) "$(RELEASE_FOLDER)"

anbernic: resources.pack
	@echo "Compilando para Anbernic: $(TARGET_NAME)"
# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"_anbernic

# Crea la carpeta temporal para realizar el lanzamiento
	$(MKDIR) "$(RELEASE_FOLDER)"_anbernic

# Copia ficheros
	cp resources.pack "$(RELEASE_FOLDER)"_anbernic

# Compila
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DANBERNIC -DNO_SHADERS -DARCADE -DVERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(RELEASE_FOLDER)_anbernic/$(TARGET_NAME)

# Opción para deshabilitar audio (equivalente a la opción DISABLE_AUDIO de CMake)
no_audio:
	@echo "Compilando sin audio: $(TARGET_NAME)_no_audio"
	$(CXX) $(filter-out source/external/jail_audio.cpp,$(APP_SOURCES)) $(INCLUDES) -DNO_AUDIO $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)_no_audio"

# Regla para mostrar la versión actual
show_version:
	@echo "Version actual: $(VERSION)"

# Regla de ayuda
help:
	@echo "Makefile para ViBe3 Physics"
	@echo "Comandos disponibles:"
	@echo "  windows          - Compilar para Windows"
	@echo "  windows_debug    - Compilar debug para Windows"
	@echo "  windows_release  - Crear release completo para Windows (.zip)"
	@echo "  linux            - Compilar para Linux"
	@echo "  linux_debug      - Compilar debug para Linux"
	@echo "  linux_release    - Crear release basico para Linux (.tar.gz)"
	@echo "  linux_release_desktop - Crear release con integracion desktop para Linux"
	@echo "  macos            - Compilar para macOS"
	@echo "  macos_debug      - Compilar debug para macOS"
	@echo "  macos_release    - Crear release completo para macOS (.dmg)"
	@echo "  pack_tool        - Compilar herramienta de empaquetado"
	@echo "  resources.pack   - Generar pack de recursos desde data/"
	@echo "  show_version     - Mostrar version actual ($(VERSION))"
	@echo "  help             - Mostrar esta ayuda"

.PHONY: windows windows_rec windows_debug windows_release macos macos_debug macos_release linux linux_debug linux_release linux_release_desktop raspi raspi_debug raspi_release anbernic no_audio show_version help pack_tool