###########################################################################
# Environment tests
#
# Kernel Search Path
# All the places we look for kernel source
KSP :=  /lib/modules/$(shell uname -r)/build \
        /usr/src/linux-$(shell uname -r) \
        /usr/src/linux-$(shell uname -r | sed 's/-.*//') \
        /usr/src/kernel-headers-$(shell uname -r) \
        /usr/src/kernel-source-$(shell uname -r) \
        /usr/src/linux-$(shell uname -r | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/') \
        /usr/src/linux

# prune the list down to only values that exist
# and have an include/linux sub-directory
test_dir = $(shell [ -e $(dir)/include/linux ] && echo $(dir))
KSP := $(foreach dir, $(KSP), $(test_dir))

# we will use this first valid entry in the search path
KSRC := $(firstword $(KSP))

ifeq (,$(KSRC))
  $(error Linux kernel source not found)
endif

# files we get information from in KSRC
# check for version.h and autoconf.h for running kernel in /boot (SuSE)
ifneq (,$(wildcard /boot/vmlinuz.version.h))
	VERSION_FILE := /boot/vmlinuz.version.h
	CONFIG_FILE  := /boot/vmlinuz.autoconf.h
	KVER := $(shell $(CC) $(CFLAGS) -I$(KSRC)/include -E -dM $(VERSION_FILE) | grep UTS_RELEASE | awk '{ print $$3 }' | sed 's/\"//g')
	ifeq ($(KVER),$(shell uname -r))
		# set up include path to override headers from kernel source
		x:=$(shell rm -rf include)
		x:=$(shell mkdir -p include/linux)
		x:=$(shell cp /boot/vmlinuz.version.h include/linux/version.h)
		x:=$(shell cp /boot/vmlinuz.autoconf.h include/linux/autoconf.h)
		CFLAGS += -I./include
	else
		VERSION_FILE := $(KSRC)/include/linux/version.h
		CONFIG_FILE  := $(KSRC)/include/linux/config.h
	endif
else
	VERSION_FILE := $(KSRC)/include/linux/version.h
	CONFIG_FILE  := $(KSRC)/include/linux/config.h
endif

ifeq (,$(wildcard $(VERSION_FILE)))
  $(error Linux kernel source not configured - missing version.h)
endif

ifeq (,$(wildcard $(CONFIG_FILE)))
  $(error Linux kernel source not configured - missing config.h)
endif

# get the kernel version - we use this to find the correct install path
KVER := $(shell $(CC) $(CFLAGS) -I$(KSRC)/include -E -dM $(VERSION_FILE) | grep UTS_RELEASE | awk '{ print $$3 }' | sed 's/\"//g')

ifneq ($(KVER),$(shell uname -r))
  $(warning ***)
  $(warning *** Warning: kernel source version ($(KVER)))
  $(warning *** does not match running kernel  ($(shell uname -r)))
  $(warning *** Continuing with build,)
  $(warning *** resulting driver may not be what you want)
  $(warning ***)
endif

# pick an appropriate install path
ifneq (,$(wildcard /lib/modules/$(KVER)/kernel))
  INSTDIR := /lib/modules/$(KVER)/kernel/drivers/scsi
else
  INSTDIR := /lib/modules/$(KVER)/scsi
endif

CROSS_COMPILE	=
CC	= $(CROSS_COMPILE)gcc
LD	= $(CROSS_COMPILE)ld
HPATH	= -I$(KSRC)/include -I$(KSRC) -Icam -I$(KSRC)/arch/i386/mach-generic
MFLAGS	= -D__KERNEL__ -DMODULE -D__LINUX__ -D_MMIO_ $(HPATH)
CFLAGS	+= $(MFLAGS) -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer \
	  -fno-strict-aliasing -pipe -mpreferred-stack-boundary=2

CFLAGS	+= $(shell [ -f $(KSRC)/include/linux/modversions.h ] && \
            echo "-DMODVERSIONS -include $(KSRC)/include/linux/modversions.h")

GENERIC	= $(CFLAGS) -fno-common -march=i386 

SuSE	= $(CFLAGS) -march=i586 

TURBO	= $(CFLAGS) -march=i586

MDK	= $(CFLAGS) -fno-common -fno-merge-constants -march=i586

PPC	= $(CFLAGS) -fno-common -DCPU=ppc

TARGET	= pdc-ultra.o
CFILES	= pdc618_mod.c
HFILES	= pdc618_mod.h


.SLIENT: $(TARGET)
$(TARGET): $(filter-out $(TARGET), $(CFILES:.c=.o)) cam/cam_mod.o
	$(LD) -r -o $@ $^

$(CFILES:.c=.o): $(CFILES) $(HFILES) Makefile
	cd cam; ${MAKE}
	$(CC) $(GENERIC) -c $*.c 
#$(CC) $(SuSE) -c $*.c 
#$(CC) $(TURBO) -c $*.c 
#$(CC) $(MDK) -c $*.c 
#$(CC) $(PPC) -c $*.c 

install: $(TARGET) 
	find /lib/modules/$(KVER) -name $(TARGET)/ -exec rm {} \; 
	mkdir -p $(INSTDIR)
	install -m 644 -o 0 $(TARGET) $(INSTDIR)
	/sbin/depmod -aqs 

uninstall:
	rm -f $(INSTDIR)/$(TARGET)
	/sbin/depmod -aqs

clean	:
	cd cam; ${MAKE} clean
	rm -rf $(TARGET) $(CFILES:.c=.o) include
