ifneq (${KERNELRELEASE},)

# KERNELRELEASE defined: we are being compiled as part of the Kernel
        obj-m := midi_top.o

else

# We are being compiled as a module: use the Kernel build system

	KERNEL_SOURCE := /usr/src/linux-headers-$(shell uname -r)
        PWD := $(shell pwd)

default: module ins test

test: note.h usbmidi.c usbmidi.h test.c
	cc -Wall note.h usbmidi.c usbmidi.h test.c -o test -lusb-1.0 -pthread

ins:
	$(shell rmmod midi_top > /dev/null 2>&1; insmod midi_top.ko > /dev/null 2>&1;)

module:
	${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:
	${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
	${RM} test usbmidi.o

TARFILES = Makefile README midi_top.h midi_top.c hello.c
TARFILE = midi_driver_sw.tar.gz
.PHONY : tar
tar : $(TARFILE)

$(TARFILE) : $(TARFILES)
	tar zcfC $(TARFILE) .. $(TARFILES:%=midi_driver_sw/%)

endif 
