VERSION = 0.15rc2
EXTRAVERSION = STA

# If you have PCMCIA support in the kernel, leave PCMCIA_CS blank, ie
# PCMCIA_CS =
# If on the other hand you are using pcmcia-cs modules to drive your
# card, set PCMCIA_CS to the name of the directory where the pcmcia-cs
# source code is (where the INSTALL, README, SUPPORTED.CARDS files are)
# e.g.
# PCMCIA_CS = /usr/src/modules/pcmcia-cs
PCMCIA_CS =

# The default kernel is the one you are running, but you may want to set
# it to another configured Linux source tree
KERNEL_SRC = $(shell readlink -f /lib/modules/`uname -r`/build)

# Output directory that you used when building Linux kernel, if any.
# Note: this driver will be compiled to the current directory regardless.
O = $(KERNEL_SRC)

# You don't need to change anything below this line in most cases

CONF_DIR = /etc/pcmcia
                                                                               
ORINOCODIR = $(shell pwd)
KERNEL_VERSION = $(shell sed -ne 's/"//g;s/^\#define UTS_RELEASE //p' \
		   $(O)/include/linux/version.h)

MODULE_DIR_TOP = /lib/modules/$(KERNEL_VERSION)
MODULE_DIR_PCMCIA = $(MODULE_DIR_TOP)/pcmcia
MODULE_DIR_WIRELESS = $(MODULE_DIR_TOP)/kernel/drivers/net/wireless

DOT_CONFIG = $(wildcard $(O)/.config)
ifeq (,$(DOT_CONFIG))
$(error The kernel source is not configured)
endif
include $(DOT_CONFIG)

ifndef CONFIG_NET_RADIO
$(error Wireless extensions are not enabled)
endif

# If kernel version starts with 2.4, use old style build system.
OLDMAKE = $(filter 2.4%, $(KERNEL_VERSION))

ifdef PCMCIA_CS
ifeq (,$(wildcard $(PCMCIA_CS)/include))
$(error The pcmcia-cs source is missing or incomplete)
endif
ifeq (,$(OLDMAKE))
$(error Can't cope with 2.6 and pcmcia-cs together)
endif
CONFIG_PCMCIA = m
else
ifneq (,$(wildcard $(MODULE_DIR_PCMCIA)/orinoco_cs.o))
$(error PCMCIA_CS must be defined to overwrite old drivers)
endif
endif

MODULES = hermes.o orinoco.o

ifdef CONFIG_PPC_PMAC
ifeq (,$(OLDMAKE))
MODULES += airport.o
endif
endif

ifdef CONFIG_PCI
MODULES += orinoco_nortel.o orinoco_pci.o orinoco_plx.o orinoco_tmd.o
endif

# Replacement for firmware loader for 2.4 kernels that don't have it
ifneq (,$(OLDMAKE))
ifdef CONFIG_HOTPLUG
ifndef CONFIG_FW_LOADER
MODULES += firmware_class.o
CONFIG_FW_LOADER = m
endif
endif
endif

ifdef CONFIG_PCMCIA
MODULES += orinoco_cs.o
ifdef CONFIG_FW_LOADER
MODULES += spectrum_cs.o
endif
endif

SRCS = airport.c firmware_class.c hermes.c orinoco.c orinoco_cs.c \
	orinoco_nortel.c orinoco_pci.c orinoco_plx.c orinoco_tmd.c \
	spectrum_cs.c dump_recs.c userhermes.c
HDRS = firmware.h hermes.h hermes_rid.h ieee802_11.h kcompat.h orinoco.h
CONF = hermes.conf
TAR = tar
DEPMOD = /sbin/depmod

CVSTAG = orinoco_$(shell echo $(VERSION) | sed 's/\./_/g')
DISTFILES = $(SRCS) $(HDRS) $(CONF) Makefile README.orinoco
TOPDISTFILES = README.orinoco $(CONF)
DISTNAME = orinoco-$(VERSION)
DISTDIR = ../dist
PUSH_WWW = push-www.sh
UCC = gcc
UCFLAGS = -g -O2 -Wall

all: modules

installconf:
ifdef CONFIG_PCMCIA
	mkdir -p $(CONF_DIR)
	install -m 644 -o 0 -g 0 $(CONF) $(CONF_DIR)/$(CONF)
endif

dump_recs: dump_recs.c
	$(UCC) $(UCFLAGS) -o $@ $<

userhermes: userhermes.c
	$(UCC) $(UCFLAGS) -lhistory -lreadline -lncurses -o $@ $<

TAGS:
	rm -f TAGS
	find $(KERNEL_SRC)/ -name '*.[ch]' | xargs etags --append
	etags --append $(SRCS) $(HDRS)

clean:
	rm -f core *.o *~ .#* a.out *.d
	rm -f *.s *.i dump_recs userhermes
	rm -f *.ko *.mod.c *.mod .*.cmd
	rm -f $(MODVERDIR)/*.mod
	rmdir $(MODVERDIR) || true

.PHONY: TAGS all clean dist installconf modules newrelease push

releasecheck: all clean
	./releasecheck.sh $(VERSION) $(EXTRAVERSION)

newrelease: dist releasecheck
	cvs tag -c $(CVSTAG)
	mkdir $(DISTDIR)/$(DISTNAME)
	cp $(DISTFILES) $(DISTDIR)/$(DISTNAME)
	cd $(DISTDIR); $(TAR) cvfz $(DISTNAME).tar.gz $(DISTNAME)

ChangeLog: CVS/Entries
	rcs2log -c "" > ChangeLog

dist:
	rsync -av -C --exclude=*.d --exclude=*.ko --exclude=*.cmd --exclude=*.mod.c --exclude=*.mod --delete-excluded ./ $(DISTDIR)/testing/
	cp $(TOPDISTFILES) $(DISTDIR)

push: dist
	$(PUSH_WWW)

ifeq (,$(OLDMAKE))
# 2.6 style modules, get the kernel makefiles to do the work

obj-m := $(MODULES)

modules:
	$(MAKE) -C $(KERNEL_SRC) M=$(ORINOCODIR) modules

install: all installconf
	mkdir -p $(MODULE_DIR_WIRELESS)
	install -m 644 -o 0 -g 0 $(MODULES:%.o=%.ko) $(MODULE_DIR_WIRELESS)
	$(DEPMOD) -ae

else
# 2.4 style modules
KERNEL_HEADERS = -I$(KERNEL_SRC)/include
ifdef PCMCIA_CS
PCMCIA_HEADERS = -I$(PCMCIA_CS)/include
endif

CPPFLAGS = -D__KERNEL__ -DMODULE $(PCMCIA_HEADERS) $(KERNEL_HEADERS)
ifndef PCMCIA_CS
	CPPFLAGS += -DEXPORT_SYMTAB
endif
CFLAGS = -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -fno-common \
	-pipe $(EXTRACFLAGS)

ifdef CONFIG_MODVERSIONS
MFLAG = -DMODVERSIONS -include $(KERNEL_SRC)/include/linux/modversions.h
endif

modules: $(MODULES)

install: all installconf
	mkdir -p $(MODULE_DIR_WIRELESS)
	for f in $(MODULES); do \
	    if test -e $(MODULE_DIR_PCMCIA)/$$f; then \
		install -m 644 -o 0 -g 0 $$f $(MODULE_DIR_PCMCIA)/$$f; \
	    else \
		install -m 644 -o 0 -g 0 $$f $(MODULE_DIR_WIRELESS)/$$f; \
	    fi; \
	done
	$(DEPMOD) -ae

%.o: %.c
	$(CC) -MD $(CFLAGS) $(CPPFLAGS) $(MFLAG) -c $<

%.s: %.c
	$(CC) -MD $(CFLAGS) $(CPPFLAGS) -S $<

%.i: %.c
	$(CC) -MD $(CPPFLAGS) -E $< -o $@

-include $(SRCS:%.c=%.d)

endif
