TARGET ?= margen
INSTALL_DIR ?= $(HOME)/.local/bin
TEST_ARGS ?=
SHELL := /bin/bash

.PHONY: build
clean:
	@rm -rf build

build:
	@mkdir -p build
	gcc -Wall -Wextra -Og -g src/*.c src/*.h -o build/$(TARGET)

.PHONY: install
install: build
	cp -f build/$(TARGET) $(INSTALL_DIR)/$(TARGET)

.PHONY: time
time: build
	time ./build/$(TARGET) $(TEST_ARGS)

.PHONY: valgrind
valgrind: build
	valgrind --leak-check=full -s ./build/$(TARGET) $(TEST_ARGS)

.PHONY: release
release: clean
	aclocal
	autoconf
	automake --add-missing
	./configure
	make distcheck
	mkdir -p build
	cp $(TARGET)-*.tar.gz build
	@rm -rf \
		autom4te.cache \
		aclocal.m4 \
		compile \
		config.* \
		configure \
		depcomp \
		$(TARGET) \
		$(TARGET)-*.tar.gz \
		$(TARGET)-*.pkg.tar.zst \
		install-sh \
		Makefile \
		Makefile.in \
		missing \
		src/.* \
		src/*.o

.PHONY: release
release-clean:
	@rm -rf \
		autom4te.cache \
		aclocal.m4 \
		compile \
		config.* \
		configure \
		depcomp \
		$(TARGET) \
		$(TARGET)-*.tar.gz \
		$(TARGET)-*.pkg.tar.zst \
		install-sh \
		Makefile \
		Makefile.in \
		missing \
		src/.* \
		src/*.o

.PHONY: release-arch
release-arch: clean
	mkdir -p build
	cp PKGBUILD build
	cd build && makepkg

.PHONY: gif
gif: build
	mkdir -p tmp
	for i in {1..50}; do \
		./build/$(TARGET) -w=1920 -h=720 --seed=$$i$$i -o=tmp/image$$i.bmp ; \
	done
	ffmpeg -y -f image2 -framerate 1 -i tmp/image%d.bmp -vf scale=960x360 images/sample.gif
	rm -rf tmp
