obj-m = langford.o
KVERSION = $(shell uname -r)

all: langford.ko langford_util langford_adc_util

langford.ko: langford.c
	export CCFLAGS="-ansi -Wall"
	sudo make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

install: langford.ko langford_util langford_adc_util
	#Ensure module recompilation everytime it needs to be installed.
	#(Prevents module version errors after kernel upgrades.) 
	-sudo rm -f langford.ko
	-sudo rm -rf /dev/langford
	-sudo modprobe -r langford
	make langford.ko
	sudo cp langford.ko /lib/modules/`uname -r`/kernel/drivers/misc
	sudo depmod -a
	sudo modprobe langford
#Allow some time for driver to finish loading. If driver does not complete loading, no character device will be registered and mknod will fail.
	sleep 3
	sync
	sudo cp langford_util /usr/bin
	sudo cp langford_adc_util /usr/bin
	sudo cp langford_init /usr/bin
#Create langford group, if not already.
	-sudo groupadd langford
	sudo chmod a+rx /usr/bin/langford_util
	make -C libs/ install
	echo "Remember to run langford_init in order to initialize the device at /dev/langford"	

langford_util: langford_util.cpp
	g++ $< -ansi -Wall -g -o $@

langford_adc_util: langford_adc_util.cpp langford_spi.cpp
	g++ langford_adc_util.cpp langford_spi.cpp -ansi -Wall -g -o $@

libs:
	make -f libs/Makefile

clean:
	-sudo make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
	-sudo rm -rf langford.ko.gz langford_util langford_adc_util
	make -C libs/ clean

distclean: clean
	-sudo make -C ./ clean
	-sudo rm -rf .tmp*
	-sudo rm -rf *~
	#-sudo rm modules.order Module.symvers

uninstall:
	-sudo rm /usr/bin/langford_util
	-sudo rm /usr/bin/langford_init
	-sudo rm /usr/bin/langford_adc_util
	# Remove langford group
	-sudo groupdel langford
	make -C libs/ uninstall
