#
# 	says how to build/install/clean & some specific rules

###############################################################
#  recursively descend into subdirs and build targets
###############################################################

all: $(LIBRARIES) $(HEADERS) $(EXECUTABLES) $(SCRIPTS) $(TESTS) $(SUBDIRS) mkdep

##################
# build a single subdirectory
##################
ifdef SUBDIRS
$(SUBDIRS): FORCE
	${MAKE} -C $@ all; 
endif

###############################################################
#  recursively descend into subdirs and install all
###############################################################

install: $(LIBRARIES) $(HEADERS) $(EXECUTABLES) $(SCRIPTS) $(TESTS)\
	 hdrinstall bininstall scriptinstall libinstall mkdep
ifdef SUBDIRS
	@for d in $(SUBDIRS) ; do \
	    ${MAKE} -C $$d install; \
	    if [ $$? != 0 ] ; then echo "----> error in $$d"; exit 1; \
	    else true; fi; \
	done
endif

###############################################################
#  recursively descend into subdirs and do hdrinstall and build
###############################################################
coda: Coda
Coda: $(LIBRARIES) $(HEADERS) $(EXECUTABLES) $(SCRIPTS) $(TESTS)\
	 hdrinstall mkdep
ifdef SUBDIRS
	@for d in $(SUBDIRS) ; do \
	    echo "----> Entering $$d"; \
	    ${MAKE} -C $$d Coda; \
	    if [ $$? != 0 ] ; then echo "----> Error in $$d"; exit 1; \
	    else echo "----> Leaving $$d."; fi; \
	done
endif

###############################################################
#  recursively descend into subdirs and build & install client
###############################################################

client-install: $(LIBRARIES) $(HEADERS) $(EXECUTABLES) $(SCRIPTS) $(TESTS)\
	 hdrinstall installclient mkdep
ifdef SUBDIRS
	@for d in $(SUBDIRS) ; do \
	    echo "----> Entering $$d"; \
	    ${MAKE} -C $$d client-install; \
	    if [ $$? != 0 ] ; then echo "----> Error in $$d"; exit 1; \
	    else echo "----> Leaving $$d."; fi; \
	done
endif

qclient-install: installclient
ifdef SUBDIRS
	@for d in $(SUBDIRS) ; do \
	    echo "----> Entering $$d"; \
	    ${MAKE} -C $$d client-install; \
	    if [ $$? != 0 ] ; then echo "----> Error in $$d"; exit 1; \
	    else echo "----> Leaving $$d."; fi; \
	done
endif

###############################################################
#  recursively descend into subdirs and build & install server
###############################################################

server-install: $(LIBRARIES) $(HEADERS) $(EXECUTABLES) $(SCRIPTS) $(TESTS)\
	 hdrinstall installserver mkdep
ifdef SUBDIRS
	@for d in $(SUBDIRS) ; do \
	    echo "----> Entering $$d"; \
	    ${MAKE} -C $$d server-install; \
	    if [ $$? != 0 ] ; then echo "----> Error in $$d"; exit 1; \
	    else echo "----> Leaving $$d."; fi; \
	done
endif

qserver-install: installserver
ifdef SUBDIRS
	@for d in $(SUBDIRS) ; do \
	    echo "----> Entering $$d"; \
	    ${MAKE} -C $$d server-install; \
	    if [ $$? != 0 ] ; then echo "----> Error in $$d"; exit 1; \
	    else echo "----> Leaving $$d."; fi; \
	done
endif

###############################################################
#  install the man pages
###############################################################
man-install:
	cd man ; cp -r man* ${prefix}/man


#####################
# install executables
#####################
bininstall: $(EXECUTABLES)
ifdef EXECUTABLES
 ifdef SHOB
	$(TOPDIR)/tools/our-install 755 $(SHOB_BIN_DIR) $?
	-touch bininstall
 else
	@echo SHOB not set\; Can not \"bininstall\"
	exit 1
 endif
endif

#####################
# install  client or server executables 
#####################
installclient: c_bin_install c_sbin_install

c_bin_install: $(CBINS) 
ifdef CBINS
	$(TOPDIR)/tools/our-install 755 $(BINDIR) $?
endif

c_sbin_install: $(CSBINS)
ifdef CSBINS
	$(TOPDIR)/tools/our-install 755 $(SBINDIR) $?
endif

installserver: s_bin_install s_sbin_install

s_bin_install: $(SBINS)
ifdef SBINS
	$(TOPDIR)/tools/our-install 755 $(BINDIR) $?
endif

s_sbin_install: $(SSBINS)
ifdef SSBINS
	$(TOPDIR)/tools/our-install 755 $(SBINDIR) $?
endif

#####################
# install scripts
#####################
scriptinstall: $(SCRIPTS)
ifdef SCRIPTS
 ifdef SHOB
	$(TOPDIR)/tools/our-install 755 $(SHOB_BIN_DIR) $?
	-touch scriptinstall
 else
	@echo SHOB not set\; Can not \"scriptinstall\"
	exit 1
 endif
endif

#####################
# install libraries
#####################
libinstall: $(LIBRARIES)
ifdef LIBRARIES
 ifdef SHOB
	$(TOPDIR)/tools/our-install 644 $(SHOB_LIB_DIR) $?
	-touch libinstall
 else
	@echo SHOB not set\; Can not \"libinstall\"
	exit 1
 endif
endif

#####################
# install headers
#####################
hdrinstall: $(HEADERS)
ifdef HEADERS
	$(TOPDIR)/tools/our-install 644 $(INCLDIR) $?
	-touch hdrinstall
endif


###############################################################
#  local reinstall
###############################################################
reinstall: clean-install install
clean-install:
	$(RM) bininstall scriptinstall hdrinstall libinstall \
	      c_bin_install c_sbin_install s_bin_install s_sbin_install 

##################
# cleaning: important too
##################

clean: subdirclean realclean

subdirclean:
ifdef SUBDIRS
	@for d in $(SUBDIRS) ; do \
	    ${MAKE} -C $$d clean; \
	done
endif

realclean:
	$(RM) *.o *.a .depend *.d
	$(RM) *.client.cc *.client.c *.server.cc *.server.c *.multi.cc *.multi.c $(RP2HEADERS)
	$(RM) $(EXECUTABLES) $(LIBRARIES) $(TESTS) core
	$(RM) lex.yy.c y.tab.c y.tab.h
	$(RM) hdrinstall libinstall bininstall scriptinstall \
	      c_bin_install c_sbin_install s_bin_install s_sbin_install 
	$(RM) *.l.c *.pt.c *.l.o *.pt.o

distclean: clean
	rm -rf include
	rm -f coda-src/rpc2/errorsdefs.h
	rm -f coda-src/rpc2/switchc2s.h
	rm -f coda-src/rpc2/switchs2c.h
	rm -f config.cache config.log config.status
	rm -f Makeconf.setup `find . -name "Makefile" | grep -v linux`

	

#####################
# dependency generation
#####################
mkdep:
	@$(TOPOBJ)/md -o .depend -d -S $(INCLFLAGS) *.d


###############################################################
# 			misc
###############################################################
#####################
# source checkin ... 
#####################
srcinstall:
	SRCDIR=$(srcdir); export SRCDIR; $(ALPHACI)


##################
# testing variables
##################
show showvar:
	@echo  RP2GEN: $(RP2GEN)
	@echo  SHADOWINCLS: $(SHADOWINCLS)
	@echo  SHADOWRP2: $(SHADOWRP2)
	@echo  INCLPATH: $(INCLPATH)	
	@echo  VPATH: $(VPATH)
	@echo  SHOB: $(SHOB) 
	@echo  SECSRC: $(SECSRC) 
	@echo  CFLAGS: $(CFLAGS)
	@echo  TOPDIR: $(TOPDIR) 
	@echo  LIBDIR: $(LIBDIR)
	@echo  LIBEXECDIR: $(LIBEXECDIR)
	@echo  BINDIR: $(BINDIR)
	@echo  SBINDIR: $(SBINDIR)
	@echo  MANDIR:  $(MANDIR)

##################
# linting
##################
lint: $(SOURCES)
	lint $(INCDIRS) $(SOURCES)

###############################################################
# 		build rules
###############################################################

%.o: %.s
	$(CPP) -P -D__ASSEMBLY__ $(AFLAGS) $< > $*.ss
	$(AS) -o $@ $*.ss
	$(RM) $*.ss

%.a:
	$(AR) rv $@ $?
	$(RANLIB) $@

%.doc: %.mss
	-scribe $?  # Scribe doesn't exist most places.

# %.client.cc %.server.cc %.multi.cc %.client.c %.server.c %.multi.c %.h: %.rpc2
%.client.cc %.server.cc %.multi.cc  %.h: %.rpc2
	${RP2GEN} ${RP2FLAGS} $<

ifndef NO_LEX
lex.yy.c:
	$(LEX) $(LFLAGS) $<

y.tab.c:
	$(YACC) $(YFLAGS) $<
endif

.PHONY FORCE:

########################
# If you need to override globally during the build, place
# Makeconf.overrule at the top of your build tree and uncomment
# the line below
########################

ifdef OVERRIDE
include $(TOPOBJ)/Makeconf.override
endif

###############################################################
# 		Finally ...
#	suck down any dependencies we know about
###############################################################

-include .depend
