CXX ?= g++
PKG_CONFIG ?= pkg-config
PREFIX ?= /usr/local
BIN_DIR ?= $(PREFIX)/bin
APPLICATIONS_DIR ?= $(PREFIX)/share/applications
SYSTEM_HELPER := $(BIN_DIR)/reforger-kwin-capture
DESKTOP_FILE := org.codex.reforger-kwin-capture.desktop

QT_CFLAGS := $(shell $(PKG_CONFIG) --cflags Qt6Core Qt6DBus)
QT_LIBS := $(shell $(PKG_CONFIG) --libs Qt6Core Qt6DBus)
PORTAL_CFLAGS := $(shell $(PKG_CONFIG) --cflags Qt6Core Qt6DBus Qt6Gui gstreamer-1.0 gstreamer-app-1.0 gstreamer-video-1.0)
PORTAL_LIBS := $(shell $(PKG_CONFIG) --libs Qt6Core Qt6DBus Qt6Gui gstreamer-1.0 gstreamer-app-1.0 gstreamer-video-1.0)
MOC ?= /usr/lib/qt6/moc

.PHONY: all clean install-kwin-auth uninstall-kwin-auth

all: kwin_capture_screen portal_capture_frame

kwin_capture_screen: kwin_capture_screen.cpp kwin_capture_screen.moc
	$(CXX) -std=c++17 -O2 -Wall -Wextra -fPIC $(QT_CFLAGS) $< -o $@ $(QT_LIBS)

kwin_capture_screen.moc: kwin_capture_screen.cpp
	$(MOC) $< -o $@

portal_capture_frame: portal_capture_frame.cpp portal_capture_frame.moc
	$(CXX) -std=c++17 -O2 -Wall -Wextra -fPIC $(PORTAL_CFLAGS) $< -o $@ $(PORTAL_LIBS)

portal_capture_frame.moc: portal_capture_frame.cpp
	$(MOC) $< -o $@

install-kwin-auth: kwin_capture_screen $(DESKTOP_FILE)
	install -Dm755 kwin_capture_screen "$(DESTDIR)$(SYSTEM_HELPER)"
	install -Dm644 $(DESKTOP_FILE) "$(DESTDIR)$(APPLICATIONS_DIR)/$(DESKTOP_FILE)"

uninstall-kwin-auth:
	rm -f "$(DESTDIR)$(SYSTEM_HELPER)" "$(DESTDIR)$(APPLICATIONS_DIR)/$(DESKTOP_FILE)"

clean:
	rm -f kwin_capture_screen kwin_capture_screen.moc portal_capture_frame portal_capture_frame.moc
