VERSION = 0.13d

# This should be defined if the modules from pcmcia-cs are used.
# Leave it blank if you have PCMCIA support in the kernel.
PCMCIA_CS =

CONF_DIR = /etc/pcmcia
                                                                               
ORINOCODIR = $(shell pwd)
KERNEL_VERSION = $(shell uname -r)
KERNEL_SRC = /lib/modules/$(KERNEL_VERSION)/build

# if Rules.make exists in the kernel tree, we assume 2.4 style modules
# if it doesn't assume 2.5 style
OLDMAKE = $(wildcard $(KERNEL_SRC)/Rules.make)
ifeq (,$(OLDMAKE))
ifneq (,$(PCMCIA_CS))
$(error Can't cope with 2.5 and pcmcia-cs together)
endif
endif

MODULES = hermes.o orinoco.o orinoco_cs.o orinoco_plx.o orinoco_tmd.o orinoco_pci.o nortel_pci.o
ifeq ($(shell uname -m),ppc)
MODULES += airport.o
endif

SRCS = hermes.c \
	orinoco.c \
	airport.c orinoco_cs.c orinoco_plx.c orinoco_tmd.c orinoco_pci.c nortel_pci.c \
	userhermes.c
HDRS = hermes.h hermes_rid.h ieee802_11.h orinoco.h
CONF = hermes.conf
TAR = tar

DISTFILES = $(SRCS) $(HDRS) $(CONF) Makefile README.orinoco
TOPDISTFILES = README.orinoco $(CONF)
DISTNAME = orinoco-$(VERSION)
DISTDIR = ../dist
PUSH_WWW = push-www.sh

all: modules

installconf:
	if [ -d $(CONF_DIR) ]; then install -m 644 -o 0 -g 0 $(CONF) $(CONF_DIR)/$(CONF); fi


userhermes: userhermes.c
	$(CC) $(CFLAGS) -lhistory -lreadline -lncurses -o $@ $<

bufparse: bufparse.c
	$(CC) $(CFLAGS) -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 userhermes bufparse
	rm -f *.ko *.mod.c .*.o.cmd

.PHONY: dist

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

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

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

push: dist
	$(PUSH_WWW)

ifeq (,$(OLDMAKE))
# 2.5 style modules, get the kernel makefiles to do the work
KBUILD_VERBOSE = 0	# default

obj-m := $(MODULES)

modules:
	$(MAKE) -C $(KERNEL_SRC) SUBDIRS=$(ORINOCODIR) KBUILD_VERBOSE=$(KBUILD_VERBOSE) modules

install: all installconf
	$(MAKE) -C $(KERNEL_SRC) SUBDIRS=$(ORINOCODIR) KBUILD_VERBOSE=$(KBUILD_VERBOSE) modules_install

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

MODULE_DIR_TOP = /lib/modules/$(KERNEL_VERSION)
MODULE_DIR_PCMCIA = $(MODULE_DIR_TOP)/pcmcia
MODULE_DIR_WIRELESS = $(MODULE_DIR_TOP)/kernel/drivers/net/wireless
CPPFLAGS = -D__KERNEL__ -DPCMCIA_DEBUG=1 \
	-DMODULE -DEXPORT_SYMTAB \
	$(PCMCIA_HEADERS) $(KERNEL_HEADERS)
CFLAGS = -O2 -Wall -Wstrict-prototypes -fno-common -pipe $(EXTRACFLAGS)

MODVER = $(shell if cat $(KERNEL_SRC)/include/linux/autoconf.h 2>/dev/null | \
grep -q '^[[:space:]]*\#define[[:space:]]*CONFIG_MODVERSIONS[[:space:]]*1'; \
then echo 1; else echo 0; fi)

ifeq ($(MODVER),1)
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 -a

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

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

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

endif
