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

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

#all: $(RP2HEADERS) $(LIBRARIES) $(HEADERS) $(EXECUTABLES) $(SCRIPTS) $(TESTS) $(SUBDIRS) hdrinstall mkdep
all: Coda

##################
# build a single subdirectory
##################
ifdef SUBDIRS
.PHONY: $(SUBDIRS)
$(SUBDIRS):
	${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
###############################################################
MANPAGES=$(CMANPAGES) $(SMANPAGES)

coda: Coda
Coda: $(RP2HEADERS) $(LIBRARIES) $(HEADERS) $(EXECUTABLES) $(SCRIPTS) $(MANPAGES) $(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 c_conf_install c_script_install c_sscript_install c_man_install

ifeq ($(target),i386-pc-djgpp)
TARGETCBINS := $(CBINS:%=%.exe)
TARGETCSBINS := $(CSBINS:%=%.exe)
else
ifeq ($(SYS),cygwin32)
TARGETCBINS := $(CBINS:%=%.exe)
TARGETCSBINS := $(CSBINS:%=%.exe)
else
TARGETCBINS := $(CBINS)
TARGETCSBINS := $(CSBINS)
endif
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

c_conf_install: $(CCONFFILES)
ifdef CCONFFILES
	if test $(sysconfdir) = /etc/coda ; then \
	    $(TOPDIR)/tools/our-install 644 $(prefix)/..$(sysconfdir) $? ; \
	else \
	    $(TOPDIR)/tools/our-install 644 $(sysconfdir) $? ; \
	fi
endif

c_script_install: $(CSCRIPTS)
ifdef CSCRIPTS
	$(TOPDIR)/tools/our-install 755 $(BINDIR) $?
endif

c_sscript_install: $(CSSCRIPTS)
ifdef CSSCRIPTS
	$(TOPDIR)/tools/our-install 755 $(SBINDIR) $?
endif

installserver: s_bin_install s_sbin_install s_conf_install s_script_install s_sscript_install s_man_install

ifeq ($(SYS),cygwin32)
TARGETSBINS := $(SBINS:%=%.exe)
TARGETSSBINS := $(SSBINS:%=%.exe)
else
TARGETSBINS := $(SBINS)
TARGETSSBINS := $(SSBINS)
endif

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

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

s_conf_install: $(SCONFFILES)
ifdef SCONFFILES
	if test $(sysconfdir) = /etc/coda ; then \
	    $(TOPDIR)/tools/our-install 644 $(prefix)/..$(sysconfdir) $? ; \
	else \
	    $(TOPDIR)/tools/our-install 644 $(sysconfdir) $? ; \
	fi
endif

s_script_install: $(SSCRIPTS)
ifdef SSCRIPTS
	$(TOPDIR)/tools/our-install 755 $(BINDIR) $?
endif

s_sscript_install: $(SSSCRIPTS)
ifdef SSSCRIPTS
	$(TOPDIR)/tools/our-install 755 $(SBINDIR) $?
endif

c_man_install: $(CMANPAGES)
ifdef CMANPAGES
	for page in $(CMANPAGES) ; do \
	    sect=`echo $$page | sed 's/.*\\.//'` ;\
	    $(TOPDIR)/tools/our-install 644 $(MANDIR)/man$$sect $(srcdir)/$$page ; \
	done
endif

s_man_install: $(SMANPAGES)
ifdef SMANPAGES
	for page in $(SMANPAGES) ; do \
	    sect=`echo $$page | sed 's/.*\\.//'` ; \
	    $(TOPDIR)/tools/our-install 644 $(MANDIR)/man$$sect $(srcdir)/$$page ; \
	done
endif



#####################
# install headers
#####################
hdrinstall: chdrinstall rp2hdrinstall

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

rp2hdrinstall: $(RP2HEADERS)
ifdef RP2HEADERS
	$(TOPDIR)/tools/our-install 644 $(INCLDIR) $(RP2HEADERS)
	-touch rp2hdrinstall
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 \
	      c_man_install s_man_install

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

clean: subdirclean realclean xtraclean

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

realclean:
	-$(RM) *.o *.a  *.d core
	-$(RM) *.client.cc *.client.c *.server.cc *.server.c *.multi.cc *.multi.c *.print.c $(RP2HEADERS)
	-$(RM) $(EXECUTABLES) $(LIBRARIES) $(TESTS) $(EXTRACLEAN)
	-$(RM) chdrinstall rp2hdrinstall libinstall bininstall scriptinstall \
	      c_bin_install c_sbin_install s_bin_install s_sbin_install \
	      c_man_install s_man_install
	-$(RM) *.l.c *.pt.c *.l.o *.pt.o

xtraclean:

distclean: clean
	-$(RM) -r include
	-$(RM) config.cache config.log config.status
	-$(RM) Makeconf.setup `find . -name "Makefile" | grep -v linux`
	-$(RM) config.h coda_config.h
	-$(RM) coda-src/scripts/bldvldb.sh
	-$(RM) coda-src/scripts/createvol_rep
	-$(RM) coda-src/scripts/rc.vice
	-$(RM) coda-src/scripts/startserver
	-$(RM) coda-src/scripts/venus.init
	-$(RM) coda-src/scripts/vice-setup
	-$(RM) coda-src/scripts/vice-setup-rvm

dist:
	-$(RM) coda-$(RELEASE).tar.gz
	-cp -al . coda-$(RELEASE)
	( cd coda-$(RELEASE) ; make distclean ; rm -rf .git )
	tar chof - coda-$(RELEASE) | gzip -c > coda-$(RELEASE).tar.gz
	-$(RM) -r coda-$(RELEASE)

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


###############################################################
# 			misc
###############################################################

##################
# testing variables
##################
VARS=SECSRC OLDVPATH VPATH VPATHTAIL CFLAGS AFLAGS LFLAGS\
    INCLDIR INCLFLAGS INCLPATH TOPDIR LIBDIR LIBS LIBEXECDIR\
    BINDIR SBINDIR MANDIR SHORTSYS VFSDIR LIBRARIES EXECUTABLES SSBINS\
    SBINS CSBINS CBINS TARGETCBINS TARGETCSBINS target sys\
    shortsys HEADERS RP2HEADERS
ECHOVAR+= echo "$(VAR)	 $($(VAR))" ;
ECHOCMD:= $(foreach VAR, $(VARS), $(ECHOVAR))

show showvar:
	@$(ECHOCMD)


###############################################################
# 		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} $<

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

-include *.d

