VERSION=2.0
DESTDIR=/
CC=gcc
LIBRARIES=gconf-2.0 hildon-1 hildon-fm-2 hildon-help gtk+-2.0 libosso gdk-2.0 gconf-2.0 gnome-vfs-2.0 gstreamer-0.10 libebook-1.2 libalarm bluez libgwobex libhildondesktop osso-gpsd  osso-addressbook-1.0 gpsbt
#LIBRARIES=gconf-2.0 hildon-1 hildon-fm-2 gtk+-2.0 libosso gdk-2.0 gconf-2.0 libossohelp gnome-vfs-2.0 gstreamer-0.10 osso-addressbook-1.0 libebook-1.2 libalarm bluez libgwobex libhildondesktop osso-gpsd gpsbt 
CFLAGS= -Wall -pedantic `pkg-config --cflags  $(LIBRARIES)`
CDEBUG=-g
# libgstinterfaces-0.10 is defined here to work around
# a bug in gstreamer's pkg-config
LDFLAGS=`pkg-config --libs $(LIBRARIES)` -lgstinterfaces-0.10

ifdef DEBUG
CFLAGS += $(CDEBUG)
LDFLAGS += $(CDEBUG)
endif

EXAMPLES=\
	example_backup_server \
	example_backup_client \
	example_banner \
	example_color_selector \
	example_context \
	example_findtoolbar \
	example_file_chooser \
	example_file_details \
	example_font_selector \
	example_gconf \
	example_hard_keys \
	example_hildonprogram \
	example_libosso \
	example_help_framework \
	example_menu \
	example_message \
	example_quickstart \
	example_toolbar \
	example_gnomevfs \
	example_drawing \
	example_camera \
	example_bluetooth \
	example_alarm \
	example_wavlaunch  \
	example_abook \
	example_gps

LIBEXAMPLES=\
	libapplet.so \
	hildon-sb-helloworld.so 

AUX_FILES=Makefile README README.gconf COPYING \
	  example_backup_client.desktop \
	  example_backup_client.service \
	  example_help_framework.service \
	  example_libosso.desktop \
	  example_libosso.service \
	  install_backup_client.sh \
	  install_controlpanel_applet.sh \
	  install_example_libosso.sh \
	  hildon-sb-helloworld.desktop
	
	

all: $(EXAMPLES) $(LIBEXAMPLES) 

ICON_DIR=$(DESTDIR)`pkg-config osso-af-settings --variable=prefix`/share/icons/hicolor
SB_LIB_DIR=$(DESTDIR)`pkg-config osso-af-settings --variable=hildondesktoplibdir`
SB_DESKTOP_DIR=$(DESTDIR)`pkg-config osso-af-settings --variable=statusbardesktopentrydir`
BIN_DIR=$(DESTDIR)`pkg-config osso-af-settings --variable=prefix`/bin
DESKTOP_DIR=$(DESTDIR)`pkg-config osso-af-settings --variable=desktopentrydir`
DBUS_SERVICE_DIR=$(DESTDIR)`pkg-config osso-af-settings --variable=dbusservicedir`
CP_LIB_DIR=$(DESTDIR)`pkg-config hildon-control-panel --variable=pluginlibdir`
CP_DESKTOP_DIR=$(DESTDIR)`pkg-config hildon-control-panel --variable=plugindesktopentrydir`

INSTALL_FILES=$(addsuffix .c, $(EXAMPLES))\
       		$(patsubst %.so, %.c, $(LIBEXAMPLES))\
       		$(AUX_FILES) \

INSTALL_DIR=$(DESTDIR)/usr/share/doc/maemo-examples
#HELP_INSTALL_DIR=$(DESTDIR)/usr/share/osso-help/$(LANG)
HELP_INSTALL_DIR=$(DESTDIR)/usr/share/osso-help/en_GB

install: install_help
	mkdir -p $(ICON_DIR)/40x40/hildon/
	mkdir -p $(ICON_DIR)/26x26/hildon/
	mkdir -p $(SB_LIB_DIR)
	mkdir -p $(SB_DESKTOP_DIR)
	mkdir -p $(BIN_DIR)
	mkdir -p $(DESKTOP_DIR)
	mkdir -p $(DBUS_SERVICE_DIR)
	mkdir -p $(CP_LIB_DIR)
	mkdir -p $(CP_DESKTOP_DIR)
	cp hello-world.40.png $(ICON_DIR)/40x40/hildon/hello.png
	cp hello-world.26.png $(ICON_DIR)/26x26/hildon/hello.png
	cp hildon-sb-helloworld.so $(SB_LIB_DIR)
	cp hildon-sb-helloworld.desktop $(SB_DESKTOP_DIR)
	cp example_*.desktop $(DESKTOP_DIR)
	cp applet.desktop $(CP_DESKTOP_DIR)
	cp libapplet.so $(CP_LIB_DIR)
	cp $(EXAMPLES) $(BIN_DIR)
	cp *.service $(DBUS_SERVICE_DIR)

.PHONY: install_help
install_help:	example.xml
	mkdir -p $(HELP_INSTALL_DIR)
	cp example.xml $(HELP_INSTALL_DIR)
	

# This rule uses GNU make implicit rules, so
# examples can be built eg make example_hildonprogram
# http://www.gnu.org/software/make/manual/make.html#Implicit-Rules
$(EXAMPLES): %: %.c

$(LIBEXAMPLES): %.so: %.c
	    $(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ $<
	    
# Bluetooth example needs glib-marshallers which are generated here
example_bluetooth_marshallers.c: example_bluetooth.marshallers example_bluetooth_marshallers.h
	glib-genmarshal --body example_bluetooth.marshallers > $@

example_bluetooth_marshallers.h: example_bluetooth.marshallers
	glib-genmarshal --header example_bluetooth.marshallers > $@

example_bluetooth_lowlevel: example_bluetooth_lowlevel.c example_bluetooth_marshallers.c

example_alarm_dbus.h: example_alarm_dbus.xml
	dbus-binding-tool --prefix=hello_alarm --mode=glib-server example_alarm_dbus.xml > $@

example_alarm.c: example_alarm_dbus.h

example_alarm: example_alarm.c

clean:
	-rm -f $(EXAMPLES) $(LIBEXAMPLES)
	-rm -f example_bluetooth_marshallers.*
	-rm -f example_alarm_dbus.h

dist: clean
	cd .. && \
	cp -a maemo_examples maemo-examples-$(VERSION) && \
	tar czf maemo-examples-$(VERSION).tar.gz \
		maemo-examples-$(VERSION) && \
	rm -rf maemo-examples-$(VERSION)
