CFLAGS=$(shell pkg-config --cflags polkit-gobject-1 gtk+-3.0 glib-2.0 gio-2.0 libgnome-control-center)
LIBS=$(shell pkg-config --libs polkit-gobject-1 gtk+-3.0 glib-2.0 gio-2.0 libgnome-control-center)
SOURCES=preferences-panel.c whoopsie-generated.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=libwhoopsie.so
LIB=$(DESTDIR)/usr/lib
DATA=$(DESTDIR)/usr/share
UIDIR=$(DATA)/gnome-control-center/ui
DBUS_CONF=$(DESTDIR)/etc/dbus-1/system.d
POLKIT_CONF=$(DESTDIR)/usr/share/polkit-1/actions
SERVICE_CONF=$(DESTDIR)/usr/share/dbus-1/system-services

DAEMON_CFLAGS=$(shell pkg-config --cflags gio-2.0 polkit-gobject-1)
DAEMON_LIBS=$(shell pkg-config --libs gio-2.0 polkit-gobject-1)

.PHONY: all clean

all: $(SOURCES) $(EXECUTABLE) whoopsie-preferences

$(EXECUTABLE): $(OBJECTS)
	gcc -shared -fPIC -Wl,-z -Wl,defs -Wl,-soname -Wl,$@ -o $@ $^ $(LIBS)
clean:
	rm -f $(OBJECTS) $(EXECUTABLE) whoopsie-preferences whoopsie-generated.*
%.o: %.c
	gcc -c $(CFLAGS) -DGNOMECC_UI_DIR=\"$(UIDIR)\" -fPIC -o $@ $^

whoopsie-preferences: whoopsie-preferences.o whoopsie-generated.o
	gcc -o $@ $^ $(DAEMON_LIBS)

whoopsie-preferences.o: whoopsie-preferences.c whoopsie-generated.c
	gcc -c $(CFLAGS) -o $@ $<

whoopsie-generated.c: whoopsie-preferences.xml
	gdbus-codegen --interface-prefix com.ubuntu. \
			--generate-c-code whoopsie-generated \
			$<

install: all
	install -d $(LIB)/control-center-1/panels
	install $(EXECUTABLE) $(LIB)/control-center-1/panels
	install -d $(DATA)/applications
	install -m644 whoopsie.desktop $(DATA)/applications
	install -d $(UIDIR)
	install -m644 whoopsie.ui $(UIDIR)
	install -d $(DBUS_CONF)
	install -m644 com.ubuntu.WhoopsiePreferences.conf $(DBUS_CONF)
	install -d $(POLKIT_CONF)
	install -m644 com.ubuntu.whoopsiepreferences.policy $(POLKIT_CONF)
	install -d $(LIB)/whoopsie
	install whoopsie-preferences $(LIB)/whoopsie
	install -d $(SERVICE_CONF)
	install -m644 com.ubuntu.WhoopsiePreferences.service $(SERVICE_CONF)
