executable = jaildoctors_dilemma
source = source/*.cpp
appName = JailDoctor's Dilemma
releaseFolder = jdd_release
version = v1.10

# 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

windows:
	@echo off
	g++ $(source) -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -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) -D DEBUG -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -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

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

# Prepare data folder
	powershell Copy-Item -Path "data" -Destination "$(releaseFolder)" -recurse -Force
	powershell if (Test-Path "$(releaseFolder)\data\room\map.world") {Remove-Item "$(releaseFolder)\data\room\map.world" -Recurse -Force}
	powershell if (Test-Path "$(releaseFolder)\data\room\standard.tsx") {Remove-Item "$(releaseFolder)\data\room\standard.tsx" -Recurse -Force}
	
# Copy root files
	powershell Copy-Item "LICENSE" -Destination "$(releaseFolder)"
	powershell Copy-Item "README.md" -Destination "$(releaseFolder)"
	powershell Copy-Item "release\*.dll" -Destination "$(releaseFolder)"

# Build
	g++ $(source) -std=$(cpp_standard) -Wall -Os -lmingw32 -lws2_32 -lSDL2main -lSDL2 -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) -std=$(cpp_standard) -Wall -Os -lSDL2 -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(executable)"

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

macos_release:
# 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 folders
	cp -R data "$(releaseFolder)/$(appName).app/Contents/Resources"
	cp -R release/SDL2.framework "$(releaseFolder)/$(appName).app/Contents/Frameworks"
	cp -R release/SDL2.framework Frameworks

# Delete data
	rm -f "$(releaseFolder)/$(appName).app/Contents/Resources/data/room/map.world"
	rm -f "$(releaseFolder)/$(appName).app/Contents/Resources/data/room/standard.tsx"

# 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)"

# Create links
	ln -s /Applications "$(releaseFolder)"/Applications

# Build INTEL
	clang++ $(source) -D MACOS_BUNDLE -std=$(cpp_standard) -Wall -Os -framework SDL2 -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

# Build INTEL DMG
	hdiutil create tmp.dmg -ov -volname "$(appName)" -fs HFS+ -srcfolder "$(releaseFolder)" 
	hdiutil convert tmp.dmg -format UDZO -o "$(macosIntelRelease)"
	rm -f tmp.dmg

# Build APPLE SILICON	
	clang++ $(source) -D MACOS_BUNDLE -std=$(cpp_standard) -Wall -Os -framework SDL2 -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

# Build APPLE SILICON DMG
	hdiutil create tmp.dmg -ov -volname "$(appName)" -fs HFS+ -srcfolder "$(releaseFolder)" 
	hdiutil convert tmp.dmg -format UDZO -o "$(macosAppleSiliconRelease)"
	rm -f tmp.dmg

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

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

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

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

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

# Copy data
	cp -R data "$(releaseFolder)"
	cp LICENSE "$(releaseFolder)"
	cp README.md "$(releaseFolder)"

# Delete data
	rm -f "$(releaseFolder)/data/room/map.world"
	rm -f "$(releaseFolder)/data/room/standard.tsx"

# Build
	g++ $(source) -std=$(cpp_standard) -Wall -Os -lSDL2 -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)"