#############################################################################
#
# PROJECT:	LRD
#
# FILE:		Makefile.std
#		Based on Erebus prototypical Makefile
#		Modified from TCA Makefile.std
#
# DESCRIPTION:
#	This file should be included at the tail of the prototypical
#	Makefile.  It provides a set of "standard" and useful targets.
#
# EXPORTS:
#	The following targets are available:
#		all bins libs privs objs
#		clean install
#		lint depend
#		update add commit
#
# HISTORY:
#
#   6/20/96 - Genesis 
#
#############################################################################

# All source files
SRCS_ALL	:= $(INCS) $(PRIVATE_INCS) $(SRCS) $(MANS) $(DOCS) $(MISC)

# Files never to be removed, even if Make encounters an error
.PRECIOUS: $(SRCS_ALL) $(SUBDIRS)

# make sure everyone is using the same default paths.
LPATH=:/usr/cs/lib:/usr/lib:/usr/misc/lib
CPATH=:/usr/misc/include

# C++ support
.SUFFIXES: suffixes .cc .cxx .gz

.cc:
#	$(LINK.cc) -o $(ODIR)/$@ $< $(LDLIBS)
	$(LINK.cc) -o $@ $< $(LDLIBS)
.cc.o:
#	$(COMPILE.cc) -o $(ODIR)/$@ $< $(CPP_LIBRARIES)
	$(COMPILE.cc) -o $@ $< $(CPP_LIBRARIES)
.cxx:
#	$(LINK.c) -o $(ODIR)/$@ $< $(LDLIBS)
	$(LINK.c) -o $@ $< $(LDLIBS)
.cxx.o:
#	$(COMPILE.c) -o $(ODIR)/$@ $<
	$(COMPILE.c) -o $@ $<
.c.o:
#	$(COMPILE.c) -o $(ODIR)/$@ $<
	$(COMPILE.c) -o $@ $<
.c:
#	$(LINK.c) -o $(ODIR)/$@ $< $(LIBRARIES)
	$(LINK.c) -o $@ $< $(LIBRARIES)
.o:
#	$(LINK.c) -o $(ODIR)/$@ $< $(LIBRARIES)
	$(LINK.c) -o $@ $< $(LIBRARIES)

# Automatic defs to aid install target
INS_PUBLIC_BINS	:= $(patsubst %,$(PUBLIC_BIN_DIR)/%,$(PUBLIC_BINS))
INS_PUBLIC_LIBS	:= $(patsubst %,$(PUBLIC_LIB_DIR)/%,$(PUBLIC_LIBS))
INS_PUBLIC_LINTS:= $(patsubst %,$(PUBLIC_LINT_DIR)/%,$(PUBLIC_LINTS))
INS_PUBLIC_INCS	:= $(patsubst %,$(PUBLIC_INC_DIR)/%,$(PUBLIC_INCS))
INS_PUBLIC_MANS1:= $(patsubst %.1,$(PUBLIC_MAN_DIR)1/%.1,$(filter %.1,$(PUBLIC_MANS)))
INS_PUBLIC_MANS3:= $(patsubst %.3,$(PUBLIC_MAN_DIR)3/%.3,$(filter %.3,$(PUBLIC_MANS)))
INS_PUBLIC_DOCS	:= $(patsubst %,$(PUBLIC_DOC_DIR)/%,$(PUBLIC_DOCS))

INS_MODULE_LIBS	:= $(patsubst %,$(MODULE_LIB_DIR)/%,$(MODULE_LIBS))
INS_MODULE_LINTS:= $(patsubst %,$(MODULE_LINT_DIR)/%,$(MODULE_LINTS))
INS_MODULE_DOCS:= $(patsubst %,$(MODULE_DOC_DIR)/%,$(MODULE_DOCS))
INS_MODULE_INCS	:= $(patsubst %,$(MODULE_INC_DIR)/%,$(MODULE_INCS))

# Automatic defs to aid local target
PRIV_BINS 	:= $(patsubst %,$(BDIR)/%,$(PRIVS))

# Automatic defs to aid cvs commands
CVSROOT := /usr/apex/lrd/software/Master
CVS_COMMIT_LOG := $(CVSROOT)/CVSROOT/commitlog
RCSBIN := $(CVS_BIN)


##########
# Standard Targets
##########

srcs: $(SRCS_ALL) $(ODIR)/.force $(LDIR)/.force $(BDIR)/.force
bins:: $(patsubst %,$(BDIR)/%,$(PUBLIC_BINS))
libs:: $(patsubst %,$(LDIR)/%,$(PUBLIC_LIBS))\
	 $(patsubst %,$(LDIR)/%,$(MODULE_LIBS))\
	 $(PUBLIC_LINTS) $(MODULE_LINTS)
privs: $(patsubst %,$(BDIR)/%,$(PRIVS))
objs:: $(patsubst %,$(ODIR)/%,$(OBJS))

# These directories are really essential since we're carrying
# around two versions of the binary/object files (Linux + SunOS)
$(ODIR)/.force:
	- mkdir $(ODIR); \
	echo `date` > $(ODIR)/.force

$(BDIR)/.force:
	- mkdir $(BDIR); \
	echo `date` > $(BDIR)/.force

$(LDIR)/.force:
	- mkdir $(LDIR); \
	echo `date` > $(LDIR)/.force

clean::
	-$(RM) $(DELETES);
	-$(RM) $(wildcard .nse_dep*)
	-$(RM) $(wildcard *.CKP)
	-$(RM) $(wildcard \#*\#)
	-$(RM) $(wildcard *~)
	-$(RM) $(wildcard .*~)
	-$(RM) $(wildcard *.CKP)
	-$(RM) $(wildcard *.BAK)
	-$(RM) $(wildcard .*.BAK)

ifdef SUBDIRS
clean:: 
	-(for sub in $(SUBDIRS); do \
	  (if [ -d $$sub ]; then \
	    cd $$sub; $(MAKE) clean; fi;) \
	done;)
endif

##########
# Source file dependencies
##########

MAKEDEPEND	= makedepend

MDFLAGS = $(CPPFLAGS) $(DEPEND_FLAGS)

ifdef DEPEND_FILE
  ifdef DEPEND_PREFIX
# Should really be 
# $(DEPEND_FILE): $(SRCS) $(INCS) $(PRIVATE_INCS)
# But that causes the depend file to be rebuilt every time.
depend:: 
	-$(RM) $(DEPEND_FILE)
	echo "# DO NOT DELETE THIS LINE -- make depend depends on it." > $(DEPEND_FILE)
	-for pre in $(DEPEND_PREFIX); do \
	  $(MAKEDEPEND) -a -p$$pre -f$(DEPEND_FILE) -- $(MDFLAGS) -- $(SRCS); \
	done;
  else
depend::
	-$(RM) $(DEPEND_FILE)
	echo "# DO NOT DELETE THIS LINE -- make depend depends on it." > $(DEPEND_FILE)
	-$(MAKEDEPEND) -f$(DEPEND_FILE) -- $(MDFLAGS) -- $(SRCS)
  endif
endif

ifdef SUBDIRS
depend:: 
	-(for sub in $(SUBDIRS); do \
	  (if [ -d $$sub ]; then \
	    echo "Making depend for: $$sub";\
	    cd $$sub; $(MAKE) depend; fi;) \
	done;)
endif

# Method of removing dependancies that vary from machine to machine.

ifdef SUBDIRS
clean_depend:: 
	for sub in $(SUBDIRS); do \
	( if [ -d $$sub ]; then \
	  echo "Cleaning depend for: $$sub";\
	  cd $$sub; $(MAKE) clean_depend; fi;) \
	done;
endif

ifdef DEPEND_FILE
clean_depend:: 
	-$(RM) $(DEPEND_FILE)
endif

lint: $(SRCS) $(PUBLIC_INCS) $(MODULE_INCS) $(PRIVATE_INCS)
	$(LINT.c) $(SRCS)

##########
#install rules
##########


install:: install_incs install_libs install_bins install_mans install_docs

install:: srcs

#ifdef SUBDIRS
#install:: 
#	for sub in $(SUBDIRS); do \
#	(if [ -d $$sub ]; then \
#	    echo "Making install for: $$sub";\
#	    cd $$sub; $(MAKE) install; fi;) \
#	done;
#endif

install_incs:: $(INS_PUBLIC_INCS) $(INS_MODULE_INCS)
ifdef SUBDIRS
install_incs:: 
	for sub in $(SUBDIRS); do \
	(if [ -d $$sub ]; then \
	    echo "Making install_incs for: $$sub";\
	    cd $$sub; $(MAKE) install_incs; fi;) \
	done;
endif


install_libs:: $(INS_PUBLIC_LIBS) $(INS_PUBLIC_LINTS)\
	      $(INS_MODULE_LIBS) $(INS_MODULE_LINTS)
ifdef SUBDIRS
install_libs:: 
	for sub in $(SUBDIRS); do \
	(if [ -d $$sub ]; then \
	    echo "Making install_libs for: $$sub";\
	    cd $$sub; $(MAKE) install_libs; fi;) \
	done;
endif

install_bins:: $(INS_PUBLIC_BINS) install_mans install_docs
ifdef SUBDIRS
install_bins:: 
	for sub in $(SUBDIRS); do \
	(if [ -d $$sub ]; then \
	    echo "Making install_bins for: $$sub";\
	    cd $$sub; $(MAKE) install_bins; fi;) \
	done;
endif

install_mans: $(INS_PUBLIC_MANS)

install_docs: $(INS_PUBLIC_DOCS) $(INS_MODULE_DOCS)

.PHONY: srcs bins libs privs objs clean depend clean_depend lint tags \
	install install_bins install_libs install_incs install_mans \
	install_docs msg commit do_commit do_version_update

#install rules for public and module files.

ifdef INS_PUBLIC_BINS
$(INS_PUBLIC_BINS): $(PUBLIC_BIN_DIR)/%: $(BDIR)/%
	-mkdir $(PUBLIC_BIN_DIR)
	$(INSTALL) $(INS_BIN_FLAGS) $(BDIR)/$(@F) $(PUBLIC_BIN_DIR)
	$(RM) $(BDIR)/$(@F)
endif

ifdef INS_PUBLIC_LIBS

ifneq ($(filter $(PUBLIC_LIB_DIR)/libtca.so.%,$(INS_PUBLIC_LIBS)),)
$(filter $(PUBLIC_LIB_DIR)/libtca.so.%,$(INS_PUBLIC_LIBS)): $(PUBLIC_LIB_DIR)/%: $(LDIR)/%
	-mkdir $(PUBLIC_LIB_DIR)
	$(INSTALL) $(INS_LIB_FLAGS) $(LDIR)/$(@F) $(PUBLIC_LIB_DIR)
	$(RM) $(LDIR)/$(@F)
	chmod 444 $@
endif

ifneq ($(filter-out $(PUBLIC_LIB_DIR)/libtca.so.%,$(INS_PUBLIC_LIBS)),)
$(filter-out $(PUBLIC_LIB_DIR)/libtca.so.%,$(INS_PUBLIC_LIBS)): $(PUBLIC_LIB_DIR)/%: $(LDIR)/%
	-mkdir $(PUBLIC_LIB_DIR)
	$(INSTALL) $(INS_LIB_FLAGS) $(LDIR)/$(@F) $(PUBLIC_LIB_DIR)
	$(RM) $(LDIR)/$(@F)
	$(RANLIB) $@
	chmod 444 $@
endif
endif

ifdef INS_PUBLIC_LINTS
$(INS_PUBLIC_LINTS): $(PUBLIC_LINT_DIR)/%: %
	-mkdir $(PUBLIC_LINT_DIR)
	$(INSTALL) $(INS_LINT_FLAGS) $(@F) $(PUBLIC_LINT_DIR)
endif

ifdef INS_PUBLIC_INCS
$(INS_PUBLIC_INCS): $(PUBLIC_INC_DIR)/%: %
	-mkdir $(PUBLIC_INC_DIR)
	$(INSTALL) $(INS_INC_FLAGS) $(@F) $(PUBLIC_INC_DIR)
endif

ifdef INS_PUBLIC_MANS
$(INS_PUBLIC_MANS): $(PUBLIC_MAN_DIR)1/%: %
	-mkdir -p $(PUBLIC_MAN_DIR)1
	$(INSTALL) $(INS_MAN_FLAGS) $(@F) $(PUBLIC_MAN_DIR)1

$(INS_PUBLIC_MANS): $(PUBLIC_MAN_DIR)3/%: %
	-mkdir -p $(PUBLIC_MAN_DIR)3
	$(INSTALL) $(INS_MAN_FLAGS) $(@F) $(PUBLIC_MAN_DIR)3
endif

ifdef INS_PUBLIC_DOCS
$(INS_PUBLIC_DOCS): $(PUBLIC_DOC_DIR)/%: %
	-mkdir $(PUBLIC_DOC_DIR)
	$(INSTALL) $(INS_DOC_FLAGS) $(@F) $(PUBLIC_DOC_DIR)
endif

ifdef INS_MODULE_DOCS
$(INS_MODULE_DOCS): $(MODULE_DOC_DIR)/%: %
	-mkdir -p $(MODULE_DOC_DIR)
	$(INSTALL) $(INS_DOC_FLAGS) $(@F) $(MODULE_DOC_DIR)
endif

ifdef INS_MODULE_LIBS
$(INS_MODULE_LIBS): $(MODULE_LIB_DIR)/%: $(LDIR)/%
	-mkdir $(MODULE_LIB_DIR)
	$(INSTALL) $(INS_LIB_FLAGS) $(LDIR)/$(@F) $(MODULE_LIB_DIR)
	$(RM) $(LDIR)/$(@F)
	$(RANLIB) -t $@
	chmod 444 $@
endif

ifdef INS_MODULE_LINTS
$(INS_MODULE_LINTS): $(MODULE_LINT_DIR)/%: %
	-mkdir $(MODULE_LINT_DIR)
	$(INSTALL) $(INS_LINT_FLAGS) $(@F) $(MODULE_LINT_DIR)
endif

ifdef INS_MODULE_INCS
$(INS_MODULE_INCS): $(MODULE_INC_DIR)/%: %
	-mkdir $(MODULE_INC_DIR)
	$(INSTALL) $(INS_INC_FLAGS) $(@F) $(MODULE_INC_DIR)
endif

##########
# Simple Recursion
##########

ifdef SUBDIRS
all:: 
	for sub in $(SUBDIRS); do \
	  (if [ -d $$sub ]; then \
	    echo "Making all for: $$sub";\
	    cd $$sub; $(MAKE) all; fi;) \
	done;
endif

##########
# Tags files
##########

tags::
	-etags $(SRCS_ALL) $(wildcard */*.[hc]) $(wildcard */*/*.[hc]) $(wildcard */Makefile) $(wildcard */*/Makefile)
	-etags -a $(wildcard *akefile) $(wildcard */Makefile) $(wildcard */*/Makefile)

ifdef SUBDIRS
tags::
	for sub in $(SUBDIRS); do \
	  (if [ -d $$sub ]; then \
	    echo "Making tags for: $$sub";\
	    cd $$sub; $(MAKE) tags; fi;) \
	done;
endif

##########
# CVS related stuff
##########

update: $(CVS_COMMIT_LOG)
	-$(CVSBIN)/cvs update

ifdef SUBDIRS
add:: 
	echo "Ignore warnings if there are no subdirectories."
	-(for sub in $(SUBDIRS); do \
		if [ -d $$sub/CVS ] ; then echo $$sys already added; \
		else (echo "y" | $(LRD_CVS) add $$sub); fi;\
	done;)
	-(for sub in $(SUBDIRS); do \
	  (if [ -d $$sub ]; then \
	    echo "Making add for: $$sub";\
	    cd $$sub; $(MAKE) add; fi;) \
	done;)
endif

add:: $(SRCS_ALL)
	echo "Ignore warnings about files already present in the archive."
	-($(CVSBIN)/cvs add $(SRCS_ALL))

diff: $(SRCS) $(INCS) $(PRIVATE_INCS) $(CVS_COMMIT_LOG)
	echo "*********************" > /dev/null
	echo "Changes you have made" > /dev/null
	echo "*********************" > /dev/null
	-$(CVSBIN)/cvs diff
	echo "********************************" > /dev/null
	echo "Changes you and others have made" > /dev/null
	echo "********************************" > /dev/null
	-$(CVSBIN)/cvs diff -D now

status: $(SRCS) $(INCS) $(PRIVATE_INCS) $(CVS_COMMIT_LOG)
	echo "*********************" > /dev/null
	echo "Status of your files." > /dev/null
	echo "*********************" > /dev/null
	-$(CVSBIN)/cvs status

UPDATE_VERSION := $(shell ls $(INSTALL_DIR)/etc/updateVersion $(PROJECT_DIR)/etc/updateVersion 2>/dev/null | grep -v "not found" | head -1)

ifdef VERSION_FILE
do_version_update::
	-(if $(CVSBIN)/cvs diff -q>/dev/null; then\
	  echo $(MODULE_DIR) "Version number up to date.";\
	else\
	  $(UPDATE_VERSION) $(INSTALL_DIR) $(MODULE_DIR) $(VERSION_FILE) $(VERSION_PREFIX);\
	  fi)
else
do_version_update::
	echo $(MODULE_DIR) "No version to update."
endif

ifdef SUBDIRS
do_version_update::
	-(for sub in $(SUBDIRS); do \
	  (if [ -d $$sub ]; then \
	    echo "Making version_update for: $$sub";\
	    cd $$sub; $(MAKE) do_version_update; fi;) \
	done;)
endif

do_commit: $(SRCS) $(INCS) $(PRIVATE_INCS) do_version_update
	-cvs -e "emacs-19.28-with-x11 -fg yellow -bg black -font 6x13" commit

#commit: do_commit release depend
commit: do_commit

##########
# Release
##########

# Release hasn't been made to work with apex

#release
#	-cd $(PROJECT_RELEASE_DIR); \
#	$(CVSBIN)/cvs checkout -d $(VERSION_DIR) tca; \
#	cd $(VERSION_DIR); \
#	$(MAKE) -f Makefile depend install



#
# want to have code is a separate directory so we can keep multiple versions.
#

$(ODIR)/%.o: %.c
	$(COMPILE.c) -o $@ $<

$(ODIR)/%.o: %.cc
	$(COMPILE.cc) -o $@ $<

$(BDIR)/%: $(ODIR)/%.o $(LIBDEP)
	$(LINK.c) -o $@ $< $(LIBRARIES)

#
# want to have some output files compressed
#

%: %.gz
	gzip $@


##########
# Exports
##########

export LPATH=:/usr/cs/lib:/usr/lib:/usr/misc/lib
export CPATH=:/usr/misc/include
.EXPORT: LPATH
.EXPORT: CPATH

# other exports, that need to be force evaluated.
export MODULE_DIR := $(MODULE_DIR)
.EXPORT: MODULE_DIR
export SRCS_ALL := $(SRCS_ALL)
.EXPORT: SRCS_ALL

# This should export everything.
.EXPORT_ALL_VARIABLES:

##########
# Include a dependency file, if there is one.
##########

ifneq '$(DEPEND_FILE)' ""
  ifneq '$(wildcard $(DEPEND_FILE))' ""
    include $(DEPEND_FILE)
  else
     MSG_sun4c = "*** Do 'gmake depend' to create a dependency file ***"
  endif
endif



