ifeq (/usr/src/linux/.config,$(wildcard /usr/src/linux/.config))
include /usr/src/linux/.config
else
ifeq (/usr/include/linux/modversions.h,$(wildcard /usr/include/linux/modversions.h))
CONFIG_MODVERSIONS = 1
endif

SMPTEST := $(shell echo 'CONFIG_SMP' | gcc -E -include /usr/include/linux/autoconf.h - | tail -1 | sed -e 's/CONFIG_SMP//' -e 's,",,g' -e 's,[      ]*,,g')

ifneq ($(SMPTEST),)
CONFIG_SMP = 1
endif
endif
KVERS := $(shell uname -r)
IVERS := $(shell echo 'UTS_RELEASE' | gcc -E -include /usr/include/linux/version.h - | tail -1 | sed -e 's,",,g' -e 's,[ 	]*,,g' || uname -r)
MODFLAGS = -DMODULE

ifdef CONFIG_MODVERSIONS
MODFLAGS += -DMODVERSIONS -include /usr/include/linux/modversions.h
endif
SMP=
ifdef CONFIG_SMP
MODFLAGS += -D__SMP__
SMP=smp
endif

HEADERS = ib_linux.h cib.h ugpib.h ibconf.h ib_linux.h ib_ni.h
IBLINUX = ib_linux-$(IVERS)$(SMP).o

CC=gcc
# These flags are only used for the driver object
CPPFLAGS= -D__GPIBDRIVER__ 
CFLAGS= -c -O -D__KERNEL__ $(MODFLAGS)

all::	pretest nigpib.o

pretest::	
	@if test $(IVERS) != $(KVERS) ; then \
		echo "ERROR! Running kernel $(KVERS) doesn't match kernel include files ($(IVERS))."; \
		echo "Please fix your kernel include files or reboot to the appropriate kernel."; \
		echo "To bypass this warning, type 'make nigpib.o' directly."; \
		rm -f nigpib.o; \
		exit 1; \
	fi

nigpib.o: ib_ni.o $(IBLINUX)
	ld -r -o nigpib.o  ib_ni.o $(IBLINUX)
	cp nigpib.o nigpib-$(IVERS)$(SMP).o

install: nigpib.o
	@test -d "/lib/modules/$(IVERS)/misc" || \
	(echo "Cannot find modules directory"; exit 1)
	cp nigpib.o /lib/modules/$(IVERS)/misc
	@if test -f /etc/conf.modules; then \
		grep 'alias char-major-31 nigpib' /etc/conf.modules >/dev/null || echo 'alias char-major-31 nigpib' >> /etc/conf.modules ; \
		depmod -a
	else \
		echo "Please add an 'insmod nigpib' to your system startup scripts or enable autoloading with kerneld." ; \
	fi


prebuilt::
	../makevers 2.0.35 0
	../makevers 2.0.36 0
	../makevers 2.2.5 0
	../makevers 2.2.5 1
	../makevers 2.2.10 0
	../makevers 2.2.10 1
	../makevers 2.2.11 0
	../makevers 2.2.11 1

#
# Generate the driver object from the source files.
#
$(IBLINUX): ib_linux.c $(HEADERS)
	$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDE) -o $(IBLINUX) ib_linux.c

#
# Generate the language interface object from the source files.
#
cib.o: $(CIB_SOURCE)
	$(CC) $(INCLUDE) -g -c cib.c -o cib.o

#
# Remove all object files and the driver.
#
clean:
	rm -f $(IBLINUX) ib_linux-*.o nigpib*.o
