#
#
#

CONFIG_CTAP_SHORT = ON

ifneq ($(KERNELRELEASE),)
	obj-m	 := r8152.o
	ccflags-y += -DRTL8152_S5_WOL
#	ccflags-y += -DRTL8152_DEBUG

ifneq (,$(filter OFF off, $(CONFIG_CTAP_SHORT)))
		ccflags-y += -DCONFIG_CTAP_SHORT_OFF
endif

ifeq (TRUE, $(shell test $(VERSION) -lt 5 && echo "TRUE" || \
		test $(VERSION) -eq 5 && test $(PATCHLEVEL) -lt 12 && echo "TRUE"))
		ccflags-y += -DLINUX_VERSION_MAJOR=$(VERSION)
		ccflags-y += -DLINUX_VERSION_PATCHLEVEL=$(PATCHLEVEL)
		ccflags-y += -DLINUX_VERSION_SUBLEVEL=$(SUBLEVEL)
endif
else
	KSRC ?= /lib/modules/$(shell uname -r)/build
	PWD :=$(shell pwd)
	MODDESTDIR := kernel/drivers/net/usb
	INSTALL_PREFIX ?=
	CLR_MODULE_FILES := *.mod.c *.mod *.o .*.cmd *.ko *~ .tmp_versions* modules.order Module.symvers
	SYMBOL_FILE := Module.symvers
	INBOXDRIVER := $(shell find $(subst build,$(MODDESTDIR),$(KSRC)) -name r8152.ko.* -type f)
	RULEFILE = 50-usb-realtek-net.rules
	RULEDIR = /etc/udev/rules.d/

USER_MODULE_NAME := r8152
ifneq ($(USER_MODULE_NAME),)
MODULE_NAME := $(USER_MODULE_NAME)
endif

.PHONY: modules
modules:
	$(MAKE) -C $(KSRC) M=$(PWD) modules

.PHONY: all
all: clean modules install

.PHONY: clean
clean:
	$(MAKE) -C $(KSRC) M=$(shell pwd) clean
	cd hal ; rm -fr */*/*/*.mod.c */*/*/*.mod */*/*/*.o */*/*/.*.cmd */*/*/*.ko*
	cd hal ; rm -fr */*/*.mod.c */*/*.mod */*/*.o */*/.*.cmd */*/*.ko*
	cd hal ; rm -fr */*.mod.c */*.mod */*.o */.*.cmd */*.ko*
	cd hal ; rm -fr *.mod.c *.mod *.o .*.cmd *.ko*
	cd core ; rm -fr */*.mod.c */*.mod */*.o */.*.cmd */*.ko*
	cd core ; rm -fr *.mod.c *.mod *.o .*.cmd *.ko*
	cd os_dep/linux ; rm -fr *.mod.c *.mod *.o .*.cmd *.ko*
	cd os_dep ; rm -fr *.mod.c *.mod *.o .*.cmd *.ko*
	cd platform ; rm -fr *.mod.c *.mod *.o .*.cmd *.ko*
	rm -fr Module.symvers ; rm -fr Module.markers ; rm -fr modules.order
	rm -fr *.mod.c *.mod *.o .*.cmd *.ko* *~
	rm -fr .tmp_versions

.PHONY: install
install:
ifneq ($(shell lsmod | grep r8153_ecm),)
	rmmod r8153_ecm
endif
ifneq ($(shell lsmod | grep r8152),)
	rmmod r8152
endif
ifneq ($(INBOXDRIVER),)
	rm -f $(INBOXDRIVER)
endif
	mkdir -p $(MODDESTDIR)
	install -p -m 644 $(MODULE_NAME).ko  $(MODDESTDIR)

	/sbin/depmod -a ${KVER}

.PHONY:uninstall
uninstall:
	rm -f $(MODDESTDIR)/$(MODULE_NAME).ko*
	/sbin/depmod -a ${KVER}

.PHONY: install_rules
install_rules:
	install --group=root --owner=root --mode=0644 $(RULEFILE) $(RULEDIR)

endif

