###############################################################################
#
#    Makefile,v : Makefile for iod directory
#    Copyright (C) 1995 A. Bode, S. Lamberts, T. Ludwig, C. R"oder
#
#    PFSLib (Parallel I/O on workstations)
#
#    PFSLib offers parallel access to files for a parallel application
#    running on a cluster of workstations.
#    It is intended but not restricted to be used in message passing
#    applications based on PVM, NXLib, MPI, and other.
#
#    PFSLib consists of a LIBRARY, deamon PROGRAMS, and utility PROGRAMS.
#
#    PFSLib is free software; you can redistribute the LIBRARY and/or
#    modify it under the terms of the GNU Library General Public
#    License as published by the Free Software Foundation; either
#    version 2 of the License, or (at your option) any later version.
#    You can redistribute the daemon PROGRAMS and utility PROGRAMS
#    and/or modify them under the terms of the GNU General Public
#    License as published by the Free Software Foundation; either
#    version 2 of the License, or (at your option) any later version.
#
#    PFSLib is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Library General Public License and GNU General Public License 
#    for more details.
#
#    You should have received a copy of the GNU Library General Public
#    License and the GNU General Public License along with this
#    library; if not, write to the Free 
#    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#    Contact to the authors:
#
#    electronic mail: pfslib@informatik.tu-muenchen.de
#
#    paper mail:      Prof. Dr. A. Bode
#                     Lehrstuhl f"ur Rechnertechnik und Rechnerorganisation
#                     Institut f"ur Informatik
#                     Technische Universit"at M"unchen
#                     80290 M"unchen
#                     Germany
#
#    This project was partially funded by a research grant form Intel
#    Corporation. 
#
###############################################################################

###############################################################################
#
#  RCS Filename : Makefile,v
#  RCS Date     : 1995/11/30 16:20:58
#  RCS Revision : 1.11
#  RCS Author   : lamberts
#
#  Authors: Stefan Lamberts, Christian R"oder
#
###############################################################################


##############################################################################
# Macros                                                                     #
##############################################################################

SHELL		= /bin/sh

INCLUDEPATH	= ../include
PFSDPATH	= ../pfsd

RPCGEN		= rpcgen

ARCH		= SUN4
ANSI_C		= -DANSI_C
CC		= gcc
CFLAGS		= -ansi -g
CPPFLAGS	= -I$(INCLUDEPATH) -I$(PFSDPATH) $(ANSI_C) -D$(ARCH) $(ADDCPPFLAGS)

LDFLAGS		=
LDLIBS		= -lrpcsvc

LINT		= lint
LINTFLAGS	= -a -b -c -h 
# lint option to create .ln file
LINT_ln		= -i

RM		= rm -f

##############################################################################
# Suffixes                                                                   #
##############################################################################

.SUFFIXES: .ln $(SUFFIXES)

.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<

.c.ln:
	$(LINT) $(LINTFLAGS) $(CPPFLAGS) $(LINT_ln) $<

##############################################################################
# Files generated by rcpgen                                                  #
##############################################################################

RPCFILES	=\
	iod_svc.c\
	iod_xdr.c\
	iod_clnt.c\
	iod.h

##############################################################################
# iod server object files generated by rpcgen                                #
##############################################################################

RPCOBJ		=\
	iod_svc.o\
	iod_xdr.o

RPCLNOBJ	= $(RPCOBJ:.o=.ln)

##############################################################################
# iod client object files generated by rpcgen                                #
##############################################################################

CLTOBJ		=\
	iod_clnt.o\
	iod_xdr.o

CLTLNOBJ	= $(CLTOBJ:.o=.ln)

##############################################################################
# iod server files                                                           #
##############################################################################

SVCSRC		=\
	iod_pfsdrpc.c\
	iod_exit.c\
	iod_op.c\
	iod_print.c\
	iod_req.c

SVCOBJ		= $(SVCSRC:.c=.o)

SVCLNOBJ	= $(SVCSRC:.c=.ln)

##############################################################################
# pfsd client object files generated by rpcgen                               #
##############################################################################

PFSDOBJ		=\
	$(PFSDPATH)/pfsd_clnt.o\
	$(PFSDPATH)/pfsd_xdr.o

##############################################################################
# all object files of iod server                                             #
##############################################################################

ALLSVCOBJ	= $(SVCOBJ) $(RPCOBJ) $(PFSDOBJ)

ALLSVCLNOBJ	= $(ALLSVCOBJ:.o=.ln)

##############################################################################
# Main targets                                                               #
##############################################################################

all: iod_client iod_server

iod_client: iod.h $(CLTOBJ)

iod_server: iod

rpc: $(RPCFILES)

##############################################################################
# Dependencies and rules                                                     #
##############################################################################

iod: $(ALLSVCOBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(ALLSVCOBJ) $(LDLIBS)

##########################
# rpcgen generated files #
##########################

iod.h: iod.x $(INCLUDEPATH)/global_rpc.h
	@if { uname -sr | grep "SunOS 4.1" > /dev/null; };\
	then\
		echo "Making $@";\
		$(RPCGEN) -h -T -o $@ iod.x;\
	else\
		echo "Unable to make $@; rpcgen must be run on SunOS 4.1.x";\
		exit 1;\
	fi

iod_svc.c: iod.x $(INCLUDEPATH)/global_rpc.h
	@if { uname -sr | grep "SunOS 4.1" > /dev/null; };\
	then\
		echo "Making $@";\
		$(RPCGEN) -s udp -s tcp iod.x |\
		sed -e '/!svc_sendreply/,/^	}$$/c\
		\
		/* BEGIN PATCH done by by Makefile */\
		/* Send reply only if resul != NULL\
		 * otherwise reply has to be delayed and sent by _do_op()\
		 */\
		if (result != NULL) {\
		  if (!svc_sendreply(transp, xdr_result, result)) {\
		    svcerr_systemerr(transp); }}\
		/* END PATCH */\
		'\
		-e '/switch/i\
		\
		/* BEGIN PATCH done by Makefile */\
		if (!auth_ok(rqstp,transp)) return;\
		/* END PATCH */\
		' > $@;\
	else\
		echo "Unable to make $@; rpcgen must be run on SunOS 4.1.x";\
		exit 1;\
	fi

iod_svc.ln \
iod_svc.o:\
	iod.h\
	$(INCLUDEPATH)/global.h\
	$(INCLUDEPATH)/auth_ok.c

iod_clnt.c: iod.x $(INCLUDEPATH)/global_rpc.h
	@if { uname -sr | grep "SunOS 4.1" > /dev/null; };\
	then\
		echo "Making $@";\
		$(RPCGEN) -l iod.x |\
		sed -e '/IOD_REQUEST/s/xdr_void, &res, /NULL, NULL, Z/'\
		 > $@;\
	else\
		echo "Unable to make $@; rpcgen must be run on SunOS 4.1.x";\
		exit 1;\
	fi

iod_clnt.ln \
iod_clnt.o:\
	iod.h\
	$(INCLUDEPATH)/global.h

iod_xdr.c: iod.x $(INCLUDEPATH)/global_rpc.h
	@if { uname -sr | grep "SunOS 4.1" > /dev/null; };\
	then\
		echo "Making $@";\
		$(RPCGEN) -c -o $@ iod.x;\
	else\
		echo "Unable to make $@; rpcgen must be run on SunOS 4.1.x";\
		exit 1;\
	fi

iod_xdr.ln \
iod_xdr.o:\
	iod.h\
	$(INCLUDEPATH)/global.h

########################
# Regular source files #
########################

iod_exit.ln \
iod_exit.o:

iod_op.ln \
iod_op.o:\
	iod.h\
	$(INCLUDEPATH)/global.h\
	iod_defines.h\
	$(INCLUDEPATH)/pfslib_errno.h

iod_pfsdrpc.ln \
iod_pfsdrpc.o:\
	$(PFSDPATH)/pfsd.h\
	$(INCLUDEPATH)/global.h

iod_req.ln \
iod_req.o:\
	iod.h\
	$(INCLUDEPATH)/global.h\
	iod_defines.h

##############################################################################
# targets in other directories                                               #
##############################################################################

$(PFSDPATH)/pfsd_xdr.o \
$(PFSDPATH)/pfsd_xdr.ln \
$(PFSDPATH)/pfsd_clnt.o \
$(PFSDPATH)/pfsd_clnt.ln:
	@echo "Making $@ in directory $(PFSDPATH)"
	@(\
		cd $(PFSDPATH);\
		target=`echo $@ | sed -e 's%$(PFSDPATH)/%%'`;\
		$(MAKE) $(MFLAGS)\
			"ARCH=$(ARCH)"\
			"ANSI_C=$(ANSI_C)"\
			"CFLAGS=$(CFLAGS)"\
			"ADDCPPFLAGS=$(ADDCPPFLAGS)"\
			"CC=$(CC)"\
			"LINT=$(LINT)"\
			"LINTFLAGS=$(LINTFLAGS)"\
			"LINT_ln=$(LINT_ln)"\
			$$target;\
	)

##############################################################################
# Additional targets                                                         #
##############################################################################

rpcmod:	$(RPCFILES)
	chmod a-w $(RPCFILES)

tags: TAGS

TAGS: $(RPCFILES) $(SVCSRC)
	etags -t $(RPCFILES) $(SVCSRC)


lint: $(SVCLNOBJ) $(RPCLNOBJ) $(CLTLNOBJ)
	$(LINT) $(LINTFLAGS) $(ALLSVCLNOBJ)


clean:
	$(RM)	*.o *.ln iod

rpcclean:
	$(RM)	$(RPCFILES)

realclean: clean
	$(RM)	TAGS *~

##############################################################################
# End of Makefile                                                            #
##############################################################################
