UNAME:=$(shell uname -r)
KVER:=$(shell uname -r)
INSTDIR:=/lib/modules/$(KVER)/kernel/drivers
RULESDIR:=/etc/udev/rules.d



#-------------------------------------------------
# Is the kernel 2.6 or 3.x ...
#-------------------------------------------------
K_VERSION:=$(shell uname -r | cut -c1-3 | sed 's/2\.[56]/2\.6/')

ifeq ($(K_VERSION), 2.6)

TARGET=tsyncpci.ko

#-------------------------------------------------
# Test if kernel version is 2.6.13 or later
#-------------------------------------------------
K_VERSION:=$(shell uname -r | cut -c1-6 | sed 's/2\.6\.[1-9][0-9]/post/')

ifeq ($(K_VERSION), post)
     RULEFILE:=25-tsyncpci.rules
else
     RULEFILE:=25-tsyncpci_2_6_9.rules
endif


ifneq ($(KERNELRELEASE),)

obj-m += tsyncpci.o

#-------------------------------------------------
# If kernel version is 2.6.10 to 2.6.12
#-------------------------------------------------
K_VERSION:=$(shell uname -r | cut -c1-6 | sed 's/2\.6\.1[0-2]/pre/')

ifeq ($(K_VERSION), pre)

tsyncpci-objs := tsync_drv_2_6.o \
                 tpro_func.o \
                 tpro_func32.o \
                 tsync_func.o \
                 tsync_hw_func.o \
                 tsync_hw_nonkts_func.o \
                 tsync_comm_validation.o \
                 tsync_comm_transaction.o \
                 tsync_comm_fifo.o \
                 tsync_error_codes.o \
                 os_wait.o \
                 tsync_gpio_stamp_queue.o \
                 binary_coded_decimal.o \
                 tsync_pps.o

     #-------------------------------------------------
else # kernel wasn't 2.6.10 to 2.6.12
     #-------------------------------------------------

K_VERSION:=$(shell uname -r | cut -c1-6 | sed 's/2\.6\.[1-9][0-9]/post/')

#-------------------------------------------------
# If kernel version was 2.6.13 or later
#-------------------------------------------------
ifeq ($(K_VERSION), post)

tsyncpci-objs := tsync_drv_2_6_13_1.o \
                 tpro_func.o \
                 tpro_func32.o \
                 tsync_func.o \
                 tsync_hw_func.o \
                 tsync_hw_nonkts_func.o \
                 tsync_comm_validation.o \
                 tsync_comm_transaction.o \
                 tsync_comm_fifo.o \
                 tsync_error_codes.o \
                 os_wait.o \
                 tsync_gpio_stamp_queue.o \
                 binary_coded_decimal.o \
                 tsync_pps.o

     #-------------------------------------------------
else # kernel version must be 2.6.0 to 2.6.9
     #-------------------------------------------------

tsyncpci-objs := tsync_drv_2_6.o \
                 tpro_func.o \
                 tpro_func32.o \
                 tsync_func.o \
                 tsync_hw_func.o \
                 tsync_hw_nonkts_func.o \
                 tsync_comm_validation.o \
                 tsync_comm_transaction.o \
                 tsync_comm_fifo.o \
                 tsync_error_codes.o \
                 os_wait.o \
                 tsync_gpio_stamp_queue.o \
                 binary_coded_decimal.o \
                 tsync_pps.o
endif

endif

# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
	@echo $(KG_VERSION)
	@echo $(KERNELRELEASE)
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

ntp:
	@echo $(KG_VERSION)
	@echo $(KERNELRELEASE)
	$(MAKE) -C $(KERNELDIR) M=$(PWD) "EXTRA_CFLAGS=-DTSYNC_NTP" modules

endif



#-------------------------------------------------
# Else, kernel version is 3.x.x
#-------------------------------------------------
else

RULEFILE:=25-tsyncpci.rules
TARGET=tsyncpci.ko

ifneq ($(KERNELRELEASE),)

obj-m += tsyncpci.o

tsyncpci-objs := tsync_drv_2_6_13_1.o \
                 tpro_func.o \
                 tpro_func32.o \
                 tsync_func.o \
                 tsync_hw_func.o \
                 tsync_hw_nonkts_func.o \
                 tsync_comm_validation.o \
                 tsync_comm_transaction.o \
                 tsync_comm_fifo.o \
                 tsync_error_codes.o \
                 os_wait.o \
                 tsync_gpio_stamp_queue.o \
                 binary_coded_decimal.o \
                 tsync_pps.o

# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
	@echo $(KG_VERSION)
	@echo $(KERNELRELEASE)
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

ntp:
	@echo $(KG_VERSION)
	@echo $(KERNELRELEASE)
	$(MAKE) -C $(KERNELDIR) M=$(PWD) "EXTRA_CFLAGS=-DTSYNC_NTP" modules

endif

endif

#-------------------------------------------------------------------------
#-------------------------------------------------------------------------

install:
	rm -f $(RULESDIR)/$(RULEFILE)
	install -D -m 644 $(RULEFILE) $(RULESDIR)/$(RULEFILE)
	rm -f $(INSTDIR)/$(TARGET)
	install -D -m 644 $(TARGET) $(INSTDIR)/$(TARGET)
	depmod -a || true

uninstall:
	if [ -e $(RULESDIR)/$(RULEFILE) ] ; then \
	    rm -f $(RULESDIR)/$(RULEFILE) ; \
	fi
	if [ -e $(INSTDIR)/$(TARGET) ] ; then \
	    rm -f $(INSTDIR)/$(TARGET) ; \
	fi
	depmod -a || true


clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

