ifeq ($(MAKELEVEL),0)
PWD:=$(shell pwd)
endif

ifndef MACHINE
  MACHINE	:=$(shell uname -m)
endif
# FIXME: this variable sets ARCH in the kernel Makefile.
ARCH		:=$(shell echo $(MACHINE) | sed -e s/i.86/i386/)
BRISTUFFBASE = $(shell dirname `pwd`)
ZAP = $(shell [ -f $(BRISTUFFBASE)/zaptel/zaptel.h ] && echo "-I$(BRISTUFFBASE)/zaptel")
EXTRA_CFLAGS+=$(ZAP)

# If you want to build for a kernel other than the current kernel, set KVERS
ifndef KVERS
KVERS:=$(shell uname -r)
endif
ifndef KSRC
  ifneq (,$(wildcard /lib/modules/$(KVERS)/build))
    KSRC:=/lib/modules/$(KVERS)/build
  else
    KSRC_SEARCH_PATH:=/usr/src/linux-2.4 /usr/src/linux
    KSRC:=$(shell for dir in $(KSRC_SEARCH_PATH); do if [ -d $$dir ]; then echo $$dir; break; fi; done)
  endif
endif
KINCLUDES:=$(KSRC)/include

ifeq (2.6,$(shell echo $(KVERS) | cut -d. -f1-2))
  BUILDVER:=linux26
else
  BUILDVER:=linux24
endif

MODULES:=qozap

MODULESO:=$(MODULES:%=%.o)
MODULESKO:=$(MODULES:%=%.ko)

ifeq ($(BUILDVER),linux26)
MODULESO+=$(SUBDIRS_EXTRA:%=%/)
endif

#NOTE NOTE NOTE
#
# all variables set before the include of Makefile.kernel26 are needed by the 2.6 kernel module build process

ifneq ($(KBUILD_EXTMOD),)

include $(src)/Makefile.kernel26

else

HOSTCC=gcc

INSTALL_PREFIX	:= /usr

CFLAGS+=-I. -O4 -g -fPIC -Wall
ifneq (,$(findstring ppc,$(MACHINE)))
  CFLAGS	+= -fsigned-char
  KFLAGS	+= -msoft-float -fsigned-char
endif
ifneq (,$(findstring x86_64,$(MACHINE)))
  CFLAGS	+= -m64
  KFLAGS	+= -mcmodel=kernel
endif
KFLAGS:=-I$(KINCLUDES) -O6 
KFLAGS+=-DMODULE -D__KERNEL__ -DEXPORT_SYMTAB\
	-Wall -I. -Wstrict-prototypes -fomit-frame-pointer 
ifneq (,$(wildcard $(KINCLUDES)/linux/modversions.h))
  KFLAGS+=-DMODVERSIONS -include $(KINCLUDES)/linux/modversions.h
endif

#
# Features are now configured in zconfig.h
#

KMAKE:= $(MAKE) -C $(KSRC) SUBDIRS=$(PWD)
KMAKE_INST:= $(KMAKE) INSTALL_MOD_PATH=$(DESTDIR) INSTALL_MOD_DIR=misc modules_install

# sample makefile "trace print"
#tracedummy=$(shell echo ====== GOT HERE ===== >&2; echo >&2)

SELINUX_ENABLED	:= $(shell  [ -x /usr/sbin/sestatus ] && (/usr/sbin/sestatus | grep "SELinux status:" | grep -q "enabled"))

all: modules


ifeq ($(BUILDVER),linux24)
modules: $(MODULESO)
else
modules: 
ifeq (,$(wildcard $(KSRC)/.config))
	@echo "You do not appear to have the sources for the $(KVERS) kernel installed (under $(KSRC))."; exit 1
endif
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) HOTPLUG_FIRMWARE=$(HOTPLUG_FIRMWARE) modules
endif


install: all install-modules
	@echo "###################################################"
	@echo "###"
	@echo "### qozap installed successfully."
	@echo "###"
	@echo "###################################################"


# Specific to a kernel version:
install-modules: modules
ifeq ($(BUILDVER),linux26)
	for x in $(MODULESKO); do \
		rm -f $(DESTDIR)/lib/modules/$(KVERS)/extra/$$x ; \
	done
	$(KMAKE_INST)
else
	install -d $(DESTDIR)$(MODS_DIR)
endif
	[ `id -u` = 0 ] && /sbin/depmod -a $(KVERS) || :

clean:
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) clean
	rm -f Module.symvers

.EXPORT_ALL_VARIABLES:

FORCE:

endif

