# ****************************************************************
# *                                                              *
# *             Module   :  Makefile                             *
# *                                                              *
# *	        Purpose  :  Build csmencaps driver file          *
# *                                                              *
# ****************************************************************

KERNEL_DIR := /lib/modules/`uname -r`/build
PREFIX :=

### add external options if they're provided  ###
ifneq ($(CROSS_COMPILE), )
OPTIONS := CROSS_COMPILE=$(CROSS_COMPILE)
endif
ifneq ($(ARCH), )
OPTIONS += ARCH=$(ARCH)
endif

KERNEL_PATCHLEVEL := $(shell grep "PATCHLEVEL =" $(KERNEL_DIR)/Makefile | cut -d" " -f 3)

ifneq ($(KERNEL_PATCHLEVEL), 6)

### kernel 2.4 Makefile ###
CROSS := $(shell cd $(KERNEL_DIR) ; $(MAKE) -f $(PWD)/Makefile.get_options get_cross CUR_DIR=$(PWD))
CC := $(CROSS)gcc
LD := $(CROSS)ld
KERNEL24_CFLAGS := $(shell cd $(KERNEL_DIR) ; $(MAKE) -f $(PWD)/Makefile.get_options get_cflags CUR_DIR=$(PWD))	\
	-I . -DMODULE

modules: check_for_embedded_csmencaps csmencaps.o

csmencaps.o: af_csmencaps.o
	$(LD) -r $^ -o $@

af_csmencaps.o: af_csmencaps.c 
	$(CC) $(KERNEL24_CFLAGS) -c $< -o $@ 

clean:
	rm -f *.o *~ core csmencaps.o

# check 2.4 kernel whether it includes csmencaps, we shouldn't even try to compile in this case
check_for_embedded_csmencaps:
	@test ! -e $(KERNEL_DIR)/include/net/csmencaps.h || \
		echo "Found csmencaps driver source in kernel $(KERNEL_DIR)."
	@test ! -e $(KERNEL_DIR)/include/net/csmencaps.h || \
		echo "Set CONFIG_CSM_ENCAPS option and recompile the kernel."
	@test ! -e $(KERNEL_DIR)/include/net/csmencaps.h

else

### kernel 2.6 Makefile ###
TOP_DIR := $(shell cd .; pwd)

modules:
	$(MAKE) KBUILD_VERBOSE=1 $(OPTIONS) -C $(KERNEL_DIR) SUBDIRS=$(TOP_DIR) modules

clean:
	$(MAKE) KBUILD_VERBOSE=1 $(OPTIONS) -C $(KERNEL_DIR) SUBDIRS=$(TOP_DIR) clean

modules_install install:
	$(MAKE) KBUILD_VERBOSE=1 $(OPTIONS) -C $(KERNEL_DIR) INSTALL_MOD_PATH=$(PREFIX) SUBDIRS=$(TOP_DIR) modules_install

module := csmencaps

EXTRA_CFLAGS := -I$(src)/ 

obj-m += csmencaps.o
$(module)-objs := af_csmencaps.o

endif
