##   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

DIRS += plugin-listener-bluez-bt

ifeq ($(BUILD_ENV), n900)
DIRS += plugin-listener-maemo-system
DIRS += plugin-listener-maemo-wlan
endif

ifeq ($(BUILD_ENV), n810)
DIRS += plugin-listener-maemo-bt
DIRS += plugin-listener-maemo-system
DIRS += plugin-listener-maemo-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)
	@if [ -d $(debdir) ] ; then rm -rf $(debdir) ; fi
	
	@install -D -s -m 755 `echo $(resultbindir)|cut -d/ -f2,3`/peerhoodd $(debbin)/peerhoodd
	
	@for instlib in `echo $(resultlibdir)|cut -d/ -f2,3|xargs ls|grep $(libproject)-`; \
	do install -D -s -m 644 `echo $(resultlibdir)|cut -d/ -f2,3`/$$instlib $(debphlib)/$$instlib ; \
	done
	
	@install -s -m 644 `echo $(resultlibdir)|cut -d/ -f2,3`/$(libproject).so $(deblib)/$(libproject).so.$(version)
	
	@ln -fs $(libproject).so.$(version) $(deblib)/$(libproject).so
	
	@for instincl in `echo $(resultincludedir)|cut -d/ -f2,3,4|xargs ls`; \
	do install -D -m 644 `echo $(resultincludedir)|cut -d/ -f2,3,4`/$$instincl $(debinclude)/$$instincl ; \
	done
	
	@install -D -m 644 $(configfile) $(debshare)/$(configfile)
	
	@install -D -m 644 $(debmat)/README $(debdoc)/README
	@install -D -m 644 $(debmat)/copyright $(debdoc)/copyright
	@install -D -m 644 $(debmat)/changelog.gz $(debdoc)/changelog.gz
	
	@$(MKDIR) $(debman)
	
	@$(MKDIR) $(debctrl)
	@echo "Source: $(debcontrol_source)" >> $(debctrl)/control
	@echo "Section: $(debcontrol_section)" >> $(debctrl)/control
	@echo "Priority: $(debcontrol_priority)" >> $(debctrl)/control
	@echo "Maintainer: $(debcontrol_maintainer)" >> $(debctrl)/control
	@echo "Package: $(debcontrol_package)" >> $(debctrl)/control
	@echo "Architecture: $(debcontrol_architecture)" >> $(debctrl)/control
	@echo "Version: $(debcontrol_version)" >> $(debctrl)/control
	@echo "Depends: $(debcontrol_depends)" >> $(debctrl)/control
	@echo "Description: $(debcontrol_description)" >> $(debctrl)/control
	
	@cat $(debmat)/$(debcontrol_description_add) >> $(debctrl)/control
	
	@install -D -m 755 $(debmat)/$(debcontrol_postinst) $(debctrl)/postinst
	
	@fakeroot dpkg-deb --build $(debdir) $(debfile)
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
	
	@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
	
	@ldconfig
	
	@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
	

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

