PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DATADIR ?= $(PREFIX)/share
DESTDIR ?=
CC ?= cc
CFLAGS ?= -O2 -g
CPPFLAGS ?=
LDFLAGS ?=
VERSION := $(shell cat VERSION)
WARNINGS := -std=c11 -Wall -Wextra -Wpedantic
LIBS := -lGL -lglut -lX11
SOURCES := game_of_life.c $(wildcard *.h *.inc)
TESTS := test_game test_settings test_boards test_app_identity test_circle_input test_paths
DIST_NAME := game-of-life-sandbox-$(VERSION)
DIST_FILES := VERSION Makefile LICENSE README.md RELEASE.md PKGBUILD .SRCINFO \
 game_of_life.c $(wildcard *.h *.inc) build.sh run.sh .gitignore \
 assets/game-of-life-sandbox.desktop assets/game-of-life-sandbox.svg \
 $(wildcard scripts/* tests/*.c docs/*.md) boards/examples/*.life boards/examples/README.md

.PHONY: all portable check install uninstall clean dist package FORCE
all: game-of-life-sandbox

game-of-life-sandbox: $(SOURCES) VERSION Makefile build/install-path
	$(CC) $(CPPFLAGS) -DAPP_PORTABLE=0 -DAPP_VERSION='"$(VERSION)"' \
	 -DAPP_DATA_DIR='"$(DATADIR)/game-of-life-sandbox"' $(CFLAGS) $(WARNINGS) \
	 -pthread game_of_life.c -o $@ $(LDFLAGS) $(LIBS)

portable: game_of_life

game_of_life: $(SOURCES) VERSION Makefile
	$(CC) $(CPPFLAGS) -DAPP_PORTABLE=1 -DAPP_VERSION='"$(VERSION)"' \
	 $(CFLAGS) $(WARNINGS) -pthread game_of_life.c -o $@ $(LDFLAGS) $(LIBS)

build:
	mkdir -p build

# Rebuild if PREFIX/DATADIR changes, including when installing a local build.
build/install-path: FORCE | build
	@printf '%s\n' '$(DATADIR)' > build/install-path.tmp
	@if ! cmp -s build/install-path.tmp $@; then mv build/install-path.tmp $@; else rm build/install-path.tmp; fi

build/test_%: tests/test_%.c $(SOURCES) Makefile | build
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARNINGS) -pthread $< -o $@ $(LDFLAGS) $(LIBS) $(if $(filter test_circle_input,$(@F)),-lXtst,)

check: $(addprefix build/,$(TESTS))
	./build/test_settings
	./build/test_boards
	./build/test_paths
	xvfb-run -a -s '-screen 0 1280x1024x24' ./build/test_game
	xvfb-run -a -s '-screen 0 1280x1024x24' ./build/test_app_identity
	xvfb-run -a -s '-screen 0 1280x1024x24' ./build/test_circle_input
	desktop-file-validate assets/game-of-life-sandbox.desktop

install: all
	install -Dm755 game-of-life-sandbox "$(DESTDIR)$(BINDIR)/game-of-life-sandbox"
	install -Dm644 assets/game-of-life-sandbox.desktop "$(DESTDIR)$(DATADIR)/applications/game-of-life-sandbox.desktop"
	install -Dm644 assets/game-of-life-sandbox.svg "$(DESTDIR)$(DATADIR)/icons/hicolor/scalable/apps/game-of-life-sandbox.svg"
	install -d "$(DESTDIR)$(DATADIR)/game-of-life-sandbox/examples"
	install -m644 boards/examples/*.life boards/examples/README.md "$(DESTDIR)$(DATADIR)/game-of-life-sandbox/examples/"
	install -Dm644 LICENSE "$(DESTDIR)$(DATADIR)/licenses/game-of-life-sandbox/LICENSE"
	install -Dm644 README.md "$(DESTDIR)$(DATADIR)/doc/game-of-life-sandbox/README.md"
	install -Dm644 docs/USAGE.md "$(DESTDIR)$(DATADIR)/doc/game-of-life-sandbox/docs/USAGE.md"

uninstall:
	rm -f "$(DESTDIR)$(BINDIR)/game-of-life-sandbox"
	rm -f "$(DESTDIR)$(DATADIR)/applications/game-of-life-sandbox.desktop"
	rm -f "$(DESTDIR)$(DATADIR)/icons/hicolor/scalable/apps/game-of-life-sandbox.svg"
	rm -rf "$(DESTDIR)$(DATADIR)/game-of-life-sandbox" "$(DESTDIR)$(DATADIR)/licenses/game-of-life-sandbox" "$(DESTDIR)$(DATADIR)/doc/game-of-life-sandbox"

clean:
	rm -f game-of-life-sandbox game_of_life
	rm -rf build

dist:
	mkdir -p dist
	tar --transform='s,^,$(DIST_NAME)/,' -czf dist/$(DIST_NAME).tar.gz $(DIST_FILES)

package:
	./scripts/package-local.sh
