DRV_NAME	:=nm_mezzo

export VERSION_MAJOR ?=
export VERSION_MINOR ?= 0

ifdef VERSION_MAJOR
CCFLAGS =-DDRIVER_VERSION_MAJOR=$(VERSION_MAJOR) \
	 -DDRIVER_VERSION_MINOR=$(VERSION_MINOR)
else
CCFLAGS = -UDRIVER_VERSION_MAJOR -UDRIVER_VERSION_MINOR
endif

export CUR_DATE ?= $(shell date '+%y%m%d')
export GIT_REVD ?= $(shell git rev-parse --short=4 HEAD)

CCFLAGS += -DBUILD_DAT=$(CUR_DATE) -DBUILD_NUM=$(GIT_REVD)

ifneq ($(KERNELRELEASE),)
obj-m		:=$(DRV_NAME).o
ccflags-y	:=$(CCFLAGS)
$(DRV_NAME)-y	:=
else

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

ifneq ($(KERNELDIR), /lib/modules/$(shell uname -r)/build)
CROSS_FLAGS := ARCH=$(HOST_ARCH) CROSS_COMPILE=$(HOST_CROSS_COMPILE)
endif

.PHONY: modules
modules:
	$(MAKE) -C $(KERNELDIR) M=`pwd` $(CROSS_FLAGS) modules

.PHONY: install
install:
	cp $(DRV_NAME).ko /lib/modules/`uname -r`
	cp $(DRV_NAME).conf /etc/modprobe.d
	cp 99-$(DRV_NAME).rules /etc/udev/rules.d
	/sbin/depmod -a
	@(((lsmod | grep -i $(DRV_NAME) 1>/dev/null 2>&1) && (echo "Module doesn't load")) || \
	((/sbin/modprobe $(DRV_NAME)) && (echo "New module is loaded")))

.PHONY: uninstall
uninstall:
	rm -f /lib/modules/`uname -r`/$(DRV_NAME).ko
	rm -f /etc/modprobe.d/$(DRV_NAME).conf
	rm -f /etc/udev/rules.d/99-$(DRV_NAME).rules
	/sbin/depmod -a
	@(((lsmod | grep -i $(DRV_NAME) 1>/dev/null 2>&1) && ((/sbin/rmmod $(DRV_NAME)) || \
	(echo "Module doesn't remove"))) || (echo "Module doesn't found"))

.PHONY: clean
clean:
	$(MAKE) -C $(KERNELDIR) M=`pwd` $(CROSS_FLAGS) clean
endif
