# $Id: Makefile,v 1.33 2005/04/15 22:34:32 sagamore Exp $
#
# Copyright (C) 2004 Zydas Inc.
# Copyright (C) 2005 Arno WILLIG <akw@users.sourceforge.net>
# Copyright (C) 2005 Dimitriy KOROVKIN <korovkin@users.sourceforge.net>
# Copyright (C) 2005 Todor T. ZVISKOV <warderx@users.sourceforge.net>
# Copyright (C) 2005 Markus KARG <markus-karg@users.sourceforge.net>
# Copyright (C) 2005 Roman Kurakin <sagamore@users.sourceforge.net>
#
# This file is part of the ZD1211 Wireless USB Driver for Linux.
#
# This driver 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 driver 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 driver; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Some commands
AWK=awk
CAT=cat
CC=gcc
CPP=g++
CVS=cvs
DATE=date
GREP=grep
GZIP=gzip
LD=ld
LN=ln
MV=mv
RM=rm -f -r
SED=sed
TAR=tar

PWD := $(shell pwd)

# When KSRC is set, use it
KSRC ?= /usr/src/linux

# Retrieve kernel version from kernel in KSRC
#KERNRELEASE := $(shell uname -r;)
KERN_VERSION := \
	$(shell $(GREP) "^VERSION[[:space:]]*=[[:space:]]*[[:digit:]]\+$$" \
	$(KSRC)/Makefile | \
	$(SED) "s/^.*=[[:space:]]*\([[:digit:]]\+\)$$/\1/g";)
KERN_PATCHLEVEL := \
	$(shell $(GREP) "^PATCHLEVEL[[:space:]]*=[[:space:]]*[[:digit:]]\+$$" \
	$(KSRC)/Makefile | \
	$(SED) "s/^.*=[[:space:]]*\([[:digit:]]\+\)$$/\1/g";)
KERN_SUBLEVEL := \
	$(shell $(GREP) "^SUBLEVEL[[:space:]]*=[[:space:]]*[[:digit:]]\+$$" \
	$(KSRC)/Makefile | \
	$(SED) "s/^.*=[[:space:]]*\([[:digit:]]\+\)$$/\1/g";)
KERN_EXTRAVERSION := \
	$(shell $(GREP) "^EXTRAVERSION[[:space:]]*=[[:space:]]*[^[:space:]]\+$$" \
	$(KSRC)/Makefile | \
	$(SED) "s/^.*=[[:space:]]*\([^[:space:]]\+\)$$/\1/g";)
KERN_LOCALVERSION := \
	$(shell $(GREP) "^CONFIG_LOCALVERSION=\"[^[:space:]]\+\"$$" \
	$(KSRC)/.config | \
	$(SED) "s/^.*=\"\([^[:space:]]\+\)\"$$/\1/g";)

KERNRELEASE := \
	$(KERN_VERSION).$(KERN_PATCHLEVEL).$(KERN_SUBLEVEL)$(KERN_EXTRAVERSION)$(KERN_LOCALVERSION)

SRC_DIR=$(PWD)/src

KVER := $(shell echo $(KERNRELEASE)|cut -d "." -f1-2;)
MODPATH := /lib/modules/$(KERNRELEASE)

# Retrieving driver version
ZD_VERSION := $(shell $(DATE) +%Y%m%d)

ifeq ($(KVER), 2.6)
MODULE = zd1211_mod.ko
else
MODULE = zd1211_mod.o
endif

SUBDIRS = $(PWD)/src/modules-$(KERNRELEASE)

all:	$(SRC_DIR)/zddevlist.h $(SUBDIRS)
ifeq ($(KVER), 2.6) # we're on a 2.6.x kernel
	$(MAKE) V=0 -C $(KSRC) SUBDIRS=$(SUBDIRS) modules
else
	$(MAKE) -C $(KSRC) SUBDIRS=$(SUBDIRS) depend modules
endif

$(SRC_DIR)/zddevlist.h:	$(SRC_DIR)/zddevlist
	$(CAT) $< | $(AWK) -f $(SRC_DIR)/zddevlist.awk > $@

$(SUBDIRS):
	mkdir -p $@
	(cd $@; \
	  $(LN) -sf $(SRC_DIR)/Makefile; \
	  for i in $(SRC_DIR)/*.c $(SRC_DIR)/*.h; \
	  do \
	  $(LN) -sf $${i}; \
	  done; \
	)
	  
install: all
	mkdir -p $(MODPATH)/net
#	mkdir -p /etc/zd1211
	cp $(SUBDIRS)/$(MODULE) $(MODPATH)/net
	depmod -a

# for apdbg
	gcc -o apdbg tools/apdbg.c
	chmod +x apdbg
	cp ./apdbg /usr/local/sbin/apdbg   

clean:
	@$(RM) .tmp_versions .*.cmd *.ko *.mod.c *.mod.o *.o $(SRC_DIR)/*.o  $(SRC_DIR)/.*.o.cmd $(SRC_DIR)/*~ $(SRC_DIR)/zddevlist.h
	$(MAKE) -C $(SUBDIRS) clean

update:
	@$(CVS) update -dP

release: clean
	@echo Creating release v$(ZD_VERSION)..;\
	$(SED) -e 's/".*"/"$(ZD_VERSION)"/' src/zdversion.h > src/zdversion2.h;\
	$(MV) src/zdversion2.h src/zdversion.h;\
	cd ..;$(TAR) --exclude 'CVS' -c --owner=0 --group=0 zd1211 > sf_zd1211_$(ZD_VERSION)_src.tar;\
	$(GZIP) -f -9 sf_zd1211_$(ZD_VERSION)_src.tar
