CC ?= gcc
CXX ?= g++
PROGRAM = exampleapp
CXXFILES = main.cc exampleappwindow.cc exampleapplication.cc exampleappprefs.cc
BUILT_SRC = resources.c
OBJS = $(BUILT_SRC:.c=.o) $(CXXFILES:.cc=.o)
DEPDIR = .deps
CXXFLAGS = `pkg-config --cflags gtkmm-4.0` -std=c++17 -Wall -g
CFLAGS = `pkg-config --cflags gtk4`
LIBS = `pkg-config --libs gtkmm-4.0`
GLIB_COMPILE_RESOURCES = $(shell pkg-config --variable=glib_compile_resources gio-2.0)
GLIB_COMPILE_SCHEMAS   = $(shell pkg-config --variable=glib_compile_schemas gio-2.0)

all: $(PROGRAM) gschemas.compiled

-include $(OBJS:%.o=$(DEPDIR)/%.Po)

gschemas.compiled: org.gtkmm.exampleapp.gschema.xml
	$(GLIB_COMPILE_SCHEMAS) --strict --dry-run --schema-file=$<
	$(GLIB_COMPILE_SCHEMAS) .

resources.c: exampleapp.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=. --generate-dependencies exampleapp.gresource.xml)
	$(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=. --generate-source $<

%.o: %.c
	@mkdir -p $(DEPDIR)
	$(CC) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $(CFLAGS) $<
	@mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po

%.o: %.cc
	@mkdir -p $(DEPDIR)
	$(CXX) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $(CXXFLAGS) $<
	@mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po

$(PROGRAM): $(OBJS)
	$(CXX) -o $(PROGRAM) $(OBJS) $(LIBS)

clean:
	rm -f gschemas.compiled
	rm -f $(BUILT_SRC)
	rm -f $(OBJS)
	rm -f $(PROGRAM)

install-desktop-file:
	@mkdir -p ~/.local/share/applications
	sed -e "s#@bindir@#$$PWD#" ../step1/exampleapp.desktop > ~/.local/share/applications/$(PROGRAM).desktop
	@mkdir -p ~/.local/share/icons/hicolor/48x48/apps
	cp ../step1/exampleapp.png ~/.local/share/icons/hicolor/48x48/apps

install-gschema-file:
	if test -n "$(GSETTINGS_SCHEMA_DIR)" -a "$(GSETTINGS_SCHEMA_DIR)" != .; then \
		mkdir -p "$(GSETTINGS_SCHEMA_DIR)"; \
		cp org.gtkmm.exampleapp.gschema.xml "$(GSETTINGS_SCHEMA_DIR)"; \
		$(GLIB_COMPILE_SCHEMAS) "$(GSETTINGS_SCHEMA_DIR)"; \
	fi
