##   This file is part of PeerHood.
##
##   PeerHood is free software: you can redistribute it and/or modify
##   it under the terms of the GNU Lesser General Public License 
##   version 2 as published by the Free Software Foundation.
##
##   PeerHood is distributed in the hope that it will be useful,
##   but WITHOUT ANY WARRANTY; without even the implied warranty of
##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##   GNU Lesser General Public License for more details.
##
##   You should have received a copy of the GNU Lesser General Public
##   License along with PeerHood. If not, see <http://www.gnu.org/licenses/>.
##

# top Makefile

include MakeCommonDefs.mk

## Directories to build. Order is important, and should reflect the 
## dependences between modules.
## You can control here which modules are applied into build. 
DIRS += libpeerhood 
DIRS += peerhoodd
DIRS += plugin-local
#DIRS += plugin-gprs
#DIRS += GPRSgw
DIRS += plugin-bt
DIRS += plugin-wlan

ifeq ($(BUILD_ENV), n900)
DIRS += plugin-listener-bluez-bt
DIRS += plugin-listener-maemo-system
DIRS += plugin-listener-maemo-wlan
else ifeq ($(BUILD_ENV), n810)
DIRS += plugin-listener-maemo-bt
DIRS += plugin-listener-maemo-system
DIRS += plugin-listener-maemo-wlan
else
DIRS += plugin-listener-bluez-bt
DIRS += plugin-listener-wlan
endif
 
DIRS += proto-client-new
DIRS += proto-client-roam
DIRS += proto-service2
DIRS += proto-service3
DIRS += proto-service4
DIRS += proto-service-moi
DIRS += ph-testclient-c-wrapper
DIRS += devel-common


##############################################################################
## target dispatching
all: build

help:
	@for i in $(DIRS); do $(MAKE) -s -C $$i $@ || exit 1; done

build:
	for i in $(DIRS); do $(MAKE) -C $$i $@ || exit 1; done

ifeq ($(BUILD_DEB_PACKET), y)
	@echo "BUILD_DEB_PACKET=y is obsolete. Create debian packet with: dpkg-buildpackage -rfakeroot -b -d"
endif

clean:
	for i in $(DIRS); do $(MAKE) -C $$i $@ || exit 1; done

install:
	@install -v -s -m 644 `echo $(resultlibdir)|cut -d/ -f2,3`/$(libproject).so $(libdir)/$(libproject).so.$(version)
	@ln -vfs $(libdir)/$(libproject).so.$(version) $(libdir)/$(libproject).so
	@ln -vfs $(libdir)/$(libproject).so.$(version) $(libdir)/$(libproject).so.0
	# maintain binary compatibility to older ones
	@ln -vfs $(libdir)/$(libproject).so.$(version) $(libdir)/$(libproject).so.0.1.2
	
	@for instlib in `echo $(resultlibdir)|cut -d/ -f2,3|xargs ls|grep $(libproject)-`; \
	do install -v -D -s -m 644 `echo $(resultlibdir)|cut -d/ -f2,3`/$$instlib $(libphdir)/$$instlib ; \
	done
	
	@install -v -s -m 755 `echo $(resultbindir)|cut -d/ -f2,3`/peerhoodd $(bindir)/
	
	@for instincl in `echo $(resultincludedir)|cut -d/ -f2,3,4|xargs ls`; \
	do install -v -D -m 644 `echo $(resultincludedir)|cut -d/ -f2,3,4`/$$instincl $(includedir)/$$instincl ; \
	done
	
	@install -v -m 644 libpeerhood.pc $(libdir)/pkgconfig/libpeerhood.pc
	
	@ldconfig

#	No config to /root/ anymore!
#	@if [ ! -f /root/$(configfile) ] ; then install -v -m 644 $(configfile) /root/$(configfile) ; fi
	@if [ ! -f /etc/default/phconfig ] ; then install -v -m 644 $(configfile) /etc/default/phconfig ; fi
	
uninstall:
	@if [ -f $(libdir)/$(libproject).so.$(version) ] ; then rm -v $(libdir)/$(libproject).so.$(version) ; fi
	@if [ -h $(libdir)/$(libproject).so ] ; then rm -v $(libdir)/$(libproject).so ; fi
	@if [ -d $(libphdir) ] ; then rm -vrf $(libphdir) ; fi
	@if [ -f $(bindir)/peerhoodd ] ; then rm -v $(bindir)/peerhoodd ; fi
	@if [ -d $(includedir) ] ; then rm -vrf $(includedir) ; fi
	
check: 
	for i in $(DIRS); do $(MAKE) -C $$i $@ || exit 1; done

##############################################################################

#ifeq (MakeVars, $(wildcard MakeVars))
#include MakeVars
#else
#config:
#	@echo "Build is not configured, running 'configure' first..."
#	@./configure
#	make
#endif

