VALAC = valac -g
PKGS = --pkg gtk+-3.0 --pkg cairo --pkg gee-0.8 --pkg json-glib-1.0 --pkg gmodule-2.0

PREFIX ?= /usr/local
BINDIR = $(DESTDIR)$(PREFIX)/bin
LIBDIR = $(DESTDIR)$(PREFIX)/lib/tech-hud-artwork
DATADIR = $(DESTDIR)$(PREFIX)/share

all: dirs interface plugins app

dirs:
	mkdir -p build plugins

interface: plugins/plugin_interface.vala
	$(VALAC) --library=plugin_interface -H plugins/plugin_interface.h $(PKGS) -X -fPIC -X -shared plugins/plugin_interface.vala -o plugins/libplugin_interface.so

plugins: interface plugins/tetris_crawlers.vala
	for f in plugins/*.vala; do \
		if [ "$$f" != "plugins/plugin_interface.vala" ]; then \
			soname=$$(basename "$$f" .vala); \
			$(VALAC) --library=$$soname $(PKGS) --vapidir=plugins --pkg plugin_interface \
				-X -fPIC -X -shared -X -Iplugins -X -Lplugins -X -lplugin_interface \
				-X -Wl,-rpath,'$$ORIGIN' -o plugins/lib$$soname.so "$$f"; \
		fi \
	done

app: interface plugins
	$(VALAC) $(PKGS) --vapidir=plugins --pkg plugin_interface \
		-X -Iplugins -X -Lplugins -X -lplugin_interface \
		-X -lm \
		-X -Wl,-rpath,'$$ORIGIN/plugins' -X -rdynamic \
		-o tech-hud-artwork src/main.vala

install: all
	install -d $(BINDIR)
	install -d $(LIBDIR)/plugins
	install -d $(DATADIR)/applications
	install -d $(DATADIR)/doc/tech-hud-artwork
	
	install -m 755 tech-hud-artwork $(BINDIR)/tech-hud-artwork
	install -m 755 plugins/*.so $(LIBDIR)/plugins/
	install -m 644 tech-hud-artwork.desktop $(DATADIR)/applications/
	install -m 644 README.md $(DATADIR)/doc/tech-hud-artwork/

uninstall:
	rm -f $(BINDIR)/tech-hud-artwork
	rm -rf $(LIBDIR)
	rm -f $(DATADIR)/applications/tech-hud-artwork.desktop
	rm -rf $(DATADIR)/doc/tech-hud-artwork

deb: all
	rm -rf deb-build
	mkdir -p deb-build/DEBIAN
	mkdir -p deb-build$(PREFIX)/bin
	mkdir -p deb-build$(PREFIX)/lib/tech-hud-artwork/plugins
	mkdir -p deb-build$(PREFIX)/share/applications
	mkdir -p deb-build$(PREFIX)/share/doc/tech-hud-artwork

	install -m 755 tech-hud-artwork deb-build$(PREFIX)/bin/
	install -m 755 plugins/*.so deb-build$(PREFIX)/lib/tech-hud-artwork/plugins/
	install -m 644 tech-hud-artwork.desktop deb-build$(PREFIX)/share/applications/
	install -m 644 README.md deb-build$(PREFIX)/share/doc/tech-hud-artwork/

	echo "Package: tech-hud-artwork" > deb-build/DEBIAN/control
	echo "Version: 1.0.0" >> deb-build/DEBIAN/control
	echo "Section: graphics" >> deb-build/DEBIAN/control
	echo "Priority: optional" >> deb-build/DEBIAN/control
	echo "Architecture: amd64" >> deb-build/DEBIAN/control
	echo "Depends: libgtk-3-0, libcairo2, libgee-0.8-2, libjson-glib-1.0-0, libgmodule-2.0-0" >> deb-build/DEBIAN/control
	echo "Maintainer: Mark <mark@nipogi>" >> deb-build/DEBIAN/control
	echo "Description: Tech-HUD Dynamic Artwork Generator with GModule Plugins" >> deb-build/DEBIAN/control

	dpkg-deb --build deb-build tech-hud-artwork_1.0.0_amd64.deb
	rm -rf deb-build
	@echo "Debian package successfully created: tech-hud-artwork_1.0.0_amd64.deb"

source:
	rm -f tech-hud-artwork_1.0.0.orig.tar.gz
	tar -czvf tech-hud-artwork_1.0.0.orig.tar.gz \
		--exclude='build' \
		--exclude='deb-build' \
		--exclude='*.deb' \
		--exclude='tech-hud-artwork' \
		--exclude='plugins/*.so' \
		--exclude='plugins/*.h' \
		--exclude='plugins/*.c' \
		src/ plugins/ Makefile README.md tech-hud-artwork.desktop
	@echo "Source tarball successfully created: tech-hud-artwork_1.0.0.orig.tar.gz"

clean:
	rm -rf build tech-hud-artwork plugins/*.so plugins/*.h plugins/*.c *.deb *.tar.gz