# Makefile for IPWireless USB Modem Driver for Linux Kernel
#
# Edit the location of the kernel directory below, or (better) set an
# environment variable for KERNELDIR
#
# NOTE!
#   * For 2.4 kernels, this can be a 'kernel-headers' directory
#   * For 2.6 kernels, this MUST be a full kernel build tree

# KERNELDIR=/usr/src/linux

# This case is for when the 2.6 kernel build calls this makefile back
ifneq ($(KERNELRELEASE),)
obj-m := ipwireless_usb.o
module-objs := ipwireless_usb.o
else

# Extract the kernel major version from 
KERNEL_MAJOR=$(shell awk '$$2=="UTS_RELEASE" {split($$3,a,".");print substr(a[1],2) "." a[2]}' $(KERNELDIR)/include/linux/version.h)


# ------ BUILD FOR 2.6 KERNEL ------
ifeq ($(KERNEL_MAJOR),2.6)
PWD  := $(shell pwd)

all:
	$(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) modules

else

# ------ BUILD FOR 2.4 KERNEL ------
ifeq ($(KERNEL_MAJOR),2.4)

include $(KERNELDIR)/.config

CFLAGS = -D__KERNEL__ -DMODULE -I$(KERNELDIR)/include -O -Wall

ifdef CONFIG_SMP
  CFLAGS += -D__SMP__ -DSMP
endif

all: ipwireless_usb.o

else
ifeq ($(KERNEL_MAJOR),)
# ------ COULDN'T DETERMINE KERNEL VERSION ------
default:
	@echo
	@echo 'Failed to determine kernel version - have you set your KERNELDIR environment variable?'
	@false
else
# ------ UNKNOWN KERNEL VERSION ------
default:
	@echo
	@echo "Don't know how to build for this kernel version: $(KERNEL_MAJOR)"
	@false
endif
endif

endif

install: all
	cp ipwireless_usb.o /lib/modules/`uname -r`/kernel/drivers/usb
	cp ipwireless /etc/init.d/
	rm -f /etc/rcS.d/S90ipwireless
	ln -s /etc/init.d/ipwireless /etc/rcS.d/S90ipwireless
	chmod +x /etc/init.d/ipwireless
	/etc/init.d/ipwireless restart

uninstall:
	@if [[ -x /etc/init.d/ipwireless ]] ; then /etc/init.d/ipwireless stop ; fi
	rm -f /etc/init.d/ipwireless /etc/rcS.d/S90ipwireless
	rm -f /lib/modules/`uname -r`/kernel/drivers/usb/ipwireless_usb.o

clean:
	rm -f *.o *~ core *.mod.c *.mod.o *.ko .*.cmd

endif
