# Don't change anything below

include config

CFLAGS :=
MODFLAGS :=

ifeq (.opts,$(wildcard .opts))
  include .opts
  do-it-all: all
else
  do-it-all: opts
endif

opts:
	$(MAKE) -Cscripts
	@echo "**"
	@echo "** Compilation configured for `cat .opts | grep KERNEL_VERSION`, now compile it with:"
	@echo "**   make"
	@echo "**"

EMU10K1_MOD := emu10k1.o
EMU10K1_OBJS := audio.o cardmi.o cardmo.o cardwi.o cardwo.o efxmgr.o emuadxmg.o\
      hwaccess.o irqmgr.o main.o midi.o mixer.o recmgr.o timer.o voicemgr.o\
      ecard.o passthrough.o

AC97_MOD := ac97_codec.o
AC97_OBJS := ac97_codec.o

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

ifeq ($(KERNEL_RELEASE),2.2)
  KERNEL_EXTRA_INCLUDES := -I2.2/
  MODULE_PATH := /lib/modules/$(KERNEL_VERSION)/misc
  AC97_MODULE_PATH := $(MODULE_PATH)
  EMU10K1_OBJS += 2.2/emu_wrapper.o
  EMU10K1_JOY_MOD := emu10k1-joy.o
  EMU10K1_JOY_OBJS := joystick.o 2.2/emu_wrapper.o
else
  ifeq ($(KERNEL_RELEASE),2.3)
    KERNEL_RELEASE := 2.4
  endif
  ifeq ($(KERNEL_RELEASE),2.4)
    KERNEL_EXTRA_INCLUDES := -I2.4/
    MODULE_PATH := /lib/modules/$(KERNEL_VERSION)/kernel/drivers/sound/emu10k1
    AC97_MODULE_PATH := /lib/modules/$(KERNEL_VERSION)/kernel/drivers/sound
    EMU10K1_OBJS += 2.4/emu_wrapper.o
  endif 
endif

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

ifeq ($(DBGEMU),y)
  CFLAGS += -DDBGEMU
endif

ifeq ($(SEQUENCER_SUPPORT),y)
  KERNEL_EXTRA_INCLUDES += -I$(KERNEL_SOURCE)/drivers/sound
  CFLAGS += -DEMU10K1_SEQUENCER
endif

CFLAGS += $(KERNEL_EXTRA_INCLUDES)

OBJS := $(EMU10K1_OBJS) $(EMU10K1_JOY_OBJS)
E_OBJS := $(AC97_OBJS)

include .rules

all: .depend $(EMU10K1_MOD) $(AC97_MOD) $(EMU10K1_JOY_MOD)
	@echo "**"
	@echo "** The driver was succefully compiled, now install it with:"
	@echo "**   make install"
	@echo "**"

.depend: $(OBJS:.o=.c) $(E_OBJS:.o=.c)
	$(CC) $(CFLAGS) $(MODFLAGS) -M $(OBJS:.o=.c) > $@
	$(CC) $(CFLAGS) $(MODFLAGS) -DEXPORT_SYMTAB -M $(E_OBJS:.o=.c) >> $@

$(EMU10K1_MOD): $(EMU10K1_OBJS)
	$(LD) -r $^ -o $@

$(EMU10K1_JOY_MOD): $(EMU10K1_JOY_OBJS)
	$(LD) -r $^ -o $@

install: all
	mkdir -p ${DESTDIR}/$(MODULE_PATH)
	install -c -m 664 $(EMU10K1_MOD) ${DESTDIR}/$(MODULE_PATH)
	install -c -m 664 $(AC97_MOD) ${DESTDIR}/$(AC97_MODULE_PATH)
ifeq ($(KERNEL_RELEASE),2.2)
	install -c -m 664 $(EMU10K1_JOY_MOD) ${DESTDIR}/$(MODULE_PATH)
endif
ifndef DESTDIR
	/sbin/depmod -a $(KERNEL_VERSION)
	/sbin/modprobe -r $(EMU10K1_MOD) $(EMU10K1_JOY_MOD) $(AC97_MOD) 
endif
	@echo "**"
	@echo "** Driver install complete, to compile the tools type \"make tools\""
	@echo "**"	

clean:
	rm -f core .opts `find . -name '*.[oas]' -o -name '*~' \
	-o -name core -o -name .depend`
	cd utils && $(MAKE) clean

notice:
	@echo "Driver for KERNEL VERSION = $(KERNEL_VERSION)"
	@if [ ! -f $(KERNEL_SOURCE)/include/linux/modversions.h ]; then \
		echo "$(KERNEL_SOURCE)/include/linux/modversions.h is missing" ;\
		echo "Please run make dep in your kernel source tree" ;\
		exit 1;\
	fi

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

tools:  
	cd utils && $(MAKE)
	@echo "**"
	@echo "** Now type \"make install-tools\" to install the tools"
	@echo "**"

install-tools: 
	cd utils && $(MAKE) install

