# Driver files
SRC = sc92031.c
TARGET = sc92031.o

# Kernel Search Path
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))

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

# if Linux kernel source is set up?
ifeq (,$(KSRC))
  $(error Linux kernel source not found)
endif

SRCDIR := $(KSRC)/drivers/net

VERSION_FILE := $(KSRC)/include/linux/version.h
CONFIG_FILE  := $(KSRC)/include/linux/config.h

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

# pick a compiler
ifneq (,$(findstring egcs-2.91.66, $(shell cat /proc/version)))
  CC := kgcc gcc cc
else
  CC := gcc cc
endif

test_cc = $(shell which $(cc) > /dev/null 2>&1 && echo $(cc))
CC := $(foreach cc, $(CC), $(test_cc))
CC := $(firstword $(CC))

# standard flags for module builds
CFLAGS += -Wall -DLINUX -D__KERNEL__ -DMODULE -DEXPORT_SYMTAB -D__NO_VERSION__ -O2 -pipe
CFLAGS += -I$(KSRC)/include -I. -Wstrict-prototypes -fomit-frame-pointer
CFLAGS += $(shell [ -f $(KSRC)/include/linux/modversions.h ] && \
            echo "-DMODVERSIONS -include $(KSRC)/include/linux/modversions.h")

# get the kernel version 
KVER := $(shell $(CC) $(CFLAGS) -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/net
else
  INSTDIR := /lib/modules/$(KVER)/net
endif

# look for SMP in config.h
SMP := $(shell $(CC) $(CFLAGS) -E -dM $(CONFIG_FILE) | \
         grep CONFIG_SMP | awk '{ print $$3 }')
ifneq ($(SMP),1)
  SMP := 0
endif

ifneq ($(SMP),$(shell uname -a | grep SMP > /dev/null 2>&1 && echo 1 || echo 0))
  $(warning ***)
  ifeq ($(SMP),1)
     $(warning *** Warning: kernel source configuration (SMP))
     $(warning *** does not match running kernel (UP))
  else
     $(warning *** Warning: kernel source configuration (UP))
     $(warning *** does not match running kernel (SMP))
  endif
  $(warning *** Continuing with build,)
  $(warning *** resulting driver may not be what you want)
  $(warning ***)
endif          

ifeq ($(SMP), 1)
  CFLAGS += -D__SMP__
endif          

#enable or disable debug
DEBUG = n
ifeq ($(DEBUG),y)
   CFLAGS += -g -DSILAN_DEBUG -DSILAN_NDEBUG
endif

.SILENT: $(TARGET) clean

$(TARGET): 
	echo
	echo "*****************************"
	echo " $(TARGET) built for $(KVER)"
	echo -n "   SMP "	
	if [ "$(SMP)" = "1" ];\
	then echo "Enabled ";\
	else echo "Disabled";\
	fi
	echo "*****************************"
	echo
	$(CC) $(CFLAGS) -c $(SRC)

install: $(SRC) $(TARGET)
	mkdir -p $(MOD_ROOT)$(SRCDIR)
	install -m 644 -o root $(SRC) $(MOD_ROOT)$(SRCDIR)
	mkdir -p $(MOD_ROOT)$(INSTDIR)
	install -m 644 -o root $(TARGET) $(MOD_ROOT)$(INSTDIR)
	

ifeq (,$(MOD_ROOT))
	/sbin/depmod -a || true
else
	/sbin/depmod -b $(MOD_ROOT) -a || true
endif


uninstall:
	if [ -e $(INSTDIR)/$(TARGET) ]; then \
	     rm -f $(INSTDIR)/$(TARGET);\
	fi
	
	if [ -e $(SRCDIR)/$(SRC) ]; then \
	     rm -f $(SRCDIR)/$(SRC);\
	fi

	/sbin/depmod -a


clean:
	rm -f $(TARGET) *~

	
