# Comment/uncomment the following line to disable/enable build options
DEBUG = n
KERNELVERSION = auto
KERNELSOURCE=/usr/src/linux

ifneq ($(shell grep '__rh_kernel_version_h__' $(KERNELSOURCE)/include/linux/version.h),)
# redhat kernel headers style
  SUB := $(shell grep __BOOT_KERNEL_ /boot/kernel.h 2>/dev/null | grep 1 | cut -d\_ -f5 | tr [:upper:] [:lower:] | cut -d' ' -f1)
  VERSION_H := version-$(SUB).h
  AUTOCONF_H := autoconf-$(SUB).h
else
  VERSION_H := version.h
  AUTOCONF_H := autoconf.h
endif

ifeq ($(KERNELVERSION),auto)
  KERNELVERSION := $(shell grep UTS_RELEASE $(KERNELSOURCE)/include/linux/$(VERSION_H) 2>/dev/null | cut -d\" -f2)
endif

KERNELRELEASE := $(shell echo $(KERNELVERSION) | cut -d\" -f2| cut -c-3)

ifeq ($(KERNELRELEASE),2.2)
  KERNEL_EXTRA_INCLUDES := -I2.2/
else
  ifeq ($(KERNELRELEASE),2.3)
    KERNELRELEASE := 2.4
    KERNEL_EXTRA_INCLUDES := -I./
  endif
endif

# extract config options from the kernel headers
dummy := $(shell grep 'define' $(KERNELSOURCE)/include/linux/$(AUTOCONF_H) | grep 'CONFIG' | awk '{ print $$2 "=y"}' - > .config)

include $(KERNELRELEASE)/Makefile

CFLAGS += -DPRIVATE_PCM_VOLUME

ifeq ($(DEBUG),y)
  CFLAGS := $(CFLAGS) -DEMU10K1_DEBUG
endif

CFLAGS := $(CFLAGS) $(MODFLAGS) $(KERNEL_EXTRA_INCLUDES)

TARGET = emu10k1
OBJS = $(TARGET).o
SRC = $(wildcard *.c)

all: .depend $(TARGET).o

$(TARGET).o: $(SRC:.c=.o)
	$(LD) -r $^ -o $@
	@chmod a+r $(TARGET).o

install:
	mkdir -p /lib/modules/$(KERNELVERSION)/misc
	install -c $(TARGET).o /lib/modules/$(KERNELVERSION)/misc
	/sbin/depmod -a $(KERNELVERSION)
	/sbin/modprobe -r emu10k1

clean:
	rm -f core .config `find . -name '*.[oas]' -o -name '*~' -o -name core -o -name dep -o -name .depend`

depend .depend dep:
	@if [ ! -f $(KERNELSOURCE)/include/linux/modversions.h ]; then \
		echo "Please run make dep in your kernel source tree modversions.h is missing" ;\
		exit 1; fi
	@echo "Driver for KERNELVERSION = $(KERNELVERSION)"
	$(CC) $(CFLAGS) -M $(SRC) > $@

ifeq (.depend,$(wildcard .depend))
include .depend
endif

