# Makefile for Fast Ethernet Adapter Linux Driver

CC = gcc
SHELL = sh
INSTALL_PATH1:= /lib/modules/$(shell uname -r)/kernel/drivers/net
INSTALL_PATH2:= /lib/modules/$(shell uname -r)/net

# Check whether the linux kernel is 2.4.x
ifneq (,$(shell uname -r | grep 2.4.))
# Check whether the kernel source is in /usr/src/linux-2.4 or in /usr/src/linux
ifneq (,$(wildcard /usr/src/linux-2.4))
	INCLUDE_PATH = /usr/src/linux-2.4/include
else
	INCLUDE_PATH = /usr/src/linux/include
endif
else
	INCLUDE_PATH = /usr/src/linux/include
endif


CFLAGS :=  -D__KERNEL__ -I$(INCLUDE_PATH) -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing -pipe -DMODULE  -DMODVERSION 

# Check whether the modversions.h exist.
ifneq (,$(wildcard $(INCLUDE_PATH)/linux/modversions.h))
	CFLAGS += -include $(INCLUDE_PATH)/linux/modversions.h
endif

# Check whether the kernel support SMP
SMP =0

ifneq (,$(shell cat /proc/version|grep smp))
SMP = 1
endif
ifneq (,$(shell cat /proc/version|grep SMP))
SMP = 1
endif
ifeq (1,$(SMP))
CFLAGS += -D__SMP__
endif

#Driver
MODULE_NAME = linuxfet
DRIVER = linuxfet.o
SOURCE = linuxfet.c
HEADER = linuxfet.h

all : clean $(DRIVER)

$(DRIVER) : $(SOURCE) $(HEADER)
	@if [ ! -d $(INCLUDE_PATH) ] ; then \
	    echo "*****  Sorry, the kernel header files path $(INCLUDE_PATH) doesn't exist." ; \
	    echo "*****  We can't compile this driver successfully without it." ; \
	    echo "*****  Please install kernel header files before you compile this driver." ; \
	else \
	    echo "*****  Compile the driver source files to generate $(DRIVER)" ; \
	    echo "$(CC) $(CFLAGS)  -c $(SOURCE)" ; \
	    $(CC) $(CFLAGS)  -c $(SOURCE) ; \
	    echo ; fi

install : clean $(DRIVER)
	@if [ -d $(INCLUDE_PATH) ] && [ -f $(DRIVER) ] ; then \
	    if [ -f $(INSTALL_PATH1)/via-rhine.o ] ; then \
	        echo "*****  Move official driver via-rhine.o to via-rhine.o.backup" ; \
	        echo "mv $(INSTALL_PATH1)/via-rhine.o $(INSTALL_PATH1)/via-rhine.o.backup" ; \
	        mv $(INSTALL_PATH1)/via-rhine.o $(INSTALL_PATH1)/via-rhine.o.backup ; \
	        echo ; fi ; \
	    if [ -f $(INSTALL_PATH2)/via-rhine.o ] ; then \
	        echo "*****  Move official driver via-rhine.o to via-rhine.o.backup" ; \
	        echo "mv $(INSTALL_PATH2)/via-rhine.o $(INSTALL_PATH2)/via-rhine.o.backup" ; \
	        mv $(INSTALL_PATH2)/via-rhine.o $(INSTALL_PATH2)/via-rhine.o.backup ; \
	        echo ; fi ; \
	    echo "*****  Copy driver module to $(INSTALL_PATH1)." ; \
	    echo "mkdir -p $(INSTALL_PATH1)" ; \
	    mkdir -p $(INSTALL_PATH1) ; \
	    echo "install -m 644 $(DRIVER) $(INSTALL_PATH1)" ; \
	    install -m 644 $(DRIVER) $(INSTALL_PATH1) ; \
	    echo ; \
	    echo "*****  Copy driver module to $(INSTALL_PATH2)." ; \
	    echo "mkdir -p $(INSTALL_PATH2)" ; \
	    mkdir -p $(INSTALL_PATH2) ; \
	    echo "install -m 644 $(DRIVER) $(INSTALL_PATH2)" ; \
	    install -m 644 $(DRIVER) $(INSTALL_PATH2) ; \
	    echo ; \
	    echo "*****  Handle dependency descriptions for loadable kernel modules." ; \
	    echo "*****  This will take some minutes, please wait!!" ; \
	    echo "depmod -a" ; \
	    depmod -a ; \
	    echo ; \
	    echo "*** It's recommended you modify /etc/modules.conf or /etc/conf.modules file now." ; \
	    echo "*** Make sure this line \"alias eth# $(MODULE_NAME)\" is there, where # is the network" ; \
	    echo "*** interface number (eg: alias eth0 $(MODULE_NAME)), and reboot the system." ; \
	    echo ; \
	fi

uninstall :
	@if [ -f $(INSTALL_PATH1)/$(DRIVER) ] ; then \
	    echo "*****  Remove driver module from $(INSTALL_PATH1)" ; \
	    echo "rm $(INSTALL_PATH1)/$(DRIVER)" ; \
	    echo ; \
	    rm $(INSTALL_PATH1)/$(DRIVER) ; fi
	@if [ -f $(INSTALL_PATH2)/$(DRIVER) ] ; then \
	    echo "*****  Remove driver module from $(INSTALL_PATH2)" ; \
	    echo "rm $(INSTALL_PATH2)/$(DRIVER)" ; \
	    echo ; \
	    rm $(INSTALL_PATH2)/$(DRIVER) ; fi

	@if [ -f $(INSTALL_PATH1)/via-rhine.o.backup ] ; then \
	    echo "*****  Restore official driver vai-rhine.o from $(INSTALL_PATH1)".; \
	    echo "mv $(INSTALL_PATH1)/via-rhine.o.backup $(INSTALL_PATH1)/via-rhine.o" ; \
	    echo ; \
	    mv $(INSTALL_PATH1)/via-rhine.o.backup $(INSTALL_PATH1)/via-rhine.o ; fi
	@if [ -f $(INSTALL_PATH2)/via-rhine.o.backup ] ; then \
	    echo "*****  Restore official driver vai-rhine.o from $(INSTALL_PATH2)".; \
	    echo "mv $(INSTALL_PATH2)/via-rhine.o.backup $(INSTALL_PATH2)/via-rhine.o" ; \
	    echo ; \
	    mv $(INSTALL_PATH2)/via-rhine.o.backup $(INSTALL_PATH2)/via-rhine.o ; fi

	@echo "*****  Handle dependency descriptions for loadable kernel modules."
	@echo "*****  This will take some minutes, please wait!!"
	depmod -a
	@echo

clean:
	@echo "***  Clean up older object files  ***"
	rm -f core *.o *~
	@echo
	
