#
# Makefile for the Linux 2.4 unionfs
#

# this should point to where your kernel headers are
LINUXSRC = /lib/modules/`uname -r`/build
TOPINC   = -I${LINUXSRC}/include
# This is where the make install target will put stuff
PREFIX   = /usr/local
MODPREFIX=
VERSION  = 1.0.10
TAROPTS = czvf
UNIONFS_DEBUG_CFLAG = -g
UNIONFS_OPT_CFLAG= -O2

# allow custmom override of TOPINC for fistgen developers
# These options enable extended attribute support
#  EXTRACFLAGS=-DUNIONFS_XATTR
# Some kernels also require FIST_SETXATTR_CONSTVOID
#  EXTRACFLAGS=-DUNIONFS_XATTR -DFIST_SETXATTR_CONSTVOID
ifneq ($(FISTDEVMK),)
-include $(FISTDEVMK)
else
-include fistdev.mk
endif

CC	= gcc
KERNELVERSION=$(shell uname -r | cut -d. -f1,2)
ifeq ($(KERNELVERSION),2.4)
CFLAGS = -D__KERNEL__ -DMODULE -DFISTGEN -I. ${TOPINC} -Wall -Wno-unused-label -Werror -fno-strict-aliasing ${EXTRACFLAGS} ${UNIONFS_DEBUG_CFLAG} ${UNIONFS_OPT_CFLAG}
endif
EXTRA_CFLAGS += -D__KERNEL__ -DMODULE -DFISTGEN -I. ${TOPINC} -Wall -Werror -fno-strict-aliasing ${EXTRACFLAGS} ${UNIONFS_DEBUG_CFLAG} ${UNIONFS_OPT_CFLAG}
UCFLAGS = -DFISTGEN -I. ${TOPINC} ${UNIONFS_DEBUG_CFLAG} ${UNIONFS_OPT_CFLAG} -Wall -Werror ${EXTRAUCFLAGS}

obj-m := unionfs.o
unionfs-objs := subr.o dentry.o file.o inode.o main.o super.o print.o \
	stale_inode.o branchman.o xattr.o rdstate.o copyup.o  \
	dirhelper.o

BINS	:=	unionctl uniondbg

all:	readthereadme unionfs$(KERNELVERSION)

readthereadme:
	@echo "******************************************************************************"
	@echo "If you have questions about or problems building Unionfs, please read INSTALL."
	@echo "******************************************************************************"
	@echo ""

unionfs2.6: ${BINS}
	make -C ${LINUXSRC} SUBDIRS=$(PWD) FISTDEVMK=$(PWD)/fistdev.mk modules

unionfs2.4: ${obj-m} ${BINS}

${obj-m}: ${unionfs-objs}
	ld -o ${obj-m} -r ${unionfs-objs}

unionctl: unionctl.c
	${CC} -o $@ $? ${UCFLAGS}

uniondbg: uniondbg.c
	${CC} -o $@ $? ${UCFLAGS}

memtest:
	perl match-malloc.pl log.txt

TAGS:
	etags -t *.[hc]

tags:
	ctags *.[ch]

clean:
	rm -f ${unionfs-objs} ${obj-m} ${obj-m:.o=.ko} *.d .*.flags \#* *~
	rm -f ${BINS} TAGS tags
	rm -f unionfs-${VERSION}.tar.gz

${unionfs-objs}: unionfs.h fist.h

install: all
	mkdir -p ${PREFIX}/sbin
	cp unionctl ${PREFIX}/sbin
	cp uniondbg ${PREFIX}/sbin
	mkdir -p ${PREFIX}/man/man4
	mkdir -p ${PREFIX}/man/man8
	cp man/unionfs.4 ${PREFIX}/man/man4
	cp man/unionctl.8 ${PREFIX}/man/man8
	cp man/uniondbg.8 ${PREFIX}/man/man8
	if [ -f unionfs.ko ] ; then cp unionfs.ko ${MODPREFIX}/lib/modules/`uname -r`/kernel/fs ; else cp unionfs.o ${MODPREFIX}/lib/modules/`uname -r`/kernel/fs ; fi

uninstall:
	if [ -f unionfs.ko ] ; then rm /lib/modules/`uname -r`/kernel/fs/unionfs.ko ; else rm /lib/modules/`uname -r`/kernel/fs/unionfs.o ; fi
	rm ${PREFIX}/man/man8/unionctl.8
	rm ${PREFIX}/man/man8/uniondbg.8
	rm ${PREFIX}/man/man4/unionfs.4
	rm ${PREFIX}/sbin/unionctl
	rm ${PREFIX}/sbin/uniondbg

release: unionfs-${VERSION}.tar.gz

unionfs-${VERSION}.tar.gz:
	mkdir unionfs-${VERSION}
	cp COPYING AUTHORS README INSTALL NEWS ChangeLog doit.sh undoit.sh *.pl Makefile *.[ch] misc/snapmerge unionfs-${VERSION}
	cp -r rpm debian unionfs-${VERSION}
	rm -rf unionfs-${VERSION}/rpm/CVS
	rm -rf unionfs-${VERSION}/debian/CVS
	mkdir unionfs-${VERSION}/regression
	cp ../regression/*.sh ../regression/ChangeLog ../regression/scaffold ../regression/README unionfs-${VERSION}/regression
	mkdir unionfs-${VERSION}/man
	cp man/*.[0-9] unionfs-${VERSION}/man
	tar ${TAROPTS} unionfs-${VERSION}.tar.gz unionfs-${VERSION}
	rm -rf unionfs-${VERSION}
