# 
#   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.4 2003/04/01 09:08:15 kds Exp $
#
#   Host Controller Interface
#
#   Fixes:	Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
#		

clean:
	@rm -f *~ linux_path

get_linux_path: ../config.in
ifeq (../config.mk, $(wildcard ../config.mk))
	@grep 'LINUX=' < ../config.mk > linux_path; \
	if [ $$? != 0 ] ; then \
		echo "Broken config.mk. Run  make config  again."; \
		exit -1; \
	fi
endif
ifneq (linux_path, $(wildcard linux_path))
	@grep 'LINUX=' < ../config.in > linux_path;
endif
	@source linux_path; \
	echo -n "Linux source directory [$$LINUX]: "; \
	read LINUX_PATH; \
	if [ -z "$$LINUX_PATH" ] ; then \
		LINUX_PATH=$$LINUX; \
	fi; \
	echo LINUX=$$LINUX_PATH > linux_path

apply_patch: get_linux_path
	@source linux_path; \
	if [ ! -f $$LINUX/Makefile ] ; then \
		echo "Cannot find $$LINUX/Makefile"; \
		exit -1; \
	fi; \
	VERSION=`sed -n 's/^VERSION = \([0-9]\+\)$$/\1/p' < $$LINUX/Makefile`; \
	PATCHLEVEL=`sed -n 's/^PATCHLEVEL = \([0-9]\+\)$$/\1/p' < $$LINUX/Makefile`; \
	SUBLEVEL=`sed -n 's/^SUBLEVEL = \([0-9]\+\)$$/\1/p' < $$LINUX/Makefile`; \
	BIN_VERS=$$[ $$VERSION * 100000 + $$PATCHLEVEL * 1000 + $$SUBLEVEL ]; \
	MAX_VERS=0; \
	for patch_file in kpatch-* ; do \
		PBIN_VERS=$$[ `echo $$patch_file|sed -n 's/kpatch-\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)$$/\1 \* 100000 \+ \2 \* 1000 \+ \3/p'` ]; \
		if [ $$PBIN_VERS -le $$BIN_VERS -a $$PBIN_VERS -ge $$MAX_VERS ] ; then \
			MAX_VERS=$$PBIN_VERS; \
			PATCH_FILE=$$patch_file; \
		fi; \
	done; \
	if [ -z "$$PATCH_FILE" ]; then \
		echo "Kernel version is $$VERSION.$$PATCHLEVEL.$$SUBLEVEL. There is no patch file for it."; \
		exit -1; \
	fi; \
	echo "Applying patch for kernel version: $$VERSION.$$PATCHLEVEL.$$SUBLEVEL"; \
	echo "Selected patch file: $$PATCH_FILE"; \
	patch -p1 -d $$LINUX < $$PATCH_FILE; \
	sed -ne 's/^EXTRAVERSION =\(.*\)/EXTRAVERSION =\1-affix/;p' $$LINUX/Makefile > $$LINUX/Makefile.affix; \
	chmod --reference=$$LINUX/Makefile $$LINUX/Makefile.affix; \
	chown --reference=$$LINUX/Makefile $$LINUX/Makefile.affix; \
	mv $$LINUX/Makefile.affix $$LINUX/Makefile

copy_files: get_linux_path
	@source linux_path; \
	echo "Creating directories in $$LINUX"; \
	install -d $$LINUX/net/affix; \
	install -d $$LINUX/drivers/affix; \
	install -d $$LINUX/include/net/affix; \
	\
	echo "Copying files to $$LINUX/net/affix"; \
	cp Makefile.net $$LINUX/net/affix/Makefile; \
	cp Config.in.net $$LINUX/net/affix/Config.in; \
	cp ../btcore/*.[ch] $$LINUX/net/affix; \
	cp ../rfcomm/*.[ch] $$LINUX/net/affix; \
	cp ../pan/*.[ch] $$LINUX/net/affix; \
	\
	echo "Copying files to $$LINUX/drivers/affix"; \
	cp Makefile.drivers $$LINUX/drivers/affix/Makefile; \
	cp Config.in.drivers $$LINUX/drivers/affix/Config.in; \
	cp ../drivers/usb/*.[ch] $$LINUX/drivers/affix; \
	cp ../drivers/uart/*.[ch] $$LINUX/drivers/affix; \
	cp ../drivers/uart_cs/*.[ch] $$LINUX/drivers/affix; \
	cp ../drivers/bluecard/*.[ch] $$LINUX/drivers/affix; \
	cp ../drivers/bt3c/*.[ch] $$LINUX/drivers/affix; \
	cp ../drivers/bt950uart/*.[ch] $$LINUX/drivers/affix; \
	\
	echo "Copying files to $$LINUX/include/net/affix"; \
	cp ../include/*.h $$LINUX/include/net/affix

patch_kernel:	copy_files apply_patch


# NOTES:  Determine LINUX version and apply all patches which has *-$(KVERS)

include ../rules.mk
