#
# 	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: 
	@echo "Choose from client-install or server-install"

###############################################################
#  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  client or server executables 
#####################
installclient: c_bin_install c_sbin_install

ifeq ($(host_alias),windows95)
TARGETCBINS := $(CBINS:%=%.exe)
TARGETCSBINS := $(CSBINS:%=%.exe)
else
TARGETCBINS := $(CBINS)
TARGETCSBINS := $(CSBINS)
endif

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

c_sbin_install: $(TARGETCSBINS)
ifdef TARGETCSBINS
	$(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 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
###############################################################

##################
# testing variables
##################
show showvar:
	@echo  SECSRC: $(SECSRC)
	@echo  SHADOWINCLS: $(SHADOWINCLS)
	@echo  SHADOWRP2: $(SHADOWRP2)
	@echo  INCLPATH: $(INCLPATH)	
	@echo  OLDVPATH: $(OLDVPATH)
	@echo  VPATH: $(VPATH)
	@echo  VPATHTAIL: $(VPATHTAIL)
	@echo  SHOB: $(SHOB)
	@echo  SECSRC: $(SECSRC) 
	@echo  CFLAGS: $(CFLAGS)
	@echo  INCLDIR: $(INCLDIR)
	@echo  INCLFLAGS: $(INCLFLAGS)
	@echo  TOPDIR: $(TOPDIR) 
	@echo  LIBDIR: $(LIBDIR)
	@echo  LIBS: $(LIBS)
	@echo  LIBEXECDIR: $(LIBEXECDIR)
	@echo  BINDIR: $(BINDIR)
	@echo  SBINDIR: $(SBINDIR)
	@echo  MANDIR:  $(MANDIR)
	@echo  SHORTSYS:  $(SHORTSYS)
	@echo  VFSDIR:  $(VFSDIR)
	@echo  MANDIR:  $(MANDIR)
	@echo  HEADERS: $(HEADERS)
	@echo  RP2GEN: $(RP2GEN)
	@echo  RP2FILES:    $(RP2FILES)
	@echo  RP2HEADERS:  $(RP2HEADERS)
	@echo  CBINS: $(CBINS)
	@echo  TARGETCBINS: $(TARGETCBINS)	
	@echo  CSBINS: $(CSBINS)
	@echo  TARGETCSBINS: $(TARGETCSBINS)
	@echo  host: $(host)
	@echo  host_alias: $(host_alias)

##################
# 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) $@

.PRECIOUS: %.client.c %.server.c %.multi.c %.h

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


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

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

.PHONY FORCE:


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

-include .depend
