# Makefile for c-evo-dh

# Set temporary HOME for lazarus primary config directory
HOME=tmphome

B_MODE   ?=Release
prefix   ?= usr

# Debian uses libexec & games directories. Arch does not.
BinDir   ?= bin
LibDir   ?= lib
ShareDir ?= share/c-evo-dh
ExecDir  ?= lib

FPC-VERSION = $(shell fpc -iV)

# Cleaner build log if jobs are spearate
.NOTPARALLEL:


all: stdai c-evo-dh-gtk2

stdai: AI/StdAI/*.*
	cp -p Pascal/$(B_MODE).cfg AI/StdAI/fpc.cfg

	# Build AI module (Peephole broken in fpc <= 3.2.2)
	if [ $(FPC-VERSION) \> 3.2.2 ]; then \
		cd AI/StdAI && fpc -d$(B_MODE) -Xc -B -O3  -k-znoexecstack -olibstdai.so StdAI.lpr; \
	else \
		cd AI/StdAI && fpc -d$(B_MODE) -Xc -B -O3 -OoNOPEEPHOLE -k-znoexecstack -olibstdai.so StdAI.lpr; \
	fi

c-evo-dh-gtk2: init-build
	lazbuild --bm=$(B_MODE) -B --ws=gtk2 $(LAZDIR) --no-write-project Integrated.lpi
	mv c-evo c-evo-dh-gtk2

c-evo-dh-gtk3: init-build
	lazbuild --bm=$(B_MODE) -B --ws=gtk3 $(LAZDIR) --no-write-project Integrated.lpi
	mv c-evo c-evo-dh-gtk3

c-evo-dh-qt5: init-build
	lazbuild --bm=$(B_MODE) -B --ws=qt5  $(LAZDIR) --no-write-project Integrated.lpi
	mv c-evo c-evo-dh-qt5

c-evo-dh-qt6: init-build
	lazbuild --bm=$(B_MODE) -B --ws=qt6  $(LAZDIR) --no-write-project Integrated.lpi
	mv c-evo c-evo-dh-qt6

init-build:
	cp -p Pascal/$(B_MODE).cfg fpc.cfg
	cp -p Pascal/$(B_MODE).cfg Packages/CevoComponents/fpc.cfg
	cp -p Pascal/$(B_MODE).cfg Packages/Common/fpc.cfg


install:
	install -p -Dm 644 AI/StdAI/StdAI.ai.txt    -t "$(DESTDIR)$(prefix)/$(ShareDir)/AI/StdAI"
	install -p -Dm 644 AI/StdAI/StdAI.png       -t "$(DESTDIR)$(prefix)/$(ShareDir)/AI/StdAI"
	install -p -Dm 755 AI/StdAI/libstdai.so     -t "$(DESTDIR)$(prefix)/$(LibDir)/c-evo-dh"
	ln -s /$(prefix)/$(LibDir)/c-evo-dh/libstdai.so  $(DESTDIR)$(prefix)/$(ShareDir)/AI/StdAI/libstdai.so

	install -p -Dm 644 Graphics/*.png           -t "$(DESTDIR)$(prefix)/$(ShareDir)/Graphics"
	install -p -Dm 644 Graphics/*.jpg           -t "$(DESTDIR)$(prefix)/$(ShareDir)/Graphics"
	install -p -Dm 644 Graphics/*.grs           -t "$(DESTDIR)$(prefix)/$(ShareDir)/Graphics"
	install -p -Dm 644 Language.txt             -t "$(DESTDIR)$(prefix)/$(ShareDir)"
	install -p -Dm 644 Language2.txt            -t "$(DESTDIR)$(prefix)/$(ShareDir)"
	install -p -Dm 644 Fonts.txt                -t "$(DESTDIR)$(prefix)/$(ShareDir)"
	install -p -Dm 644 Linux/c-evo-dh.xml       -t "$(DESTDIR)$(prefix)/share/mime/packages"
	install -p -Dm 644 Desktop/c-evo-dh.svg     -t "$(DESTDIR)$(prefix)/share/icons/hicolor/scalable/apps"
	install -p -Dm 644 Help/*                   -t "$(DESTDIR)$(prefix)/$(ShareDir)/Help"
	install -p -Dm 644 Maps/*                   -t "$(DESTDIR)$(prefix)/$(ShareDir)/Maps"
	install -p -Dm 644 Sounds/*                 -t "$(DESTDIR)$(prefix)/$(ShareDir)/Sounds"
	install -p -Dm 644 Tribes/*                 -t "$(DESTDIR)$(prefix)/$(ShareDir)/Tribes"
	install -p -Dm 644 Docs/*                   -t "$(DESTDIR)$(prefix)/share/doc/c-evo-dh-data"
	find Localization -type f -exec install -Dm 644    "{}" "$(DESTDIR)$(prefix)/$(ShareDir)/{}" \;

	install -p -Dm 755 c-evo-dh-gtk2                        -t "$(DESTDIR)$(prefix)/$(ExecDir)/c-evo-dh"
	install -p -Dm 755 Linux/c-evo-launch-gtk2              -t "$(DESTDIR)$(prefix)/$(BinDir)"
	install -p -Dm 644 Linux/c-evo-dh-gtk2.metainfo.xml     -t "$(DESTDIR)$(prefix)/share/metainfo"
	install -p -Dm 644 Desktop/c-evo-dh-gtk2.desktop        -t "$(DESTDIR)$(prefix)/share/applications"
	install -p -Dm 644 Desktop/c-evo-manual-gtk2.desktop    -t "$(DESTDIR)$(prefix)/share/applications"
	install -p -Dm 644 Man/c-evo-launch-gtk2.6              -t "$(DESTDIR)$(prefix)/share/man/man6"

clean:
	rm -fr lib
	rm -fr tmphome
	rm -fr Packages/CevoComponents/lib
	rm -fr Packages/Common/lib
	rm -fr AI/StdAI/lib/

	rm -f *.o
	rm -f *.ppu
	rm -f *.res
	rm -f *.trc
	rm -f *.cfg

	rm -f AI/StdAI/*.so
	rm -f AI/StdAI/*.o
	rm -f AI/StdAI/*.ppu
	rm -f AI/StdAI/*.res
	rm -f AI/StdAI/*.s
	rm -f AI/StdAI/*.cfg

	rm -f Packages/CevoComponents/*.cfg
	rm -f Packages/CevoComponents/CevoComponents.pas
	rm -f Packages/Common/*.cfg
	rm -f c-evo-dh-gtk2
	rm -f c-evo-dh-gtk2.dbg
	rm -f c-evo-dh-gtk3
	rm -f c-evo-dh-gtk3.dbg
	rm -f c-evo-dh-qt5
	rm -f c-evo-dh-qt5.dbg
	rm -f c-evo-dh-fpgui
	rm -f c-evo-dh-fpgui.dbg
