#
# Makefile for the Linux Wireless network device drivers.
# 
# Original makefile by Peter Johanson
#
# NOTE: This make file can serve as both an external Makefile (launched
#       directly by the user), or as the sub-dir Makefile used by the kernel
# 	build system.




# If CONFIG_IPW2100 isn't set, we'll assume the user has never configured
# their kernel to include this module and set up some defaults.
#
# NOTE: If you have previously added IPW2100 to your kernel and configured it 
#       for inclusion, these settings will be overridden by your kernel
#       configuration.

ifndef CONFIG_IPW2100
EXTERNAL_BUILD=y
CONFIG_IPW2100=m
CONFIG_IPW2100_AVERATEC_5100P=m
CONFIG_IPW2100_PACKARDBELL_E5=m
CONFIG_IPW2100_DEBUG=y

# If you are interested in using promiscuous mode, simply uncomment this line:
# CONFIG_IPW2100_PROMISC=y

# If you are building externally and you want to use the legacy firmware,
# then uncomment the following line:
# CONFIG_IPW2100_LEGACY_FW_LOAD=y

# If you do not have hostap-driver-0.1.3 installed then you can not use WEP.
# You will need to comment out this line:
# CONFIG_IPW2100_NOWEP=y
endif

# Set up the include path to be able to find hostap_crypt.h
# 
# If CONFIG_HOSTAP is set by the kernel then we use the default kernel
# source location in the kernel.  Otherwise we check for the existence
# of the HOSTAP environment variable.  If set, we base the Host AP
# include directory from that.  If it is not set, we default to looking
# in $(PWD)/hostap-driver-0.1.3
#
ifneq ($(PATCHLEVEL),6) # 2.4
	BASE := $(TOPDIR)
else # 2.6
	BASE := $(srctree)
endif

list-m :=
list-$(CONFIG_IPW2100) += ipw2100
list-$(CONFIG_IPW2100_AVERATEC_5100P) += av5100
list-$(CONFIG_IPW2100_PACKARDBELL_E5) += pbe5

obj-$(CONFIG_IPW2100) += ipw2100.o
obj-$(CONFIG_IPW2100_AVERATEC_5100P) += av5100.o
obj-$(CONFIG_IPW2100_PACKARDBELL_E5) += pbe5.o
ipw2100-objs := \
        ipw2100_main.o \
        ipw2100_fw.o \
        ipw2100_wx.o \
	ieee80211_tx.o \
	ieee80211_rx.o \
	ieee80211.o \
	ieee80211_wx.o

#
# Begin dual Makefile mode here.  First we provide support for when we
# are being invoked by the kernel build system
#
ifneq ($(KERNELRELEASE),)

#
# If you receive a compile message about multiple definitions of
# CONFIG_IPW2100_DEBUG or CONFIG_IPW2100_LEGACY_FW_LOAD, then you have
# likely previously included the IPW2100 into the full kernel build, and 
# these definitions are now being set up by the kernel build system.  
#
# To correct this, remove any CONFIG_IPW2100_* lines from $(KSRC)/.config
#

ifeq ($(EXTERNAL_BUILD),y)
ifdef CONFIG_IPW2100_DEBUG	
EXTRA_CFLAGS += -DCONFIG_IPW2100_DEBUG=$(CONFIG_IPW2100_DEBUG)
endif	
ifdef CONFIG_IPW2100_LEGACY_FW_LOAD
EXTRA_CFLAGS += -DCONFIG_IPW2100_LEGACY_FW_LOAD=$(CONFIG_IPW2100_LEGACY_FW_LOAD)
endif
ifdef CONFIG_IPW2100_NOWEP
EXTRA_CFLAGS += -DCONFIG_IPW2100_NOWEP=$(CONFIG_IPW2100_NOWEP)
endif
endif

ifneq ($(PATCHLEVEL),6) # If we are not on a 2.6, then do 2.4 specific things

O_TARGET := ipw.o

include $(TOPDIR)/Rules.make

ipw2100.o: $(ipw2100-objs)
	$(LD) -r -o $@ $(ipw2100-objs)

endif # End if 2.4 specific settings

else 
# Here we begin the portion that is executed if the user invoked this Makefile
# directly.

# KSRC should be set to the path to your sources
# modules are installed into KMISC
KVER  := $(shell uname -r)
KSRC := /lib/modules/$(KVER)/build
KMISC := /lib/modules/$(KVER)/kernel/drivers/net/wireless/ipw2100/

# KSRC_OUTPUT should be overridden if you are using a 2.6 kernel that
# has it's output sent elsewhere via KBUILD_OUTPUT= or O=
KSRC_OUTPUT := $(KSRC)

# If we find Rules.make, we can assume we're using the old 2.4 style building
OLDMAKE=$(wildcard $(KSRC)/Rules.make)
PWD=$(shell pwd)

VERFILE := $(KSRC_OUTPUT)/include/linux/version.h
KERNELRELEASE := $(shell \
	if [ -r $(VERFILE) ]; then \
		(cat $(VERFILE); echo UTS_RELEASE) | \
		$(CC) -I$(KSRC_OUTPUT) $(CFLAGS) -E - | \
		tail -n 1 | \
		xargs echo; \
        else \
		uname -r; \
	fi)

MODPATH := $(DESTDIR)/lib/modules/$(KERNELRELEASE)

all: modules

.PHONY : hostap_check
ifeq ($(CONFIG_IPW2100_NOWEP),y)
hostap_check :

else

hostap_check :
	@if [ ! -e "$(HOSTAP_SRC)/hostap_crypt.h" ]; then \
		echo ""; \
		echo "The build failed to find the Host AP files in:"; \
		echo ""; \
		echo "'$(HOSTAP_SRC)'"; \
		echo ""; \
		echo -n "Prior to building IPW2100 you need to download and "; \
		echo "install the "; \
		echo "hostap-driver-0.1.3 package available from "; \
		echo ""; \
		echo "    http://hostap.epitest.fi."; \
		echo ""; \
		echo -n "Once you have it installed on your system, you can "; \
		echo "invoke the build via:"; \
		echo ""; \
		echo -n "  % make [KSRC=/path/to/kernel] "; \
		echo "HOSTAP=/path/to/hostap-driver-0.1.3"; \
		echo ""; \
		exit 1; \
	fi
endif

clean:
	rm -f *.mod.c *.mod *.o *.ko .*.cmd .*.flags
	rm -rf $(PWD)/tmp

ifeq ($(OLDMAKE),)

TMP=$(PWD)/tmp
MODVERDIR=$(TMP)/.tmp_versions

modules: hostap_check
ifdef ($(KSRC_OUTPUT)/.tmp_versions)
	mkdir -p $(MODVERDIR)
	-cp $(KSRC_OUTPUT)/.tmp_versions/*.mod $(MODVERDIR)
endif
ifeq ($(KSRC),$(KSRC_OUTPUT)) # We're not outputting elsewhere
ifdef ($(KSRC)/.tmp_versions)
	-cp $(KSRC)/.tmp_versions/*.mod $(MODVERDIR)
endif
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) MODVERDIR=$(PWD) modules
else # We've got a kernel with seperate output, copy the config, and use O=
	mkdir -p $(TMP)
	cp $(KSRC_OUTPUT)/.config $(TMP)
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) MODVERDIR=$(PWD) O=$(PWD)/tmp modules
endif

install: modules
	install -d $(KMISC)
	install -m 644 -c $(addsuffix .ko,$(list-m)) $(KMISC)
	/sbin/depmod -a
ifdef CONFIG_IPW2100_LEGACY_FW_LOAD
	@echo "Don't forget to copy firmware to /etc/firmware/."
else
	@echo "Don't forget to copy firmware to /usr/lib/hotplug/firmware/ and have the "
	@echo "hotplug tools in place."
endif
	@echo "See INSTALL for more information."


else # We're on 2.4, and things are slightly different

modules: hostap_check
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) BUILD_DIR=$(PWD) \
		HOSTAP_SRC=$(HOSTAP_SRC) modules

install: modules
	install -d $(KMISC)
	install -m 644 -c $(addsuffix .o,$(list-m)) $(KMISC)
	/sbin/depmod -a
ifdef CONFIG_IPW2100_LEGACY_FW_LOAD
	@echo "Don't forget to copy firmware to /etc/firmware/."
else
	@echo "Don't forget to copy firmware to /usr/lib/hotplug/firmware/ "
	@echo "and have the hotplug tools in place."
endif
	@echo "See INSTALL for more information."

endif

uninstall:
	rm -rf $(KMISC)
	/sbin/depmod -a

endif
