# 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/windows/vibe3.res
DIST_DIR       := dist

# 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             := $(DIST_DIR)/$(TARGET_NAME)-$(VERSION)-win32-x64.zip
MACOS_INTEL_RELEASE         := $(DIST_DIR)/$(TARGET_NAME)-$(VERSION)-macos-intel.dmg
MACOS_APPLE_SILICON_RELEASE := $(DIST_DIR)/$(TARGET_NAME)-$(VERSION)-macos-apple-silicon.dmg
LINUX_RELEASE               := $(DIST_DIR)/$(TARGET_NAME)-$(VERSION)-linux.tar.gz
RASPI_RELEASE               := $(DIST_DIR)/$(TARGET_NAME)-$(VERSION)-raspberry.tar.gz

# Lista completa de archivos fuente (detección automática con wildcards, como CMakeLists.txt)
APP_SOURCES := $(wildcard source/*.cpp) \
               $(wildcard source/external/*.cpp) \
               $(wildcard source/shapes/*.cpp) \
               $(wildcard source/themes/*.cpp) \
               $(wildcard source/state/*.cpp) \
               $(wildcard source/input/*.cpp) \
               $(wildcard source/scene/*.cpp) \
               $(wildcard source/shapes_mgr/*.cpp) \
               $(wildcard source/boids_mgr/*.cpp) \
               $(wildcard source/text/*.cpp) \
               $(wildcard source/ui/*.cpp)

# Excluir archivos antiguos si existen
APP_SOURCES := $(filter-out source/main_old.cpp, $(APP_SOURCES))

# Includes
INCLUDES := -Isource -Isource/external

# Variables según el sistema operativo
CXXFLAGS_BASE   := -std=c++20 -Wall
CXXFLAGS        := $(CXXFLAGS_BASE) -Os -ffunction-sections -fdata-sections
LDFLAGS         :=

ifeq ($(OS),Windows_NT)
    FixPath         = $(subst /,\\,$1)
    CXXFLAGS        += -DWINDOWS_BUILD
    LDFLAGS         += -Wl,--gc-sections -static-libstdc++ -static-libgcc \
                       -Wl,-Bstatic -lpthread -Wl,-Bdynamic -Wl,-subsystem,windows \
                       -lmingw32 -lws2_32 -lSDL3 -lSDL3_ttf
    RMFILE          := del /Q
    RMDIR           := rmdir /S /Q
    MKDIR           := mkdir
else
    FixPath         = $1
    LDFLAGS         += -lSDL3 -lSDL3_ttf
    RMFILE          := rm -f
    RMDIR           := rm -rf
    MKDIR           := mkdir -p
    UNAME_S         := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        CXXFLAGS       += -DLINUX_BUILD
    endif
    ifeq ($(UNAME_S),Darwin)
        CXXFLAGS       += -DMACOS_BUILD -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)

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

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

# Target para forzar regeneración de resources.pack (usado por releases)
.PHONY: force_resource_pack
force_resource_pack: $(PACK_TOOL)
	@echo "Regenerando resources.pack para release..."
	$(PACK_TOOL) data resources.pack
	@echo "✓ resources.pack regenerado exitosamente"

# Reglas para compilación
windows:
	@echo off
	@echo Compilando para Windows con nombre: "$(APP_NAME).exe"
	windres release/windows/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_release: force_resource_pack
	@echo "Creando release para Windows - Version: $(VERSION)"

# Crea carpeta temporal 'RELEASE_FOLDER'
	@if exist "$(RELEASE_FOLDER)" rmdir /S /Q "$(RELEASE_FOLDER)"
	@mkdir "$(RELEASE_FOLDER)"

# Copia el archivo 'resources.pack'
	@copy /Y "resources.pack" "$(RELEASE_FOLDER)\" >nul

# Copia los ficheros que estan en la raíz del proyecto
	@copy /Y "LICENSE" "$(RELEASE_FOLDER)\" >nul 2>&1 || echo LICENSE not found (optional)
	@copy /Y "README.md" "$(RELEASE_FOLDER)\" >nul
	@copy /Y release\windows\dll\*.dll "$(RELEASE_FOLDER)\" >nul 2>&1 || echo DLLs copied successfully

# Compila
	@if not exist "$(DIST_DIR)" mkdir "$(DIST_DIR)"
	@windres release/windows/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
	@if exist "$(WINDOWS_RELEASE)" del /Q "$(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'
	@rmdir /S /Q "$(RELEASE_FOLDER)"

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

macos_release: force_resource_pack
	@echo "Creando release para macOS - Version: $(VERSION)"

# Verificar e instalar create-dmg si es necesario
	@which create-dmg > /dev/null || (echo "Instalando create-dmg..." && brew install create-dmg)

# Elimina datos de compilaciones anteriores
	$(RMDIR) "$(RELEASE_FOLDER)"
	$(RMDIR) Frameworks
	$(RMFILE) "$(MACOS_INTEL_RELEASE)"
	$(RMFILE) "$(MACOS_APPLE_SILICON_RELEASE)"

# Limpia archivos temporales de create-dmg y desmonta volúmenes
	@echo "Limpiando archivos temporales y volúmenes montados..."
	@rm -f rw.*.dmg 2>/dev/null || true
	@hdiutil detach "/Volumes/$(APP_NAME)" 2>/dev/null || true
	@hdiutil detach "/Volumes/ViBe3 Physics" 2>/dev/null || true

# 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/macos/frameworks/SDL3.xcframework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
	cp -R release/macos/frameworks/SDL3_ttf.xcframework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
	cp -R release/macos/frameworks/SDL3.xcframework Frameworks
	cp -R release/macos/frameworks/SDL3_ttf.xcframework Frameworks
	cp release/icons/*.icns "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
	cp release/macos/Info.plist "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents"
	$(MKDIR) "$(DIST_DIR)"
	cp LICENSE "$(RELEASE_FOLDER)"
	cp README.md "$(RELEASE_FOLDER)"

# NOTA: create-dmg crea automáticamente el enlace a /Applications con --app-drop-link
# No es necesario crearlo manualmente aquí

# 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-macos12

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

# Empaqueta el .dmg de la versión Intel con create-dmg
	@echo "Creando DMG Intel con iconos de 96x96..."
	@create-dmg \
	  --volname "$(APP_NAME)" \
	  --window-pos 200 120 \
	  --window-size 720 300 \
	  --icon-size 96 \
	  --text-size 12 \
	  --icon "$(APP_NAME).app" 278 102 \
	  --icon "LICENSE" 441 102 \
	  --icon "README.md" 604 102 \
	  --app-drop-link 115 102 \
	  --hide-extension "$(APP_NAME).app" \
	  "$(MACOS_INTEL_RELEASE)" \
	  "$(RELEASE_FOLDER)"
	@if [ -f "$(MACOS_INTEL_RELEASE)" ]; then \
		echo "✓ Release Intel creado exitosamente: $(MACOS_INTEL_RELEASE)"; \
	else \
		echo "✗ Error: No se pudo crear el DMG Intel"; \
		exit 1; \
	fi
	@rm -f rw.*.dmg 2>/dev/null || true
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-macos12

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

# Empaqueta el .dmg de la versión Apple Silicon con create-dmg
	@echo "Creando DMG Apple Silicon con iconos de 96x96..."
	@create-dmg \
	  --volname "$(APP_NAME)" \
	  --window-pos 200 120 \
	  --window-size 720 300 \
	  --icon-size 96 \
	  --text-size 12 \
	  --icon "$(APP_NAME).app" 278 102 \
	  --icon "LICENSE" 441 102 \
	  --icon "README.md" 604 102 \
	  --app-drop-link 115 102 \
	  --hide-extension "$(APP_NAME).app" \
	  "$(MACOS_APPLE_SILICON_RELEASE)" \
	  "$(RELEASE_FOLDER)"
	@if [ -f "$(MACOS_APPLE_SILICON_RELEASE)" ]; then \
		echo "✓ Release Apple Silicon creado exitosamente: $(MACOS_APPLE_SILICON_RELEASE)"; \
	else \
		echo "✗ Error: No se pudo crear el DMG Apple Silicon"; \
		exit 1; \
	fi
	@rm -f rw.*.dmg 2>/dev/null || true

# 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_release: force_resource_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)"
	$(MKDIR) "$(DIST_DIR)"

# 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: force_resource_pack
	@echo "Creando release con integracion desktop para Linux - Version: $(VERSION)"
# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"

# Crea la carpeta de distribución y la estructura de directorios estándar para Linux
	$(MKDIR) "$(DIST_DIR)"
	$(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/icons/icon.png" ]; then \
		if command -v magick >/dev/null 2>&1; then \
			magick "release/icons/icon.png" -resize 256x256 "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png"; \
			echo "Icono redimensionado de release/icons/icon.png (usando ImageMagick)"; \
		elif command -v convert >/dev/null 2>&1; then \
			convert "release/icons/icon.png" -resize 256x256 "$(RELEASE_FOLDER)/$(TARGET_NAME)/share/icons/hicolor/256x256/apps/$(TARGET_NAME).png"; \
			echo "Icono redimensionado de release/icons/icon.png (usando ImageMagick legacy)"; \
		elif command -v ffmpeg >/dev/null 2>&1; then \
			ffmpeg -i "release/icons/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/icons/icon.png (usando ffmpeg)"; \
		else \
			cp "release/icons/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; \
	else \
		echo "Advertencia: No se encontró release/icons/icon.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) "$(DIST_DIR)/$(TARGET_NAME)-$(VERSION)-linux-desktop.tar.gz"
	tar -czvf "$(DIST_DIR)/$(TARGET_NAME)-$(VERSION)-linux-desktop.tar.gz" -C "$(RELEASE_FOLDER)" .
	@echo "Release con integracion desktop creado: $(DIST_DIR)/$(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) $(CXXFLAGS) $(LDFLAGS) -o $(TARGET_FILE)
	strip -s -R .comment -R .gnu.version $(TARGET_FILE) --strip-unneeded

raspi_release: force_resource_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)"
	$(MKDIR) "$(DIST_DIR)"

# 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) "$(RASPI_RELEASE)"
	tar -czvf "$(RASPI_RELEASE)" -C "$(RELEASE_FOLDER)" .
	@echo "Release creado: $(RASPI_RELEASE)"

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

# 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_release       - Crear release completo para Windows (.zip)"
	@echo "  linux                 - Compilar 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_release         - Crear release completo para macOS (.dmg)"
	@echo "  raspi                 - Compilar para Raspberry Pi"
	@echo "  raspi_release         - Crear release para Raspberry Pi (.tar.gz)"
	@echo "  pack_tool             - Compilar herramienta de empaquetado"
	@echo "  resources.pack        - Generar pack de recursos desde data/"
	@echo "  force_resource_pack   - Regenerar resources.pack (usado por releases)"
	@echo "  show_version          - Mostrar version actual ($(VERSION))"
	@echo "  help                  - Mostrar esta ayuda"

.PHONY: windows windows_release macos macos_release linux linux_release linux_release_desktop raspi raspi_release show_version help pack_tool force_resource_pack