COMMON_DIR:=common
DRIVER_DIR:=
KDIR:=/lib/modules/$(shell uname -r)/build
pwd:=$(src)

ifneq ($(KERNELRELEASE),)
obj-$(CONFIG_ISCSI_MOD)		:= 	iscsi_sfnet.o
iscsi_sfnet-objs		:= 	$(DRIVER_DIR)/iscsi-initiator.o 
iscsi_sfnet-objs		+=	$(DRIVER_DIR)/iscsi-attr.o \
					$(DRIVER_DIR)/iscsi-session.o \
					$(DRIVER_DIR)/iscsi-task.o \
					$(DRIVER_DIR)/iscsi-portal.o \
					$(DRIVER_DIR)/iscsi-ioctl.o \
					$(DRIVER_DIR)/iscsi-network.o \
					$(DRIVER_DIR)/iscsi-recv-pdu.o \
					$(DRIVER_DIR)/iscsi-xmit-pdu.o \
					$(DRIVER_DIR)/iscsi-auth.o \
					$(COMMON_DIR)/login/iscsi-login.o \
					$(COMMON_DIR)/auth/iscsi-auth-client.o

obj-m				+=	scsi_transport_iscsi.o


ISCSI_MODFLAGS :=
ISCSI_MODFLAGS += -Werror -I$(pwd)/driver/include -I$(pwd)/common/include -I$(pwd)/common/auth -I$(pwd)/common/login -I$(pwd)/include

ifeq ($(CONFIG_SCSI_DEBUG),y)
ISCSI_MODFLAGS += -DDEBUG=1
endif

EXTRA_CFLAGS    += $(ISCSI_MODFLAGS)

endif ## THE KERNEL MAKEFILE ENDS HERE!!!

# record the version of the running kernel
KERNEL_VPSE:=$(shell uname -r)

# Try to find the top of the Linux kernel source tree for the running kernel
ifeq ($(TOPDIR),)
  ifeq ($(wildcard /lib/modules/$(KERNEL_VPSE)/build/.),/lib/modules/$(KERNEL_VPSE)/build/.)
    TOPDIR:=/lib/modules/$(KERNEL_VPSE)/build
  endif
  ifeq ($(TOPDIR),)
    $(warning Linux kernel source must be installed to compile this code.)
    $(error Could not find the top of the linux kernel source tree.)
  endif
endif

# record the kernel source version
KERNEL_VERSION:=$(shell awk -F"[ \t]*=[ \t]*" '$$1 == "VERSION" { print $$2 }' $(TOPDIR)/Makefile | sed -e 's/[ \t\n]*//g')
KERNEL_PATCHLEVEL:=$(shell awk -F"[ \t]*=[ \t]*" '$$1 == "PATCHLEVEL" {print $$2}' $(TOPDIR)/Makefile | sed -e 's/[ \t\n]*//g')
KERNEL_SUBLEVEL:=$(shell awk -F"[ \t]*=[ \t]*" '$$1 == "SUBLEVEL" {print $$2 }' $(TOPDIR)/Makefile | sed -e 's/[ \t\n]*//g')
KERNEL_EXTRAVERSION:=$(shell awk -F"[ \t]*=[ \t]*" '$$1 == "EXTRAVERSION" {print $$2 }' $(TOPDIR)/Makefile | sed -e 's/[ \t\n]*//g')
KERNEL_SRC_VPSE:=$(KERNEL_VERSION).$(KERNEL_PATCHLEVEL).$(KERNEL_SUBLEVEL)$(KERNEL_EXTRAVERSION)
ifeq ($(wildcard $(TOPDIR)/.config),$(TOPDIR)/.config)
  KERNEL_CONFIG:=$(TOPDIR)/.config
endif

checkkernel:
	@printf "\nNote: using kernel source from $(TOPDIR)\ncontaining kernel version $(KERNEL_SRC_VPSE)\n\n" | fmt -
	@if [ "$(KERNEL_CONFIG)" ] ; then \
                printf "Note: using kernel config from $(KERNEL_CONFIG)\n\n" | fmt - ; \
        else \
	   ( printf "Warning: failed to find a kernel config file for $(KERNEL_SRC_VPSE).\n" ; \
             printf "The iSCSI kernel module may fail to load or run properly.\n\n"; ) | fmt - ; \
        fi
        @if [ "$(KERNEL_VERSION).$(KERNEL_PATCHLEVEL)" = "2.4" ] ; then \
            ( printf "\nError: this iSCSI driver does not support 2.4 kernels.\n" ; \
              printf "You must upgrade your kernel to 2.6 or later\n" ; \
              printf "in order to use this driver.\n\n" ; ) | fmt - ; \
            exit 1; \
        fi
	@if [ "$(KERNEL_SRC_VPSE)" != "$(KERNEL_VPSE)" ] ; then\
            (   echo "Warning: $(TOPDIR) contains kernel version $(KERNEL_SRC_VPSE)," ; \
                echo "but kernel version $(KERNEL_VPSE) is currently running.";\
                echo "This mismatch may prevent the iSCSI module from loading onthe"; \
                echo "running kernel due to unresolved symbols, or may cause problems" ; \
                echo "during the operation of the iSCSI driver.  The iSCSI driver will" ; \
                echo "be built for kernel version $(KERNEL_SRC_VPSE)." ; \
                echo "If you wish to build the iSCSI driver for the kernel that is" ; \
                echo "currently running, you must install the appropriate kernelsource"; \
                echo "and rebuild the iSCSI driver."; \
                echo ; ) | fmt - ; \

all:: module 
module: checkkernel
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules;
