ifneq ($(KERNELRELEASE),)   # We were called by kbuild
CFLAGS += $(DEFINES) -g
obj-$(CONFIG_ZC030X) += zc030x.o
zc030x-objs := zc030x_main.o zc030x_i2c.o zc030x_matrix.o zc030x_mm.o zc030x_isochron.o zc030x_v4l.o zc030x_cameras.o zc030x_jpeg.o zc030x_fp.o zc030x_reg.o

else   # We were called from command line

ifeq ($(shell uname -r | cut -d. -f1,2), 2.6)

KERNEL_VERSION = `uname -r`
KERNELDIR := /lib/modules/$(KERNEL_VERSION)/build
PWD  := $(shell pwd)
MODULE_INSTALLDIR = /lib/modules/$(KERNEL_VERSION)/kernel/drivers/usb/media/

default:
	@echo '   Building ZC030X driver for 2.5/2.6 kernel.'
	@echo '   PLEASE IGNORE THE "Overriding SUBDIRS" WARNING'
	@echo '   Remember: you must have read/write access to your kernel source tree.'
	$(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) modules
install:
	install -c -m 0644 zc030x.ko $(MODULE_INSTALLDIR)
	/sbin/depmod -ae
uninstall:
	rm -f $(MODULE_INSTALLDIR)/zc030x.ko
	/sbin/depmod -aq
clean:
	rm -f *.o zc030x.ko
	rm -f .*.cmd 
	rm -f zc030x.mod.o
	rm -rf .tmp_versions
else
#makefile2.4

KERNEL_VERSION = `uname -r`

###
# Location of the header files (most importantly the config files)
# # for the kernel you want to build the module against.
# # This should be correct for the currently installed kernel on your machine.
KINCLUDE   = /lib/modules/$(KERNEL_VERSION)/build/include
KERNEL_ACFILE = $(KINCLUDE)/linux/autoconf.h
KERNEL_MODVERSIONSFILE = $(KINCLUDE)/linux/modversions.h
MODULE_INSTALLDIR = /lib/modules/$(KERNEL_VERSION)/kernel/drivers/usb/

# Detect module versioning support
ifneq ($(strip $(shell grep 'define CONFIG_MODVERSIONS 1' $(KERNEL_ACFILE))),)
       DEFINES += -DMODVERSIONS -include $(KERNEL_MODVERSIONSFILE)
endif
#
# Detect SMP support
ifneq ($(strip $(shell grep 'define CONFIG_SMP 1' $(KERNEL_ACFILE))),)
       DEFINES += -D__SMP__ -DSMP
endif

# Setup the tools
CC         = gcc
LD         = ld

# Setup compiler warnings
WARNINGS   = -Wall -Wpointer-arith
WARNINGS  += -Wcast-align -Wwrite-strings -Wstrict-prototypes
WARNINGS  += -Wuninitialized -Wreturn-type -Wunused -Wparentheses

# Setup compiler flags
CFLAGS     = -O2 -g -fomit-frame-pointer -fno-strict-aliasing -pipe -msoft-float 
CFLAGS    += -mpreferred-stack-boundary=2 
CFLAGS    += -I$(KINCLUDE) -Idrivers/usb
# what are following options for ??
CFLAGS	+= -nostdinc -iwithprefix include -DMODULE -D__KERNEL__ -DNOKERNEL

# Setup link flags
LDFLAGS    = -r

OBJS       = zc030x_main.o \
             zc030x_i2c.o \
             zc030x_matrix.o \
             zc030x_mm.o \
             zc030x_v4l.o \
             zc030x_cameras.o \
             zc030x_jpeg.o \
             zc030x_fp.o \
             zc030x_reg.o \
             zc030x_isochron.o
             
#
#	     zc030x_proc.o \
#	     zc030x_legacy_init.o
	     
BINARY     = zc030x.o

###
# Targets follow here

binary:	$(OBJS)
	@echo Linking $(BINARY)
	@$(LD) $(LDFLAGS) -o $(BINARY) $(OBJS)

install: binary
	@echo Installing.. Your root password may be required.
	su -c "make install-root"

install-root:
	install -c -m 0644 zc030x.o $(MODULE_INSTALLDIR)
	/sbin/depmod -ae

.c.o:	Makefile $*.c
	@echo Compiling $*.c
	@$(CC) $(CFLAGS) $(WARNINGS) $(DEFINES) -c $*.c -o $*.o

###
# Dependencies follow here
#
zc030x_main.o: zc030x_kerneldef.h \
               zc030x_cameras.h \
               zc030x_i2c.h \
               zc030x_sensorsig.h \
               zc030x.h \
               zc030x_mm.h \
               zc030x_isochron.h \
               zc030x_frame.h \
               zc030x_fp.h \
               zc030x_cameras.h \
               ./sensors/CS2102.h \
               ./sensors/CS2103.h \
               ./sensors/HDCS1020.h \
               ./sensors/HDCS2020.h \
               ./sensors/HV7121B.h \
               ./sensors/HV7131B.h \
               ./sensors/ICM102A.h \
               ./sensors/ICM105A.h \
               ./sensors/OV7620.h \
               ./sensors/OVCIF.h \
               ./sensors/PAS106B.h \
               ./sensors/PAS202B.h \
               ./sensors/PB0111.h \
               ./sensors/PB0330.h \
               ./sensors/TAS5130C.h \
               ./sensors/TAS5110B.h

zc030x_i2c.o:  zc030x_i2c.h \
               zc030x_kerneldef.h \
               zc030x.h

zc030x_matrix.o: zc030x_matrix.h \
               zc030x_kerneldef.h

zc030x_mm.o:   zc030x_mm.h \
               zc030x_kerneldef.h

zc030x_isochron.o:   zc030x_isochron.h \
               zc030x_kerneldef.h

zc030x_fp.o: zc030x_fp.h

zc030x_v4l.o: zc030x_v4l.h \
              zc030x_kerneldef.h 

zc030x_cameras.o: zc030x_cameras.h \
                  zc030x_kerneldef.h

zc030x_jpeg.o: zc030x_jpeg.h \
               zc030x_frame.h \
               zc030x_kerneldef.h

zc030x_reg.o:  zc030x_reg.h \
               zc030x_kerneldef.h \

############################################################################## 
# OTHER TARGETS 
##############################################################################
clean:
	rm -f *.o zc030x.ko
	rm -f .*.cmd 
	rm -f zc030x.mod.o
	rm -rf .tmp_versions
	
############################################################################## 

endif #2.6 <-> 2.4

endif #kbuild <-> commandline
readframe:
	gcc -o readframe readimage.c

detectsensor:
	gcc -o detectsensor detectsensor.c

package:
	pwd | awk -F / '{printf("%s", $$(NF));}'> /tmp/$$stupidname$$
	(cd .. && tar -jcvf zc030x_new.tar.bz2 `cat /tmp/$$stupidname$$` --exclude=CVS --exclude=*.o --exclude=*.ko --exclude=*.init  && rm -f /tmp/$$stupidname$$)


