# 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-03-25

# 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

# Includes
INCLUDES := -I$(DIR_SOURCES)

# Variables según el sistema operativo
ifeq ($(OS),Windows_NT)
    FixPath         = $(subst /,\\,$1)
    SOURCES         := source/*.cpp
    CXXFLAGS        := -std=c++20 -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows
    CXXFLAGS_DEBUG  := -std=c++20 -Wall -g
    LDFLAGS         := -lmingw32 -lws2_32 -lSDL3 -lopengl32
    RM              := del /Q
    MKDIR           := mkdir
else
    FixPath         = $1
    SOURCES         := source/*.cpp
    CXXFLAGS        := -std=c++20 -Wall -Os -ffunction-sections -fdata-sections
    CXXFLAGS_DEBUG  := -std=c++20 -Wall -g
    LDFLAGS         := -lSDL3
    RMFILE          := rm -f
	RMDIR			:= rm -rdf
    MKDIR           := mkdir -p
    UNAME_S         := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        LDFLAGS     += -lGL
    endif
    ifeq ($(UNAME_S),Darwin)
        CXXFLAGS       += -Wno-deprecated
        CXXFLAGS_DEBUG += -Wno-deprecated
        LDFLAGS        += -framework OpenGL
    endif
endif

# Reglas para compilación
windows:
	@echo off
	windres release/coffee.rc -O coff -o $(RESOURCE_FILE)
	$(CXX) $(SOURCES) $(RESOURCE_FILE) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE).exe"
	strip -s -R .comment -R .gnu.version "$(TARGET_FILE).exe" --strip-unneeded

windows_rec:
	@echo off
	$(CXX) $(SOURCES) -D RECORDING $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)_rec.exe"

windows_debug:
	@echo off
	$(CXX) $(SOURCES) -D DEBUG -D VERBOSE $(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) $(SOURCES) $(RESOURCE_FILE) $(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) $(SOURCES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"

macos_debug:
	$(CXX) $(SOURCES) -D DEBUG -D VERBOSE $(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/SDL2.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
	cp -R release/SDL2.framework 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) $(SOURCES) -D MACOS_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) $(SOURCES) -D MACOS_BUNDLE -D SDL_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) $(SOURCES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"
	strip -s -R .comment -R .gnu.version "$(TARGET_FILE)" --strip-unneeded

linux_debug:
	$(CXX) $(SOURCES) -D DEBUG -D VERBOSE $(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) $(SOURCES) $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FILE)"
	strip -s -R .comment -R .gnu.version "$(RELEASE_FILE)" --strip-unneeded

# Empaqueta ficheros
	$(RMFILE) "$(LINUX_RELEASE)"
	cd "$(RELEASE_FOLDER)" && tar -czvf "$(LINUX_RELEASE)" *

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

raspi:
	$(CXX) $(SOURCES) -D VERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(TARGET_FILE)
	strip -s -R .comment -R .gnu.version $(TARGET_FILE) --strip-unneeded

raspi_debug:
	$(CXX) $(SOURCES) -D VERBOSE -D DEBUG $(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) $(SOURCES) -D VERBOSE $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FILE)"
	strip -s -R .comment -R .gnu.version "$(RELEASE_FILE)" --strip-unneeded

# Empaqueta ficheros
	$(RMFILE) "$(LINUX_RELEASE)"
	cd "$(RELEASE_FOLDER)" && tar -czvf "$(RASPI_RELEASE)" *

# 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) $(SOURCES) -D ANBERNIC -D NO_SHADERS -D ARCADE -D VERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(RELEASE_FOLDER)_anbernic/$(TARGET_NAME)