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

# Variables
TARGET_NAME    := coffee_crisis_arcade_edition
TARGET_FILE    := $(DIR_BIN)$(TARGET_NAME)
APP_NAME       := Coffee Crisis Arcade Edition
RELEASE_FOLDER := ccae_release
RELEASE_FILE   := $(RELEASE_FOLDER)/$(TARGET_NAME)
RESOURCE_FILE  := release/coffee.res
VERSION        := 2025-08-10

# 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 CMakeLists.txt)
APP_SOURCES := \
    source/animated_sprite.cpp \
    source/asset.cpp \
    source/audio.cpp \
    source/background.cpp \
    source/balloon_formations.cpp \
    source/balloon_manager.cpp \
    source/balloon.cpp \
    source/bullet.cpp \
    source/color.cpp \
    source/define_buttons.cpp \
    source/difficulty.cpp \
    source/director.cpp \
    source/enter_name.cpp \
    source/explosions.cpp \
    source/external/gif.cpp \
    source/external/jail_audio.cpp \
    source/external/jail_shader.cpp \
    source/fade.cpp \
    source/game_logo.cpp \
    source/global_events.cpp \
    source/global_inputs.cpp \
    source/input_types.cpp \
    source/input.cpp \
    source/item.cpp \
    source/lang.cpp \
    source/main.cpp \
    source/manage_hiscore_table.cpp \
    source/mouse.cpp \
    source/moving_sprite.cpp \
    source/options.cpp \
    source/param.cpp \
    source/path_sprite.cpp \
    source/player.cpp \
    source/resource.cpp \
    source/scoreboard.cpp \
    source/screen.cpp \
    source/sections/credits.cpp \
    source/sections/game.cpp \
    source/sections/hiscore_table.cpp \
    source/sections/instructions.cpp \
    source/sections/intro.cpp \
    source/sections/logo.cpp \
    source/sections/title.cpp \
    source/shutdown.cpp \
    source/smart_sprite.cpp \
    source/sprite.cpp \
    source/stage.cpp \
    source/system_utils.cpp \
    source/tabe.cpp \
    source/text.cpp \
    source/texture.cpp \
    source/tiled_bg.cpp \
    source/ui/menu_option.cpp \
    source/ui/menu_renderer.cpp \
    source/ui/notifier.cpp \
    source/ui/service_menu.cpp \
    source/ui/ui_message.cpp \
    source/ui/window_message.cpp \
    source/utils.cpp \
    source/writer.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++ -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 compilación
windows:
	@echo off
	windres release/coffee.rc -O coff -o $(RESOURCE_FILE)
	$(CXX) $(APP_SOURCES) $(RESOURCE_FILE) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE).exe"
	strip -s -R .comment -R .gnu.version "$(TARGET_FILE).exe" --strip-unneeded

windows_rec:
	@echo off
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DRECORDING $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)_rec.exe"

windows_debug:
	@echo off
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug.exe"

windows_release:
	@echo off

# Crea carpeta temporal 'RELEASE_FOLDER'
	powershell if (Test-Path "$(RELEASE_FOLDER)") {Remove-Item "$(RELEASE_FOLDER)" -Recurse -Force}
	powershell if (-not (Test-Path "$(RELEASE_FOLDER)")) {New-Item "$(RELEASE_FOLDER)" -ItemType Directory}

# Copia la carpeta 'data'
	powershell Copy-Item -Path "data" -Destination "$(RELEASE_FOLDER)" -recurse -Force

# Copia los ficheros que estan en la raíz del proyecto
	powershell Copy-Item "LICENSE" -Destination "$(RELEASE_FOLDER)"
	powershell Copy-Item "README.md" -Destination "$(RELEASE_FOLDER)"
	powershell Copy-Item "release\*.dll" -Destination "$(RELEASE_FOLDER)"

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

# Crea el fichero .zip
	powershell if (Test-Path "$(WINDOWS_RELEASE)") {Remove-Item "$(WINDOWS_RELEASE)"}
	powershell Compress-Archive -Path "$(RELEASE_FOLDER)"/* -DestinationPath "$(WINDOWS_RELEASE)"

# Elimina la carpeta temporal 'RELEASE_FOLDER'
	powershell if (Test-Path "$(RELEASE_FOLDER)") {Remove-Item "$(RELEASE_FOLDER)" -Recurse -Force}

macos:
	$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"

macos_debug:
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"

macos_release:
# 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 -R data "$(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
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

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

linux:
	$(CXX) $(APP_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"
	strip -s -R .comment -R .gnu.version "$(TARGET_FILE)" --strip-unneeded

linux_debug:
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"

linux_release:
# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"

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

# Copia ficheros
	cp -R data "$(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)" .

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

raspi:
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DVERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(TARGET_FILE)
	strip -s -R .comment -R .gnu.version $(TARGET_FILE) --strip-unneeded

raspi_debug:
	$(CXX) $(APP_SOURCES) $(INCLUDES) -DVERBOSE -DDEBUG $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"

raspi_release:
# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"

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

# Copia ficheros
	cp -R data "$(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)" .

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

anbernic:
# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"_anbernic

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

# Copia ficheros
	cp -R data "$(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:
	$(CXX) $(filter-out source/external/jail_audio.cpp,$(APP_SOURCES)) $(INCLUDES) -DNO_AUDIO $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)_no_audio"

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