#===================================================================
# API-CAN(LNX) Configuration Makefile
# Copyright(C) 2002 CONTEC CO.,LTD.
#===================================================================

I386_DIR = i386
AMD64_DIR = amd64
ARM32_DIR = arm32
ARM64_DIR = arm64
OBJS = cgetres.o
TARGET = cgetres.ko

all :   $(TARGET)

$(TARGET):  cgetres.c
	rm -fr `uname -r`
	mkdir `uname -r`
	make -C /lib/modules/`uname -r`/build M=`pwd` V=1 modules
	mv $(TARGET) `uname -r`

clean :
	if [ -f "/etc/udev/rules.d/180-contec-can.rules" ]; then rm -f "/etc/udev/rules.d/180-contec-can.rules"; udevadm control --reload; fi
	if [ -f "./180-contec-can.rules" ]; then rm -f "./180-contec-can.rules"; udevadm control --reload; fi
	if [ -f "/usr/local/sbin/contec_can_setup.sh" ]; then rm -f "/usr/local/sbin/contec_can_setup.sh"; fi
	if [ -f "/etc/systemd/system/contec_can_unload.service" ]; then rm -f "/etc/systemd/system/contec_can_unload.service"; systemctl daemon-reload; fi
	if lsmod | grep -q '^ccan '; then rmmod ccan; fi
	if [ -f "/lib/modules/`uname -r`/kernel/drivers/char/ccan.ko" ]; then rm -f "/lib/modules/`uname -r`/kernel/drivers/char/ccan.ko"; fi
	if set -- /dev/ccan[0-9][0-9][0-9]; [ -e "$$1" ]; then rm -f /dev/ccan[0-9][0-9][0-9]; fi
	if [ -f "/usr/local/etc/contec_can.conf" ]; then rm -f "/usr/local/etc/contec_can.conf"; fi
	if [ -f "/run/ccan.lock" ]; then rm -f "/run/ccan.lock"; fi
	

	make -C /lib/modules/`uname -r`/build M=`pwd` V=1 clean
	rm -fr `uname -r`

obj-m :=  cgetres.o

clean-files := *.o *.ko *.mod.[co] *~ .*.cmd

install:
ifeq ($(shell arch),aarch64)
	cp $(ARM64_DIR)/config ./
else ifeq ($(shell arch),armv7l)
	cp $(ARM32_DIR)/config ./
else ifeq ($(shell arch),x86_64)
	cp $(AMD64_DIR)/config ./
else
	cp $(I386_DIR)/config ./
endif

