ifndef CONFIG_SPADFS_FS

#KERNELDIR := /usr/src/linux-$(shell uname -r)/
#KERNELDIR := /usr/src/linux/
KERNELDIR := /lib/modules/$(shell uname -r)/build

all : spadfs.module mkspadfs spadfsck

spadfs.module :
	$(MAKE) -C $(KERNELDIR) M=`pwd` STANDALONE_BUILD=y CONFIG_SPADFS_FS=m

CC := gcc
SPADFS_CFLAGS := -Wall -W -Wno-unused-parameter -Wno-sign-compare -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -O2 -fno-strict-aliasing -g -fomit-frame-pointer -shared-libgcc $(CFLAGS)
#CC := icc
#SPADFS_CFLAGS := -no-ansi-alias -ipo $(CFLAGS)

%.o : %.c
	$(CC) $(SPADFS_CFLAGS) -c -o $@ -x c $<

mkspadfs : \
	utils/mounted.o \
	utils/mkspadfs/mkspadfs.o
	$(CC) $(SPADFS_CFLAGS) -o $@ $^

spadfsck : \
	utils/common.o \
	utils/mounted.o \
	utils/spadfsck/sck.o \
	utils/spadfsck/sckalloc.o \
	utils/spadfsck/sckapage.o \
	utils/spadfsck/sckbuf.o \
	utils/spadfsck/sckcct.o \
	utils/spadfsck/sckcrt.o \
	utils/spadfsck/sckdir.o \
	utils/spadfsck/sckea.o \
	utils/spadfsck/sckfblk.o \
	utils/spadfsck/sckfile.o \
	utils/spadfsck/sckfn.o \
	utils/spadfsck/sckfxfn.o \
	utils/spadfsck/sckhdlnk.o \
	utils/spadfsck/sckino.o \
	utils/spadfsck/scklog.o \
	utils/spadfsck/sckmass.o \
	utils/spadfsck/sckrcv.o \
	utils/spadfsck/scksuper.o \
	utils/spadfsck/sckutil.o \
	utils/spadfsck/sckxl.o
	$(CC) $(SPADFS_CFLAGS) -o $@ $^

clean :
	-$(MAKE) -C $(KERNELDIR) M=$$PWD clean
	-rm -f *.ko *.o */*.o */*/*.o mkspadfs spadfsck Module.markers Modules.symvers modules.builtin modules.order .*.cmd *.mod.c

install : install-kernel install-userspace

install-kernel : spadfs.module
	$(MAKE) -C $(KERNELDIR) M=`pwd` STANDALONE_BUILD=y modules_install
	depmod -a

install-userspace : mkspadfs spadfsck
	install mkspadfs spadfsck /sbin/
	-strip /sbin/mkspadfs
	-strip /sbin/spadfsck
	ln -sf mkspadfs /sbin/mkfs.spadfs
	ln -sf spadfsck /sbin/fsck.spadfs

else

ifdef M
include $(M)/Kbuild
else
include fs/spadfs/Kbuild
endif

endif
