# Makefile for Vortex au88xx driver
#
# WARNING: Do not modify this file unless you know what you are doing!
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Contact: http://aureal.sourceforge.net
#
#

LSPCI=lspci

ifneq ($(shell $(LSPCI) -d '12eb:3'),)
NAME = au8810
endif
ifneq ($shell ($(LSPCI) -d '12eb:1'),)
NAME = au8820
endif
ifneq ($shell ($(LSPCI) -d '12eb:2'),)
NAME = au8830
endif

PROCESSOR_TYPE = $(shell uname -m)
ifeq ($(PROCESSOR_TYPE),i486)
CFCPU = -m486
endif
ifeq ($(PROCESSOR_TYPE),i586)
CFCPU = -mpentium
endif
ifeq ($(PROCESSOR_TYPE),i686)
CFCPU = -mpentiumpro
endif

KERNEL_VERSION = $(shell uname -r)
KERNEL_VERSION_MINOR = $(shell uname -r | cut -d. -f2)
MODULE_DIR = /lib/modules/$(KERNEL_VERSION)/drivers/sound
ifeq ($(KERNEL_VERSION_MINOR),2)
MODULE_DIR = /lib/modules/$(KERNEL_VERSION)/misc
endif
ifeq ($(KERNEL_VERSION_MINOR),1)
MODULE_DIR = /lib/modules/$(KERNEL_VERSION)/misc
endif

CONF = $(wildcard /etc/conf.modules)
ifeq ($(strip $(CONF)),)
CONF = /etc/modules.conf
endif


CC = gcc

INCLUDE_DIRS = -I/lib/modules/$(KERNEL_VERSION)/build/include
CPPFLAGS = $(INCLUDE_DIRS)

#CFDEBUG = -DDEBUG=1 -DDEBUG_AU -D__VerboseMode -g
CFOPT = -O6 #-fomit-frame-pointer
#CFDEVEL = -Wall -Werror -save-temps
CFUSER = -Wall -pipe
MFLAGS = -D__KERNEL__ -DMODULE -D$(shell echo $(NAME) | tr [a-z] [A-Z])
CFLAGS	= $(MFLAGS) $(CFDEBUG) $(CFCPU) $(CFOPT) $(CFDEVEL) $(CFUSER)

SRCS = $(wildcard *.c)
DEPS = $(SRCS:.c=.dep)
PPCS = $(SRCS:.c=.i)
ASMS = $(wildcard asm/*.s)
OBJS = $(SRCS:.c=.o) $(ASMS:.s=.o)
TGTS = au88*.o


all: compile

install-all: mod_conf

mod_conf: install
	mv -f $(CONF) $(CONF).old
	awk -f mod_conf $(CONF).old > $(CONF)
	echo "alias char-major-14 sound" >> $(CONF)
	echo "alias sound-slot-0 sound" >> $(CONF)
	echo "alias sound $(NAME)" >> $(CONF)
	echo "alias midi $(NAME)" >> $(CONF)

install: compile
	mkdir -p $(MODULE_DIR)
	cp -f $(TGTS) $(MODULE_DIR)
	/sbin/depmod -a

compile: $(DEPS) $(OBJS)
	$(LD) -m elf_i386 -r $(OBJS) -o $(NAME).o

au8810:
	$(MAKE) compile NAME=au8810

au8820:
	$(MAKE) compile NAME=au8820

au8830:
	$(MAKE) compile NAME=au8830

depend dep: $(DEPS)
	@echo "Dependancies are now up to date."

%.dep: %.c
	$(CC) -M $(CPPFLAGS) $< > $@
	$(CC) -M $(CPPFLAGS) $< | sed s/\\.o/.dep/ >> $@

ifneq ($(findstring clean,$(MAKECMDGOALS)),clean)
 -include $(DEPS)
 ifneq ($(findstring au88,$(MAKECMDGOALS)),au88)
  ifeq ($(NAME),)
   $(warning No Aureal Vortex soundcard detected!)
   $(error See the "Troubleshooting" of the README file for help)
  endif
 endif
endif

semiclean:
	-rm -f $(OBJS) $(PPCS) $(DEPS) *~

clean:
	$(MAKE) semiclean
	-rm -f $(TGTS)
