TARGET_DRIVER:=mxuport2000
LVER:=$(shell uname -r)

ifeq ($(KERNELRELEASE),)
       include $(PWD)/ver.mk
else
endif

export DRV_VER

##############################################################
ifneq	"$(findstring 2.4, $(shell uname -a))" "2.4"

all: module

install: module
	@if [ ! -d "/etc/moxa" ]; then \
		mkdir /etc/moxa; \
	fi
	@cp -f mxuport2000_unbind /etc/moxa/
	@cp -f 99-mxuport2000-unbind.rules /etc/udev/rules.d/
	@udevadm control --reload-rules && udevadm trigger
	@mkdir -p /lib/modules/$(LVER)/kernel/drivers/usb/serial/
	@cp -f ./$(TARGET_DRIVER).ko /lib/modules/$(LVER)/kernel/drivers/usb/serial/
	@depmod -a
	@echo " "
	@echo "**********************************************************************"
	@echo " MOXA UPort 2000 series driver ver $(DRV_VER) installed successfully."
	@echo "**********************************************************************"

ifneq ($(KERNELRELEASE),)
obj-m := $(TARGET_DRIVER).o
#DBG_CFLAGS := -g -DDEBUG
MXPARM += ${DBG_CFLAGS}
ccflags-y += ${DBG_CFLAGS}
else
KDIR	:= /lib/modules/$(LVER)/build
PWD	:= $(shell pwd)

module: envchk
	@$(MAKE) EXTRA_CFLAGS=$(MXPARM) -s -C $(KDIR) M=$(PWD) modules 2>> build.log \
	&& cp -f ./build.log ../build.log \
	&& exit 0 \
	|| echo "Build Fail! Please check build.log for further information." \
	&& cp -f ./build.log ../build.log \
	&& exit 1
endif

envchk:
	@./mxconf | tee build.log

remove: clean_lib
	@if lsmod | grep -q $(TARGET_DRIVER) ; then rmmod $(TARGET_DRIVER) ; fi
	@depmod -a
	@rm -f /etc/udev/rules.d/99-mxuport2000-unbind.rules
	@rm -f /etc/moxa/mxuport2000_unbind
	@echo ""
	@echo "**********************************************************************"
	@echo " MOXA UPort 2000 series driver ver $(DRV_VER) removed successfully."
	@echo "**********************************************************************"

cleanall: clean

clean: clean_local clean_lib

clean_local:
	@echo "Removing driver files..."
	@rm -f *.o
	@rm -rf ./.tmp_versions
	@rm -f $(TARGET_DRIVER).mod*
	@rm -f .$(TARGET_DRIVER)*
	@rm -f $(TARGET_DRIVER).ko
	@rm -f *.symvers
	@rm -f *.log
	@rm -f .Module.symvers.cmd
	@rm -f .modules.order.cmd
	
clean_lib:
	@rm -f /lib/modules/$(LVER)/kernel/drivers/usb/serial/$(TARGET_DRIVER).ko
	@rm -rf ../output
	@rm -f ../*log
	@rm -f ./*log
	@rm -f ./*.markers
	@rm -f ./*.order

endif
