ifneq ($(KERNELRELEASE),)
# call from kernel build system

obj-m := shfs.o

shfs-objs := dcache.o dir.o fcache.o file.o inode.o ioctl.o proc.o shell.o symlink.o

else
# external module build

ifndef KERNEL
KERNEL=$(shell uname -r)
endif

ifndef MODULESDIR
MODULESDIR=${ROOT}/lib/modules/${KERNEL}
endif

ifndef KERNEL_SOURCES
KERNEL_SOURCES=${MODULESDIR}/build
endif

LINVER := linux-${KERNEL}
PWD := $(shell pwd)

default:
	$(MAKE) -C $(KERNEL_SOURCES) SUBDIRS=$(PWD) modules

clean: patch-clean
	rm -f *.o *.ko *.mod.c .*o.cmd

install: shfs.ko
	rm -f ${MODULESDIR}/kernel/fs/shfs/shfs.ko
	install -m644 -b -D shfs.ko ${MODULESDIR}/kernel/fs/shfs/shfs.ko
	if [ -x /sbin/depmod -a "${ROOT}" = "/" ]; then /sbin/depmod -aq; fi

uninstall:
	rm -rf ${MODULESDIR}/kernel/fs/shfs
	if [ -x /sbin/depmod -a "${ROOT}" = "/" ]; then /sbin/depmod -aq; fi

patch:
	rm -rf ${LINVER} ${LINVER}.orig; mkdir ${LINVER};
	for i in fs/shfs include/linux; do \
	  mkdir -p ${LINVER}/$$i; \
	done
	cp ${KERNEL_SOURCES}/fs/{Makefile,Kconfig} ${LINVER}/fs
	cp -r ${LINVER} ${LINVER}.orig
	cp ../../Changelog Makefile *.c shfs_debug.h proc.h ${LINVER}/fs/shfs/
	cp shfs.h shfs_fs* ${LINVER}/include/linux/
	(cd ${LINVER}; patch -p1 <../kernel-config.diff)
	find . -type f -name "*.orig" -print | xargs rm -f
	diff -urN ${LINVER}.orig ${LINVER} >${LINVER}.diff; true

patch-clean:
	rm -rf ${LINVER} ${LINVER}.orig;
	rm -f ${LINVER}.diff
	
endif

.PHONY : all tidy clean install uninstall patch patch-clean
