#
#   Affix - Bluetooth Protocol Stack for Linux
#   Copyright (C) 2001 Nokia Corporation
#   Original Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
#
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by the
#   Free Software Foundation; either version 2 of the License, or (at your
#   option) any later version.
#
#   This program 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
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

# 
#   $Id: Makefile,v 1.27 2003/11/17 10:21:24 kds Exp $
#
#   Makefile for compiling and installing the Affix stack
#
#   Fixes:	Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
#		Imre Deak <ext-imre.deak@nokia.com>
# 

ifndef src
    src = .
endif

-include $(src)/config.mk

ALL = etc include btcore rfcomm pan kpatch \
	drivers/usb drivers/uart drivers/uart_cs \
	drivers/bt3c drivers/bluecard drivers/bt950uart

DIRS = etc/ include/ btcore/

ifdef CONFIG_AFFIX_RFCOMM_MODULE
DIRS += rfcomm/
endif

ifdef CONFIG_AFFIX_PAN_MODULE
DIRS += pan/
endif

ifdef CONFIG_AFFIX_UART_MODULE
DIRS += drivers/uart/
endif

ifdef CONFIG_AFFIX_UART_CS_MODULE
DIRS += drivers/uart_cs/
endif

ifdef CONFIG_AFFIX_USB_MODULE
DIRS += drivers/usb/
endif

ifdef CONFIG_AFFIX_BLUECARD_CS_MODULE
DIRS += drivers/bluecard/
endif

ifdef CONFIG_AFFIX_BT3C_CS_MODULE
DIRS += drivers/bt3c/
endif

ifdef CONFIG_AFFIX_BT950UART_CS_MODULE
DIRS += drivers/bt950uart/
endif

obj-m += $(DIRS)

.PHONY:	.prereq.ok

all:	.prereq.ok
	@$(MAKE) build

build:
	@echo "Compiling driver and control programs";
	@mkdir -p bin;
	@set -e; for d in $(DIRS); do $(MAKE) -C $$d all; done; 

config:
	@touch config.mk
	@if [ -e config.out ] ; then mv -f config.out config.out.x ; fi
	@$(MAKE) -s -f Makefile distclean
	@if [ -e config.out.x ] ; then mv -f config.out.x config.out ; fi
	@./Configure

.prereq.ok:
	@if [ ! -f .prereq.ok ] ; then \
		echo "Run 'make config' first."; \
		$(MAKE) -s -f Makefile config; \
	else \
		. ./config.out ; \
		echo "Checking kernel checksum ..."; \
		if [ "$$CHECK" != "" ] ; then \
		    if [ "`cksum < $$CHECK`" != "$$CKSUM" ] ; then \
			echo -n "Kernel configuration has changed." ; \
			echo "Please re-run 'make config'." ; \
			$(MAKE) -s -f Makefile config; \
		    fi ; \
		fi ; \
	fi	

clean:
	@echo "Cleaning programs"
	@touch config.mk
	@set -e; for d in $(ALL); do $(MAKE) -C $$d $@; done
	@rm -f core core.* *.o .*.o *.s *.a *.e .depend tmp_make *~
	@rm -rf bin
ifeq (debian, $(wildcard debian))
	@set -e; $(MAKE) -f debian/rules clean_debian
endif

distclean:	clean
	@rm -f config.out
	@rm -f .prereq.ok config.mk include/config.h 

uninstallold:
	@echo "Removing old files from the system"
ifeq (, $(DESTDIR))
	# PCMCIA
	rm -f /etc/pcmcia/affix_uart.conf
	rm -f /etc/pcmcia/bt3c.conf /etc/pcmcia/bluecard.conf
	# MODULES
	rm -f $(MODDIR)/net/affix*
	rm -f $(MODDIR)/pcmcia/affix*
	rm -f $(MODDIR)/kernel/net/affix_rfcomm.o 
	rm -f $(MODDIR)/kernel/drivers/usb/affix_usb.o
	rm -f $(MODDIR)/kernel/drivers/bluetooth/affix*
	rm -f $(MODDIR)/kernel/net/bluetooth/affix*
	#
	rm -rf /usr/include/affix/types.h /usr/include/affix/debug.h
endif
	

install: all uninstallold
	@echo "Installing driver and control programs"
	@set -e; for d in $(DIRS); do $(MAKE) -C $$d install ; done
ifeq (, $(DESTDIR))
	@depmod || true
endif
	@echo "Installation complete"

install-headers:
	$(MAKE) -C include install

uninstall:
	@echo "UnInstalling..."
	@set -e; for d in $(DIRS); do $(MAKE) -C $$d uninstall ; done


patch_kernel:
	@set -e; $(MAKE) -C kpatch patch_kernel

copy_files:
	@set -e; $(MAKE) -C kpatch copy_files

apply_patch:
	@set -e; $(MAKE) -C kpatch apply_patch

count:
	@find ./ -name "*.[ch]*" -exec cat {} \; | wc -l


debapp:	all
	debian/rules binary-app

debmod:	all
	debian/rules KSRC=$(LINUX) KVERS=$(UTS_RELEASE) binary-modules


dist:
	@PACKAGE_VERSION=`sed -ne "/^AFFIX_VERSION=/s///p" config.in` ;\
	PACKAGE_NAME="affix-kernel-$$PACKAGE_VERSION" ;\
	rm -rf $$PACKAGE_NAME $$PACKAGE_NAME.tar.gz ;\
	find -path "*include/linux" -prune -o -path "*include/affix" -prune \
	-o -name "config.out" -o -name "config.mk" -o -path "*include/config.h" \
	-o -name "*.gz"	-o -name ".*" -o -name "*.o" \
	-o -name "*.d" -o -name "*.s" -o -name "*~" \
	-o -path "*/CVS" -prune -o -path "*/bin" -prune \
	-o -name "packet.*" -o -name cscope.out -o -name TODO.int \
	-o -path ".depfiles" -prune -o -print > .affix-cpio.list ; \
	cpio -p --make-directories $$PACKAGE_NAME <.affix-cpio.list ;\
	rm -f .affix-cpio.list ;\
	tar cvfz $$PACKAGE_NAME.tar.gz $$PACKAGE_NAME ;\
	rm -rf $$PACKAGE_NAME
	
	
help:
	@echo "Pick one of the following targets:"
	@echo -e "\tmake config\t\t- configure and check system setup"
	@echo -e "\tmake all\t\t- build modules and programs"
	@echo -e "\tmake install\t\t- install everything"
	@echo -e "\tmake install-headers\t\t- install header filess"
	@echo -e "\tmake clean\t\t- remove old binaries and dependency files"
	@echo -e "\tmake distclean\t\t- cleaning everything..."
	@echo -e "\tmake debmod\t\t- create Debian package w/ kernel modules"
	@echo -e "\tmake debapp\t\t- create Debain package w/ control applications\n               (btctl, btsrv, sdp, obex)"
	@echo -e "\tmake patch_kernel\t\t- patch the kernel"
	@echo -e ""

