#################################################
# config

# where the kernel sources are located
RUNNING := /lib/modules/$(shell uname -r)/build
KDIR := $(shell if test -d $(RUNNING); then echo $(RUNNING); else echo /usr/src/linux; fi)

# kernel version
CURRENT	:= $(shell ./uts-release $(KDIR))
TREE	:= $(shell echo $(CURRENT) | cut -d. -f1,2)

# conditional stuff
ifeq ($(TREE),2.4)
  KCOMPAT :=
else
  KCOMPAT := kcompat24.o
endif

MODULE_DIR := $(shell test -d "/lib/modules/$(CURRENT)/kernel" && \
	echo "/lib/modules/$(CURRENT)/kernel/drivers/media/video" ||\
	echo "/lib/modules/$(CURRENT)/misc")

EXTRA_CFLAGS=-g -Wmissing-prototypes -Wstrict-prototypes #-DVIDEODAT_HACK


#################################################
# some magic for using linux kernel settings
# when compiling module(s)

# for new-style kernel Makefiles (2.4)
export-objs	:= bttv-if.o $(KCOMPAT)
list-multi	:= bttv.o
bttv-objs	:= bttv-driver.o bttv-cards.o bttv-if.o

obj-m		:= bttv.o btaudio.o tuner.o tvaudio.o tvmixer.o \
		   msp3400.o tda7432.o tda9875.o dpl3518.o $(KCOMPAT)

multi-m		:= $(filter $(list-multi), $(obj-m))
int-m		:= $(sort $(foreach m, $(multi-m), $($(basename $(m))-objs)))

# for old-style kernel Makefiles (2.2)
M_OBJS		:= $(sort $(filter-out $(export-objs), $(obj-m)))
MX_OBJS		:= $(sort $(filter     $(export-objs), $(obj-m)))
MI_OBJS		:= $(sort $(filter-out $(export-objs), $(int-m)))
MIX_OBJS        := $(sort $(filter     $(export-objs), $(int-m)))

here:
	DIR=`pwd`; (cd $(KDIR); make SUBDIRS=$$DIR modules)
	@echo
	@echo '"make list" prints a list of supported cards'

list:
	@echo "bttv.o"
	@grep name: bttv-cards.c | perl -ne '/"([^"]+)"/;printf("  card=%d - %s\n",$$i++,$$1)'
	@echo
	@echo "tuner.o"
	@grep "{ \"" tuner.c | perl -ne '/"([^"]+)"/;printf("  type=%d - %s\n",$$i++,$$1)'

install:
	strip --strip-debug *.o
	su -c "set -x; mkdir -p $(MODULE_DIR); cp -v $(M_OBJS) $(MX_OBJS) $(MODULE_DIR); depmod -a"

clean:
	-rm -f *.o .*.o.flags *~

include $(KDIR)/Rules.make

bttv.o: $(bttv-objs)
	$(LD) $(LD_RFLAG) -r -o $@ $(bttv-objs)

