#
# Transparent Cryptopgraphic File System
#
#                   by: Giuseppe  Cattaneo
#                       Giuseppe  Persiano
#                       Angelo    Celentano
#                       Andrea    Cozzolino
#                       Aniello   Del Sorbo
#                       Ermelindo Mauriello
#                       Raffaele  Pisapia
#

# -+-_==

#
# Please make no change to the lines below unless you know what are you doing
#

.EXPORT_ALL_VARIABLES:

CC = gcc 
AR = ar
RANLIB = ranlib
RPCGEN = rpcgen -C
SHELL = /bin/sh
INSTALL = /usr/bin/install

DEFS = $(DEFINES) -DHAVE_CONFIG_H -D__linux__ 
CFLAGS = -w -O2 
LDFLAGS = -O2 
WARNFLAGS = 
RPC_WARNFLAGS = -Wno-unused -Wno-switch -Wno-uninitialized
TRANSPORTFLAGS = -I -s udp -s tcp

LIBS = libns.a

LOADLIBES= -lsecude

LIBSRCS	= version.c getopt.c getopt1.c eaccess.c \
          fsusage.c mountlist.c xmalloc.c xstrdup.c \
          nfsmounted.c

MOUNTSRCS = fstab.c sundries.c tcfsmount.c \
            version.c stick.c adm.c nfsmount.c \
            $(MOUNTXSRCS)            

UMOUNTSRCS = tcfsumount.c fstab.c version.c \
             sundries.c $(UMOUNTXSRCS)

MOUNTDSRCS = mountd.c temporaneo.c tcfs_adm.c \
             $(COMMONSRCS) $(MOUNTDXSRCS)

COMMONSRCS = logging.c fh.c auth_init.c auth_clnt.c 

TCFSDSRCS = tcfsd.c dispatch.c getattr.c tcfs_adm.c \
           tcfs_server.c tcfscrypt.c d3des.c uid.c \
           cryptname.c tcfsencode.c tcfsdecode.c \
           $(COMMONSRCS) $(TCFSDXSRCS)

TCFSDXSRCS = tcfs_prot_xdr.c mount_xdr.c xattr_xdr.c 

MOUNTDXSRCS = mount_svc.c mount_xdr.c

MOUNTXSRCS = mount_clnt.c tcfs_prot_xdr.c

UMOUNTXSRCS = mount_xdr.c 

RPCGENERED = tcfs_prot_xdr.c tcfs_prot.h xattr_xdr.c \
             xattr.h mount_xdr.c mount_clnt.c \
             mount.h mount_svc.c

TARGETS = libns.a tcfsmount tcfsumount mountd tcfsd 

#----------------------- MAIN section -------------------------------------

.PHONY:	all utils

all: $(TARGETS) utils

utils:
	$(MAKE) -C utils all

LIBNS_OBJS = $(patsubst %, libns.a(%), $(patsubst %.c, %.o, $(LIBSRCS)))
MOUNT_OBJS = $(patsubst %.c, %.o, $(MOUNTSRCS))
UMOUNT_OBJS = $(patsubst %.c, %.o, $(UMOUNTSRCS))
MOUNTD_OBJS = $(patsubst %.c, %.o, $(MOUNTDSRCS))
TCFSD_OBJS = $(patsubst %.c, %.o, $(TCFSDSRCS))

libns.a: $(LIBNS_OBJS)
	$(RANLIB) $@

tcfsmount: $(MOUNT_OBJS) $(LIBS)

tcfsumount: $(UMOUNT_OBJS) $(LIBS)

mountd: $(MOUNTD_OBJS) $(LIBS)

tcfsd: $(TCFSD_OBJS) $(LIBS)

#----------------------- DEPEND section -----------------------------------

adm.c: tcfs_prot.h mount.h tcfs.h 
auth_clnt.c: $(NFSDH)
cryptname.c: defines.h tcfscode.h uid.h d3des.h 
d3des.c: d3des.h 
dispatch.c: $(NFSDH) 
eaccess.c: config.h
fh.c: $(NFSDH) tcfs.h tcfs_prot.h xattr.h mount.h 
fstab.c: fstab.h 
fsusage.c: fsusage.h config.h
getattr.c: $(NFSDH) 
getopt.c: config.h getopt.h 
getopt1.c: config.h getopt.h 
logging.c: $(NFSDH) 
mountd.c: $(NFSDH) getopt.h tcfs.h 
mountlist.c: mountlist.h config.h 
nfsmount.c: mount.h $(SUNDRIES)
nfsmounted.c: config.h
tcfs_adm.c: mount.h tcfs_prot.h tcfs.h 
tcfs_server.c: uid.h 
tcfscrypt.c: uid.h d3des.h defines.h
tcfsd.c: $(NFSDH) xattr.h getopt.h fsusage.h tcfs.h mount.h tcfscrypt.h cryptname.h 
tcfsmount.c: $(SUNDRIES) mount.h tcfs_prot.h
tcfsumount.c: tcfs_prot.h $(SUNDRIES) mount.h 
stick.c: tcfs_prot.h tcfs.h mount.h 
sundries.c: $(SUNDRIES) 
uid.c: uid.h 
xmalloc.c: config.h
xstrdup.c: config.h

NFSDH = nfsd.h mount.h system.h auth.h fh.h logging.h tcfs_prot.h
SUNDRIESH = sundries.h fstab.h

#----------------------- RPCGEN section -----------------------------------

MOUNTXSRCS = mount_clnt.c tcfs_prot_xdr.c 

tcfs_prot_xdr.c: tcfs_prot.x tcfs_prot.h
	rm -f $@
	$(RPCGEN) -c tcfs_prot.x | \
		sed 's;^#include ".*/tcfs_prot.h"$$;#include "tcfs_prot.h";' >$@

tcfs_prot.h: tcfs_prot.x
	rm -f $@
	$(RPCGEN) -h -o $@ tcfs_prot.x

xattr_xdr.c: xattr.x xattr.h
	$(RPCGEN) -c xattr.x | \
                sed -e 's;^#include ".*/mount.h"$$;#include "mount.h";' >$@

xattr.h: xattr.x
	$(RPCGEN) -h -o xattr.h xattr.x

mount_xdr.c: mount.x mount.h
	rm -f $@
	$(RPCGEN) -c mount.x | \
		  sed 's;^#include ".*/mount.h"$$;#include "mount.h";' >$@

mount_clnt.c: mount.x mount.h
	rm -f $@
	$(RPCGEN) -c mount.x | \
		  sed 's;^#include ".*/mount.h"$$;#include "mount.h";' >$@

mount_svc.c: mount.x mount.h
	rm -f $@
	$(RPCGEN) $(TRANSPORTFLAGS) mount.x | \
		sed -e 's/main/mountd_&/' \
		    -e 's/static int _rpc/int _rpc/' \
		    -e 's;^#include ".*/mount.h"$$;#include "mount.h";' >$@
	if grep 'int _rpcfdtype' $@ >/dev/null; then \
		:; \
	else \
		echo 'int _rpcfdtype;' >tmp; \
		cat $@ >>tmp; \
		mv tmp $@; \
	fi

mount.h: mount.x
	rm -f $@
	$(RPCGEN) -h -o $@ mount.x

mount_xdr.o: mount_xdr.c
	$(COMPILE) $(RPC_WARNFLAGS) -c mount_xdr.c

mount_svc.o: mount_svc.c
	$(COMPILE) $(RPC_WARNFLAGS) -c mount_svc.c

#----------------------- ADMIN section ------------------------------------

clean: 
	rm -f *.o *~ $(TARGETS) $(RPCGENERED)
	$(MAKE) -C utils clean
	$(MAKE) -C man clean

install: all
	$(INSTALL) -m 755 -o $(OWNER) -g $(GROUP) -d $(SBINDIR)
	$(INSTALL) -s -m 500 -o $(OWNER) -g $(GROUP) mountd tcfsd tcfsmount tcfsumount $(SBINDIR)
	$(MAKE) -C utils install
	$(MAKE) -C man install

#----------------------- RULES section ------------------------------------

COMPILE = $(CC) -c $(CPPFLAGS) $(DEFS) -I. -I/usr/include/isode $(CFLAGS) $(WARNFLAGS)

.c.o:
	$(COMPILE) $<

