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

AMD64_DIR = amd64
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-zm.rules" ]; then rm -f "/etc/udev/rules.d/180-contec-zm.rules"; udevadm control --reload; fi
	if [ -f "./180-contec-zm.rules" ]; then rm -f "./180-contec-zm.rules"; udevadm control --reload; fi
	if [ -f "/usr/local/sbin/contec_zm_setup.sh" ]; then rm -f "/usr/local/sbin/contec_zm_setup.sh"; fi
	if [ -f "/etc/systemd/system/contec_zm_unload.service" ]; then rm -f "/etc/systemd/system/contec_zm_unload.service"; systemctl daemon-reload; fi
	if lsmod | grep -q '^czm '; then rmmod czm; fi
	if [ -f "/lib/modules/`uname -r`/kernel/drivers/char/czm.ko" ]; then rm -f "/lib/modules/`uname -r`/kernel/drivers/char/czm.ko"; fi
	if set -- /dev/czm[0-9][0-9][0-9]; [ -e "$$1" ]; then rm -f /dev/czm[0-9][0-9][0-9]; fi
	if [ -f "/usr/local/etc/contec_zm.conf" ]; then rm -f "/usr/local/etc/contec_zm.conf"; fi
	if [ -f "/run/czm.lock" ]; then rm -f "/run/czm.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:
	cp $(AMD64_DIR)/config ./

