executable = jaildoctors_dilemma
source = $(shell find source -name "*.cpp")
appName = JailDoctor's Dilemma
releaseFolder = jdd_release

# Automatic version based on current date (OS-specific)
ifeq ($(OS),Windows_NT)
    VERSION := $(shell powershell -Command "Get-Date -Format 'yyyy-MM-dd'")
else
    VERSION := $(shell date +%Y-%m-%d)
endif

# Release names
windowsRelease = $(executable)-$(VERSION)-win32-x64.zip
macosIntelRelease = $(executable)-$(VERSION)-macos-intel.dmg
macosAppleSiliconRelease = $(executable)-$(VERSION)-macos-apple-silicon.dmg
linuxRelease = $(executable)-$(VERSION)-linux.tar.gz

# Specify the C++ standard
cpp_standard = c++20

# Resource packing tool
packTool = tools/pack_resources/pack_resources
packToolSource = tools/pack_resources/pack_resources.cpp source/core/resources/resource_pack.cpp

# Build the resource packing tool
pack_tool:
	@echo "Building pack_resources tool..."
	@cd tools/pack_resources && $(MAKE)

# Create resources.pack from data directory
resources.pack: pack_tool
	@echo "Creating resources.pack..."
	@$(packTool) data resources.pack

windows:
	@echo off
	g++ $(source) -Isource -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL3main -lSDL3 -lopengl32 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(executable).exe"
	strip -s -R .comment -R .gnu.version "$(executable).exe" --strip-unneeded

windows_debug:
	@echo off
	g++ $(source) -Isource -D DEBUG -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL3main -lSDL3 -lopengl32 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(executable)_debug.exe"
	strip -s -R .comment -R .gnu.version "$(executable)_debug.exe" --strip-unneeded

windows_release:
	@echo off

# Build packing tool and create resources.pack
	@$(MAKE) pack_tool
	@$(MAKE) resources.pack

# Create release folder
	powershell if (Test-Path "$(releaseFolder)") {Remove-Item "$(releaseFolder)" -Recurse -Force}
	powershell if (-not (Test-Path "$(releaseFolder)")) {New-Item "$(releaseFolder)" -ItemType Directory}

# Copy resources.pack instead of data folder
	powershell Copy-Item "resources.pack" -Destination "$(releaseFolder)"

# Copy root files
	powershell Copy-Item "LICENSE" -Destination "$(releaseFolder)"
	powershell Copy-Item "README.md" -Destination "$(releaseFolder)"
	powershell Copy-Item "gamecontrollerdb.txt" -Destination "$(releaseFolder)"
	powershell Copy-Item "release\*.dll" -Destination "$(releaseFolder)"

# Build with RELEASE_BUILD flag (include jdd.res for icon)
	g++ $(source) release/jdd.res -Isource -D RELEASE_BUILD -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL3main -lSDL3 -lopengl32 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o "$(releaseFolder)/$(executable).exe"
	strip -s -R .comment -R .gnu.version "$(releaseFolder)/$(executable).exe" --strip-unneeded

# Create ZIP
	powershell if (Test-Path $(windowsRelease)) {Remove-Item $(windowsRelease)}
	powershell Compress-Archive -Path "$(releaseFolder)"/* -DestinationPath $(windowsRelease)

# Remove folder
	powershell if (Test-Path "$(releaseFolder)") {Remove-Item "$(releaseFolder)" -Recurse -Force}

macos:
	clang++ $(source) -Isource -std=$(cpp_standard) -Wall -Os -lSDL3 -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(executable)"

macos_debug:
	clang++ $(source) -Isource -D DEBUG -std=$(cpp_standard) -Wall -Os -lSDL3 -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(executable)_debug"

macos_release:
# Build packing tool and create resources.pack
	@$(MAKE) pack_tool
	@$(MAKE) resources.pack
	@echo "Creating macOS release - Version: $(VERSION)"

# Verify and install create-dmg if necessary
	@which create-dmg > /dev/null || (echo "Installing create-dmg..." && brew install create-dmg)

# Remove data and possible data from previous builds
	rm -rdf "$(releaseFolder)"
	rm -rdf Frameworks
	rm -f tmp.dmg
	rm -f "$(macosIntelRelease)"
	rm -f "$(macosAppleSiliconRelease)"

# Create folders
	mkdir -p "$(releaseFolder)/$(appName).app/Contents/Frameworks"
	mkdir -p "$(releaseFolder)/$(appName).app/Contents/MacOS"
	mkdir -p "$(releaseFolder)/$(appName).app/Contents/Resources"
	mkdir -p Frameworks

# Copy resources.pack instead of data folder
	cp resources.pack "$(releaseFolder)/$(appName).app/Contents/Resources"
	cp gamecontrollerdb.txt "$(releaseFolder)/$(appName).app/Contents/Resources"
	cp -R release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(releaseFolder)/$(appName).app/Contents/Frameworks"
	cp -R release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework Frameworks

# Copy files
	cp release/*.icns "$(releaseFolder)/$(appName).app/Contents/Resources"
	cp release/Info.plist "$(releaseFolder)/$(appName).app/Contents"
	cp LICENSE "$(releaseFolder)"
	cp README.md "$(releaseFolder)"

# Build INTEL with RELEASE_BUILD flag
	clang++ $(source) -Isource -D MACOS_BUNDLE -D RELEASE_BUILD -std=$(cpp_standard) -Wall -Os -framework SDL3 -F ./Frameworks -framework OpenGL -Wno-deprecated  -ffunction-sections -fdata-sections -o "$(releaseFolder)/$(appName).app/Contents/MacOS/$(executable)" -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos10.15

# Code sign the Intel application
	codesign --deep --force --sign - --timestamp=none "$(releaseFolder)/$(appName).app"

# Build INTEL DMG with create-dmg
	@echo "Creating Intel DMG with 96x96 icons..."
	create-dmg \
	  --volname "$(appName)" \
	  --window-pos 200 120 \
	  --window-size 720 300 \
	  --icon-size 96 \
	  --text-size 12 \
	  --icon "$(appName).app" 278 102 \
	  --icon "LICENSE" 441 102 \
	  --icon "README.md" 604 102 \
	  --app-drop-link 115 102 \
	  --hide-extension "$(appName).app" \
	  "$(macosIntelRelease)" \
	  "$(releaseFolder)" || true
	@echo "Intel release created: $(macosIntelRelease)"

# Build APPLE SILICON with RELEASE_BUILD flag
	clang++ $(source) -Isource -D MACOS_BUNDLE -D RELEASE_BUILD -std=$(cpp_standard) -Wall -Os -framework SDL3 -F ./Frameworks -framework OpenGL -Wno-deprecated  -ffunction-sections -fdata-sections -o "$(releaseFolder)/$(appName).app/Contents/MacOS/$(executable)" -rpath @executable_path/../Frameworks/ -target arm64-apple-macos11

# Code sign the Apple Silicon application
	codesign --deep --force --sign - --timestamp=none "$(releaseFolder)/$(appName).app"

# Build APPLE SILICON DMG with create-dmg
	@echo "Creating Apple Silicon DMG with 96x96 icons..."
	create-dmg \
	  --volname "$(appName)" \
	  --window-pos 200 120 \
	  --window-size 720 300 \
	  --icon-size 96 \
	  --text-size 12 \
	  --icon "$(appName).app" 278 102 \
	  --icon "LICENSE" 441 102 \
	  --icon "README.md" 604 102 \
	  --app-drop-link 115 102 \
	  --hide-extension "$(appName).app" \
	  "$(macosAppleSiliconRelease)" \
	  "$(releaseFolder)" || true
	@echo "Apple Silicon release created: $(macosAppleSiliconRelease)"

# Remove data
	rm -rdf Frameworks
	rm -rdf "$(releaseFolder)"

linux:
	g++ $(source) -Isource -std=$(cpp_standard) -Wall -Os -lSDL3 -lGL -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(executable)"
	strip -s -R .comment -R .gnu.version "$(executable)" --strip-unneeded

linux_debug:
	g++ $(source) -Isource -D DEBUG -std=$(cpp_standard) -Wall -Os -lSDL3 -lGL -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(executable)_debug"
	strip -s -R .comment -R .gnu.version "$(executable)_debug" --strip-unneeded

linux_release:
# Build packing tool and create resources.pack
	@$(MAKE) pack_tool
	@$(MAKE) resources.pack

# Remove data
	rm -rdf "$(releaseFolder)"

# Create folders
	mkdir -p "$(releaseFolder)"

# Copy resources.pack instead of data folder
	cp resources.pack "$(releaseFolder)"
	cp LICENSE "$(releaseFolder)"
	cp README.md "$(releaseFolder)"
	cp gamecontrollerdb.txt "$(releaseFolder)"

# Build with RELEASE_BUILD flag
	g++ $(source) -Isource -D RELEASE_BUILD -std=$(cpp_standard) -Wall -Os -lSDL3 -lGL -ffunction-sections -fdata-sections -Wl,--gc-sections -o "$(releaseFolder)/$(executable)"
	strip -s -R .comment -R .gnu.version "$(releaseFolder)/$(executable)" --strip-unneeded

# Pack files
	rm -f "$(linuxRelease)"
	cd "$(releaseFolder)" &&	tar -czvf "../$(linuxRelease)" *

# Remove data
	rm -rdf "$(releaseFolder)"