###############################################################################
#
#    Makefile,v Makefile for PFSLib
#    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:53:02
#  RCS Revision : 1.18
#  RCS Author   : lamberts
#
#  Authors: Stefan Lamberts, Christian R"oder
#
###############################################################################


##############################################################################
# Different architectures                                                    #
##############################################################################
#
# Coose one of the following architectures
#
# If you want to use a compiler other than gcc edit the appropriate 
# Makefile.$(ARCH)
#

# Sun 4 running SunOS 4.1.X #
#ARCH	= SUN4

# Sun 4 running SunOS 5.X called Solaris 2 #
#ARCH	= SUN4SOL2

# HP running HPUX 9.X #
#ARCH	= HPPA

# IBM RS6000 running AIX 3.2 #
#ARCH	= RS6K

# IBM PC and Compatible running Linux
#ARCH	= LINUX

##############################################################################
# Installation directory                                                     #
##############################################################################

INSTALLDIR	= $(HOME)/PFSLib_V1_0_$(ARCH)

##############################################################################
# DO NOT EDIT BELOW THIS LINE                                                #
##############################################################################

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

SHELL		= /bin/sh

PFSDDIR		= pfsd
IODDIR		= iod
CLNTDIR		= clnt
CMDDIR		= cmds
INCDIR		= include

DIRS		= $(PFSDDIR) $(IODDIR) $(CLNTDIR) $(CMDDIR)

ARCH_FILE	= .arch

INSTALLBINDIR	= $(INSTALLDIR)/bin
INSTALLLIBDIR	= $(INSTALLDIR)/lib
INSTALLINCDIR	= $(INSTALLDIR)/include

##############################################################################
# Default commands and flags                                                 #
##############################################################################

ANSI_C		= -DANSI_C
CC		= gcc
CFLAGS		= -ansi -O -w
#CFLAGS		= -ansi -g -Wall -pedantic -Wtraditional -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wconversion

LDFLAGS		=
LDLIBS		= -lrpcsvc

AR		= ar
ARFLAGS		= rv
RANLIB		= ranlib

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

RM		= rm -f

MKDIR		= mkdir -p

include Makefile.$(ARCH)

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

install: all inst_files
	@echo
	@echo "****************************************************************************"
	@echo "*"
	@echo "* Installation of PFSLib in directory $(INSTALLDIR)"
	@echo "* completed."
	@echo "*"
	@echo "* Adding $(INSTALLBINDIR) to your PATH environment"
	@echo "* variable is recomended."
	@echo "*"
	@echo "* To run applications in the example subdirectory set the environment"
	@echo "* variable PFSLIBPATH to $(INSTALLDIR)."
	@echo "*"
	@echo "****************************************************************************"
	@echo

all: check_arch pfsd_client iod_client pfsd_server iod_server lib cmd

uninstall: uninst_files

#############################################################################
# Check Architecture                                                        #
##############################################################################

check_arch:
	@if [ -f $(ARCH_FILE) ] ;\
	then\
		arch=`cat $(ARCH_FILE)` ;\
		if [ $$arch != $(ARCH) ] ;\
		then \
			echo "Cleaning due to wrong architecture" ;\
			$(MAKE) $(MFLAGS)\
				"RM=$(RM)"\
				clean;\
			echo $(ARCH) > $(ARCH_FILE) ;\
			echo;\
		fi \
	else\
		echo $(ARCH) > $(ARCH_FILE) ;\
	fi

##############################################################################
# Installation                                                               #
##############################################################################

inst_files: $(INSTALLDIR) $(INSTALLBINDIR) $(INSTALLLIBDIR) $(INSTALLINCDIR)
	@echo "Installing PFSLib for $(ARCH) in directory $(INSTALLDIR)"
	cp $(CLNTDIR)/libpfs.a		$(INSTALLLIBDIR)/libpfs.a
	cp $(INCDIR)/pfslib.h		$(INSTALLINCDIR)/pfslib.h
	cp $(INCDIR)/pfslib_errno.h	$(INSTALLINCDIR)/pfslib_errno.h
	cp $(PFSDDIR)/pfsd		$(INSTALLBINDIR)/pfsd
	cp $(IODDIR)/iod		$(INSTALLBINDIR)/iod
	cp $(CMDDIR)/pfsdexit		$(INSTALLBINDIR)/pfsdexit
	cp $(CMDDIR)/pfsdreset		$(INSTALLBINDIR)/pfsdreset
	cp $(CMDDIR)/pfsdstat		$(INSTALLBINDIR)/pfsdstat
	@echo

$(INSTALLDIR) \
$(INSTALLBINDIR) \
$(INSTALLLIBDIR) \
$(INSTALLINCDIR):
	@echo "Creating directory $@"
	$(MKDIR) $@
	@echo


uninst_files:
	@echo "Uninstalling PFSLib from directory $(INSTALLDIR)"
	$(RM)	$(INSTALLLIBDIR)/libpfs.a
	$(RM)	$(INSTALLINCDIR)/pfslib.h
	$(RM)	$(INSTALLINCDIR)/pfslib_errno.h
	$(RM)	$(INSTALLBINDIR)/pfsd
	$(RM)	$(INSTALLBINDIR)/iod
	$(RM)	$(INSTALLBINDIR)/pfsdexit
	$(RM)	$(INSTALLBINDIR)/pfsdreset
	$(RM)	$(INSTALLBINDIR)/pfsdstat
	-rmdir	$(INSTALLBINDIR)
	-rmdir	$(INSTALLLIBDIR)
	-rmdir	$(INSTALLINCDIR)
	-rmdir  $(INSTALLDIR)
	@echo

##############################################################################
# pfsd client and server files                                               #
##############################################################################

pfsd_client \
pfsd_server:
	@echo "Making $@ for $(ARCH)"
	@(\
	echo "Going to directory $(PFSDDIR)";\
	cd $(PFSDDIR);\
	$(MAKE) $(MFLAGS)\
		"ARCH=$(ARCH)"\
		"CC=$(CC)"\
		"ANSI_C=$(ANSI_C)"\
		"CFLAGS=$(CFLAGS)"\
		"ADDCPPFLAGS=$(ADDCPPFLAGS)"\
		"LDFLAGS=$(LDFLAGS)"\
		"LDLIBS=$(LDLIBS)"\
		$@;\
	)
	@echo

##############################################################################
# iod client and server files                                                #
##############################################################################

iod_client \
iod_server:
	@echo "Making $@ for $(ARCH)"
	@(\
	echo "Going to directory $(IODDIR)";\
	cd $(IODDIR);\
	$(MAKE) $(MFLAGS)\
		"ARCH=$(ARCH)"\
		"CC=$(CC)"\
		"ANSI_C=$(ANSI_C)"\
		"CFLAGS=$(CFLAGS)"\
		"ADDCPPFLAGS=$(ADDCPPFLAGS)"\
		"LDFLAGS=$(LDFLAGS)"\
		"LDLIBS=$(LDLIBS)"\
		$@;\
	)
	@echo

##############################################################################
# library                                                                    #
##############################################################################

lib:
	@echo "Making client library for $(ARCH)"
	@(\
	echo "Going to directory $(CLNTDIR)";\
	cd $(CLNTDIR);\
	$(MAKE) $(MFLAGS)\
		"ARCH=$(ARCH)"\
		"CC=$(CC)"\
		"ANSI_C=$(ANSI_C)"\
		"CFLAGS=$(CFLAGS)"\
		"ADDCPPFLAGS=$(ADDCPPFLAGS)"\
		"AR=$(AR)"\
		"ARFLAGS=$(ARFLAGS)"\
		"RANLIB=$(RANLIB)"\
		lib;\
	)
	@echo

##############################################################################
# rpc files                                                                  #
##############################################################################

rpc \
rpcmod \
rpcclean:
	@echo "Making $@ for rpcgen generated files"
	@for dir in $(PFSDDIR) $(IODDIR);\
	do\
	(\
		echo "Going to directory $$dir";\
		cd $$dir;\
		$(MAKE) $(MFLAGS) $@;\
		echo;\
	)\
	done
	@echo

##############################################################################
# commands                                                                   #
##############################################################################

cmd: lib
	@(\
	echo "Making command for $(ARCH)";\
	echo "Going to directory $(CMDDIR)";\
	cd $(CMDDIR);\
	$(MAKE) $(MFLAGS)\
		"ARCH=$(ARCH)"\
		"CC=$(CC)"\
		"ANSI_C=$(ANSI_C)"\
		"CFLAGS=$(CFLAGS)"\
		"ADDCPPFLAGS=$(ADDCPPFLAGS)"\
		"LDFLAGS=$(LDFLAGS)"\
		"LDLIBS=$(LDLIBS)";\
	echo;\
	)

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

dist: realclean rpcclean rpcmod

realclean:: localrealclean

tags:: localtags

clean \
lint \
realclean \
tags::
	@echo "Making $@"
	@for dir in $(DIRS);\
	do\
	(\
		echo "Going to directory $$dir";\
		cd $$dir;\
		$(MAKE) $(MFLAGS)\
		"ARCH=$(ARCH)"\
		"ANSI_C=$(ANSI_C)"\
		"ADDCPPFLAGS=$(ADDCPPFLAGS)"\
		"LINT=$(LINT)"\
		"LINTFLAGS=$(LINTFLAGS)"\
		"LINT_ln=$(LINT_ln)" $@;\
		echo;\
	)\
	done

localtags:
	@(\
		files="" ;\
		for dir in $(DIRS);\
		do\
			files="$$files $$dir/*.c";\
		done;\
		echo "Making TAGS with $$files";\
		etags -t $$files;\
	)
	@echo

localrealclean:
	$(RM) TAGS *~

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