# Makefile by Roberto Prieto - http://www.megastormsystems.com
# March 2024 - Improved for better Linux support (owner, permissions, icon, etc)
# May 2018

# Application output name
PROGNAME64=Pac-Man Evolution
PKGNAME=PacManEvolution

# Compiler, option and source path
CC=g++
CCFLAGS=-O3 -w -march=native -s -std=c++11
LIBS=-lCRM64Pro -ldl
PATHSRC=../../source/
PATHSRC2=../../source/res/
PATHOUT=../../bin/

# Object files
OBJS =  ArtificialNeuralNet.o EVNTrainer.o GeneticAlgorithm.o Brains.o BrainsFactory.o \
		GameField.o HoF.o MapSearchAStar.o MazeDynamic.o MemoryManager.o Menu.o Objects.o \
		ObjectsGhost.o ObjectsPacMan.o ResourceManager.o PME_icon.o Pac-Man_Evolution.o

# Information
all:
	@echo "Parameters supported: build64|clean|package"

# Build application tests
build64: $(PROGNAME64)
$(PROGNAME64): MODE=-m64
$(PROGNAME64): $(OBJS)
	@echo "Building '$(PROGNAME64)' application..."
	$(CC) $(CCFLAGS) $(MODE) $ $(OBJS) -o "$(PATHOUT)$(PKGNAME).bin" $(LIBS)

# Clean all files
clean:
	@echo "Cleaning all binary files"
	/bin/rm -f $(PATHOUT)*.bin
	/bin/rm -f $(OBJS)

# Create the distribution package
package:
	@echo "--------------------------------------------------------"
	@echo "Creating customized installation for $(PROGNAME64)"
	@echo "--------------------------------------------------------"
	@echo ""
	@read -p " - Application version (x.y.z): " APPVER; \
	echo "   $$APPVER selected."; \
	echo " - Compressing files..."; \
	mkdir -p /tmp/$(PKGNAME)/licenses; \
	cp ../../History.txt /tmp/$(PKGNAME)/; \
	cp ../../bin/*.cdc /tmp/$(PKGNAME)/; \
	cp ../../bin/$(PKGNAME).bin /tmp/$(PKGNAME)/game.bin; \
	cp ../../licenses/* /tmp/$(PKGNAME)/licenses/; \
	cp ../../../../CRM64Pro/licenses/* /tmp/$(PKGNAME)/licenses/; \
	cp ../../resources/Pac-Man_Evolution_Title_2.png /tmp/$(PKGNAME)/icon.png; \
	touch /tmp/$(PKGNAME)/Pac-Man_Evolution.log; \
	cp Exec /tmp/$(PKGNAME)/icon.bin; \
	chown ubuntu: -R /tmp/$(PKGNAME)/*; \
	chmod 444 /tmp/$(PKGNAME)/licenses/* /tmp/$(PKGNAME)/*; \
	chmod 555 /tmp/$(PKGNAME)/licenses /tmp/$(PKGNAME)/*.bin; \
	chmod 666 /tmp/$(PKGNAME)/*.cdc /tmp/$(PKGNAME)/*.log; \
	(cd /tmp/$(PKGNAME) && tar cf - * | xz) > lib.tar.xz; \
	sudo rm -rf /tmp/$(PKGNAME); \
	echo "   Files compressed."; \
	echo " "; \
	echo " - Creating installer..."; \
	cp installer.template installer; \
	cat lib.tar.xz >> installer; \
	rm lib.tar.xz; \
	chmod +x installer; \
	mv installer ../../Deploys/$(PKGNAME)-$$APPVER-linux-x64.sh; \
	echo "   Installer created."; \
	echo " "; \
	echo "-------------------------------------------------------"

# Compiling of source files
%.o: $(PATHSRC)%.cpp 
	$(CC) $(CCFLAGS) -c $(MODE) $<
	
%.o: $(PATHSRC2)%.cpp 
	$(CC) $(CCFLAGS) -c $(MODE) $<