
# read the standard variable definitions
include $(SUIFHOME)/Makefile.defs

REALSRCS =	$(YSRCS) $(DSRCS) $(HEADERS) $(CSRCS) $(SRCS) \
		$(INFOSRCS) $(MANPAGES) $(GIFSRCS) $(VERDATAFILE) $(TCLSRCS) \
		$(OTHERSRCS)
ALLTMPSRCS =	$(TMP_YSRCS) $(TMP_DSRCS) $(TMP_HEADERS) $(TMP_CSRCS) \
		$(TMP_SRCS) $(TMP_INFOSRCS) $(TMP_MANPAGES) $(TMP_OTHERSRCS)
ALLSRCS =	$(REALSRCS) $(ALLTMPSRCS)
ALLCSRCS =	$(CSRCS) $(TMP_CSRCS)
ALLCXXSRCS =	$(SRCS) $(TMP_SRCS)
ALLHEADERS =	$(HEADERS) $(TMP_HEADERS)

# Why put $(LIBNAME_SET) under .PHONY?  Because at the top of
# Makefile.rules vpath is set to include the library binary directory,
# but we don't want it to find local targets there and not build them
# locally.  Makefile.rules attempts to solve this problem by only
# adding the vpath constructs if LIBS is defined, so it won't be
# invoked on pure libraries.  But this isn't sufficient for sharlit
# and nsharlit because they define both a program and a library.  So
# we use .PHONY to make sure the local libraries are always rebuilt.
#
.PHONY:		$(TARGET) $(LIBNAME_SET) pure prog_ver.cc lib_ver.cc \
		suif_init.cc lib prog info install install-keep-src \
		show-install reinstall reinstall-keep-src show-reinstall \
		reinstall-bin show-reinstall-bin real-reinstall \
		real-reinstall-keep-src deinstall install-local-ver \
		install-ver install-prog install-lib install-bin-base \
		deinstall-bin install-bin-recursive install-bin-all \
		deinstall-src install-src-dir install-local-src \
		install-recursive-src install-src install-incl \
		install-local-incl deinstall-local-incl \
		install-recursive-incl install-info install-local-info \
		deinstall-local-info install-recursive-info preinstall-check \
		install-verdata deinstall-verdata source clean clean-src \
		clean-rcs-links depend sub-packages recursive

include $(SUIFHOME)/Makefile.rules

ifdef BUILD_INFO
STAMP_INFO_TARGET =	stamp-info
endif
ifdef BUILD_PS
STAMP_PS_TARGET =	stamp-ps
endif
ifdef BUILD_HTML
STAMP_HTML_TARGET =	stamp-html
endif
STAMP_DOC_TARGETS =	$(STAMP_INFO_TARGET) $(STAMP_PS_TARGET) \
			$(STAMP_HTML_TARGET)

%.o : %.c $(TMP_HEADERS) $(TMP_CSRCS) FORCE
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

%.o : %.cc $(TMP_HEADERS) $(TMP_SRCS) FORCE
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

$(TARGET): $(TMP_HEADERS) $(TMP_SRCS) $(TMP_CSRCS)
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

pure: $(TMP_HEADERS) $(TMP_SRCS) $(TMP_CSRCS)
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

$(STATIC_LIBNAME): $(TMP_HEADERS) $(TMP_SRCS) $(TMP_CSRCS)
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

$(SHARED_LIBNAME): $(TMP_HEADERS) $(TMP_SRCS) $(TMP_CSRCS)
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

prog_ver.cc: $(TMP_HEADERS) $(TMP_SRCS) $(TMP_CSRCS)
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

lib_ver.cc: $(TMP_HEADERS) $(TMP_SRCS) $(TMP_CSRCS)
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

suif_init.cc: $(TMP_HEADERS) $(TMP_SRCS) $(TMP_CSRCS)
		@$(MAKE) --no-print-directory -f $(SUIFHOME)/Makefile.sub $@

lib:		$(LIBNAME_SET)

prog:		$(TARGET_EXECUTABLES)

info:		stamp-info

ps:		stamp-ps

html:		stamp-html

all-docs:	stamp-info stamp-ps stamp-html

stamp-info:	$(INFOSRCS)
ifdef INFOS
		for i in $(INFOS) ; do \
			makeinfo $$i.texi ; \
		done
endif
		@touch stamp-info

ifdef INFOS
stamp-ps:	$(INFOSRCS) suif_logo.epsf
		for i in $(INFOS) ; do \
			tex $$i.texi ; \
			texindex $$i.cp $$i.fn $$i.ky $$i.pg $$i.tp $$i.vr ; \
			tex $$i.texi ; \
			dvips -o $$i.ps $$i.dvi ; \
		done
else
stamp-ps:	$(INFOSRCS)
endif
		@touch stamp-ps

stamp-html:	$(INFOSRCS)
ifdef INFOS
		for i in $(INFOS) ; do \
			texi2html -split_node -menu $$i.texi ; \
		done
endif
		@touch stamp-html

current:
		@if test -d RCS ; then \
			$(MAKE) --no-print-directory \
				VERSION=`ver_find_by_attribute current` ; \
		else \
			$(MAKE) --no-print-directory ; \
		fi

show-install:
		@$(MAKE) preinstall-check
		@$(MAKE) deinstall-src 1>> $(LOGFILE) 2>&1
		@$(MAKE) install-src-dir 1>> $(LOGFILE) 2>&1
ifdef SUB_PACKAGES
		@SILENT_RECURSIVE_MAKE=yes suif-recursive-make $@ 2> \
			/dev/null | \
			grep -v 'Entering directory' | \
			grep -v 'Leaving directory'
endif
		@if $(MAKE) SUB_PACKAGES= install-keep-src 1>> $(LOGFILE) \
				2>&1; then \
			echo $(PACKAGE_FULL_NAME)" ... OK"; \
		else \
			echo $(PACKAGE_FULL_NAME)" ... FAILED -- see "$(PACKAGE_FULL_NAME)/$(LOGFILE); \
		fi;
		@$(MAKE) install-verdata 1>> $(LOGFILE) 2>&1

install:	install-ver reinstall

install-keep-src:	install-ver reinstall-keep-src

show-reinstall:
		@$(MAKE) deinstall-verdata 1> $(LOGFILE) 2>&1
		@$(MAKE) preinstall-check
		@$(MAKE) deinstall-src 1>> $(LOGFILE) 2>&1
		@$(MAKE) install-src-dir 1>> $(LOGFILE) 2>&1
ifdef SUB_PACKAGES
		@SILENT_RECURSIVE_MAKE=yes suif-recursive-make $@ 2> \
			/dev/null | \
			grep -v 'Entering directory' | \
			grep -v 'Leaving directory'
endif
		@if $(MAKE) SUB_PACKAGES= reinstall-keep-src 1>> $(LOGFILE) \
				2>&1; then \
			echo $(PACKAGE_FULL_NAME)" ... OK"; \
		else \
			echo $(PACKAGE_FULL_NAME)" ... FAILED -- see "$(PACKAGE_FULL_NAME)/$(LOGFILE); \
		fi;
		@$(MAKE) install-verdata 1>> $(LOGFILE) 2>&1

reinstall:
		@if test -d RCS ; then \
			if test $(VERSION)"" = "" ; then \
				$(MAKE) --no-print-directory real-reinstall \
				    VERSION=`ver_find_by_attribute current` ; \
			else \
				$(MAKE) --no-print-directory real-reinstall \
					VERSION=$(VERSION) ; \
			fi ; \
		else \
			$(MAKE) --no-print-directory real-reinstall ; \
		fi

reinstall-keep-src:
		@if test -d RCS ; then \
			if test $(VERSION)"" = "" ; then \
				$(MAKE) --no-print-directory \
				    real-reinstall-keep-src \
				    VERSION=`ver_find_by_attribute current` ; \
			else \
				$(MAKE) --no-print-directory \
				    real-reinstall-keep-src \
				    VERSION=$(VERSION) ; \
			fi ; \
		else \
			$(MAKE) --no-print-directory \
				real-reinstall-keep-src ; \
		fi

show-reinstall-bin:
ifdef SUB_PACKAGES
		@SILENT_RECURSIVE_MAKE=yes suif-recursive-make $@ 2> \
			/dev/null | \
			grep -v 'Entering directory' | \
			grep -v 'Leaving directory'
endif
		@if $(MAKE) SUB_PACKAGES= reinstall-bin 1> $(LOGFILE) \
				2>&1; then \
			echo $(PACKAGE_FULL_NAME)" ... OK"; \
		else \
			echo $(PACKAGE_FULL_NAME)" ... FAILED -- see "$(PACKAGE_FULL_NAME)/$(LOGFILE); \
		fi;

reinstall-bin:
		@if test -d RCS ; then \
			if test $(VERSION)"" = "" ; then \
				$(MAKE) --no-print-directory install-bin-all \
				    VERSION=`ver_find_by_attribute current` ; \
			else \
				$(MAKE) --no-print-directory install-bin-all \
					VERSION=$(VERSION) ; \
			fi ; \
		else \
			$(MAKE) --no-print-directory install-bin-all ; \
		fi

real-reinstall:	deinstall-verdata preinstall-check deinstall-src \
		real-reinstall-keep-src install-verdata

real-reinstall-keep-src:	install-bin install-src-dir install-local-src \
		install-local-incl install-local-info
ifdef SUB_PACKAGES
		suif-recursive-make real-reinstall-keep-src
endif

deinstall:	deinstall-verdata deinstall-src deinstall-bin \
		deinstall-local-incl deinstall-local-info
ifdef SUB_PACKAGES
		suif-recursive-make deinstall
endif

install-local-ver:
		@if test -d RCS ; then \
			ver_assert_ver_does_not_exist $(VERSION) ; \
			ver_build_current $(VERSION) ; \
		else \
			echo > /dev/null ; \
		fi

install-ver:	install-local-ver
		@if test ! ""$(SUPER_PACKAGES_PATH) = "" ; then \
			owner_package=`echo $(SUPER_PACKAGES_PATH) | \
				sed 's/.*\///'`; \
			if test -d RCS/../../$$owner_package ; then \
				mkdir $$owner_package ; \
				(cd $$owner_package ; \
				 ln -s ../RCS/../../$$owner_package RCS ; \
				 $(CO) $(COFLAGS) Makefile ; \
				 export PACKAGE_NAME ; \
				 export VERDATAFILE ; \
				 export OTHERSRCS ; \
				 export SUB_PACKAGES ; \
				 export INFOS ; \
				 export INFOSRCS ; \
				 export YSRCS ; \
				 export DSRCS ; \
				 export HEADERS ; \
				 export CSRCS ; \
				 export SRCS ; \
				 export MANPAGES ; \
				 export GIFSRCS ; \
				 export TMP_YSRCS ; \
				 export TMP_DSRCS ; \
				 export TMP_HEADERS ; \
				 export TMP_CSRCS ; \
				 export TMP_SRCS ; \
				 export TMP_INFOSRCS ; \
				 export TMP_MANPAGES ; \
				 export TMP_OTHERSRCS ; \
				 export TARGET ; \
				 export SUPER_PACKAGES_PATH ; \
				 export SUB_PACKAGES ; \
				 export TCLSRCS ; \
				 PACKAGE_NAME= ; \
				 VERDATAFILE= ; \
				 OTHERSRCS= ; \
				 SUB_PACKAGES= ; \
				 INFOS= ; \
				 INFOSRCS= ; \
				 YSRCS= ; \
				 DSRCS= ; \
				 HEADERS= ; \
				 CSRCS= ; \
				 SRCS= ; \
				 MANPAGES= ; \
				 GIFSRCS= ; \
				 TMP_YSRCS= ; \
				 TMP_DSRCS= ; \
				 TMP_HEADERS= ; \
				 TMP_CSRCS= ; \
				 TMP_SRCS= ; \
				 TMP_INFOSRCS= ; \
				 TMP_MANPAGES= ; \
				 TMP_OTHERSRCS= ; \
				 TARGET= ; \
				 SUPER_PACKAGES_PATH= ; \
				 SUB_PACKAGES= ; \
				 TCLSRCS= ; \
				 $(MAKE) install-ver ; \
				) ; \
				rm -rf $$owner_package ; \
			fi ; \
		fi

install-prog:	$(TARGET_EXECUTABLES) install-bin-base
		for target in $(TARGET_EXECUTABLES); do \
			suif-install $$target $(BINDIR) 775 ; \
		done

install-lib:	$(LIBNAME_SET) install-bin-base
ifeq ($(INSTALL_SHARED_LIB),true)
		suif-install $(SHARED_LIBNAME) $(SODIR) 444
endif
ifeq ($(INSTALL_STATIC_LIB),true)
		suif-install $(STATIC_LIBNAME) $(LIBDIR) 444
endif
		rm -f $(AUTOINITDIR)/$(TARGET)
ifdef AUTOINITLIB
		touch $(AUTOINITDIR)/$(TARGET)
endif

install-bin-base: $(TCLSRCS)
ifdef TCLSRCS
		for target in $(TCLSRCS); do \
			suif-install $$target $(TCLDIR) 444 ; \
		done
endif

deinstall-bin:
		for target in $(TARGET_EXECUTABLES); do \
			rm -f $(BINDIR)/$$target ; \
		done
		rm -f $(LIBDIR)/$(STATIC_LIBNAME)
		rm -f $(SODIR)/$(SHARED_LIBNAME)
		rm -f $(AUTOINITDIR)/$(TARGET)

install-bin-recursive:
ifdef SUB_PACKAGES
		suif-recursive-make install-bin-all
endif

install-bin-all: install-bin install-bin-recursive

install-src:	deinstall-src install-src-dir install-local-src \
		install-recursive-src

deinstall-src:
		if test -d RCS ; then \
			rm -rf $(SRCDIR)/$(PACKAGE_FULL_NAME) ; \
		else : ; \
		fi

install-src-dir:
		if test -d RCS ; then \
			mkdir $(SRCDIR)/$(PACKAGE_FULL_NAME) ; \
			chmod 775 $(SRCDIR)/$(PACKAGE_FULL_NAME) ; \
		else : ; \
		fi

install-local-src:	$(REALSRCS)
		if test -d RCS ; then \
			cp $(REALSRCS) Makefile $(SRCDIR)/$(PACKAGE_FULL_NAME) ; \
			for i in $(SRCDIR)/$(PACKAGE_FULL_NAME)/* ; do \
				if test -f $${i} ; then \
					chmod 444 $${i} ; \
				else \
					true ; \
				fi ; \
			done ; \
		else : ; \
		fi

install-recursive-src:
ifdef SUB_PACKAGES
		suif-recursive-make install-src
endif

install-incl:	install-local-incl install-recursive-incl

install-local-incl:	$(EXPORTS)
ifdef EXPORTS
		rm -rf $(INCLDIR)/$(PACKAGE_NAME)
		mkdir $(INCLDIR)/$(PACKAGE_NAME)
		for i in $(ALLHEADERS) ; do \
			rm -f $(INCLDIR)/$(PACKAGE_NAME)/$$i ; \
			suif-install $$i $(INCLDIR)/$(PACKAGE_NAME) 444 ; \
		done
		for i in $(EXPORTS) ; do \
			rm -f $(INCLDIR)/$$i ; \
			suif-install $$i $(INCLDIR) 444 ; \
		done
endif

deinstall-local-incl:
ifdef EXPORTS
		rm -rf $(INCLDIR)/$(PACKAGE_NAME)
		for i in $(EXPORTS) ; do \
			rm -f $(INCLDIR)/$$i ; \
		done
endif

install-recursive-incl:
ifdef SUB_PACKAGES
		suif-recursive-make install-incl
endif

install-info:	install-local-info install-recursive-info

install-local-info:	$(STAMP_DOC_TARGETS) $(MANPAGES) $(GIFSRCS)
ifdef INFOS
ifdef BUILD_INFO
		for i in $(INFOS) ; do \
			rm -f $(INFODIR)/$$i.info* ; \
			cp $$i.info* $(INFODIR) ; \
			chmod 444 $(INFODIR)/$$i.info* ; \
		done
endif
ifdef BUILD_PS
		for i in $(INFOS) ; do \
			suif-install $$i.ps $(DOCDIR) 444 ; \
		done
endif
ifdef BUILD_HTML
ifdef GIFSRCS
		for i in $(GIFSRCS) ; do \
			rm -f $(HTMLDIR)/$$i ; \
			cp $$i $(HTMLDIR) ; \
			chmod 444 $(HTMLDIR)/$$i ; \
		done
endif
		for i in $(INFOS) ; do \
			rm -f $(HTMLDIR)/$$i*.html ; \
			cp $$i*.html $(HTMLDIR) ; \
			chmod 444 $(HTMLDIR)/$$i*.html ; \
		done
endif
endif
ifdef MANPAGES
		for i in $(MANPAGES) ; do \
			MANSUFFIX=`echo $$i | sed 's/.*\.//'` ; \
			MANSUFDIR=$(MANDIR)/man$$MANSUFFIX ; \
			if test ! -d $$MANSUFDIR; then \
				mkdir $$MANSUFDIR; \
			else \
				: ; \
			fi ; \
			suif-install $$i $$MANSUFDIR 444 ; \
		done
ifdef BUILD_HTML
		for i in $(MANPAGES) ; do \
			rm -f $(HTMLDIR)/man_$$i.html ; \
			nroff -man $$i | rman -n $$i -f HTML -r "man_%s.%s.html" -l "Man page for %s" > $(HTMLDIR)/man_$$i.html ; \
		done
endif
endif

deinstall-local-info:
ifdef INFOS
		for i in $(INFOS) ; do \
			rm -f $(INFODIR)/$$i.info* ; \
		done
		for i in $(INFOS) ; do \
			rm -f $(DOCDIR)/$$i.ps ; \
		done
		for i in $(GIFSRCS) ; do \
			rm -f $(HTMLDIR)/$$i ; \
		done
		for i in $(INFOS) ; do \
			rm -f $(HTMLDIR)/$$i*.html ; \
		done
endif
ifdef MANPAGES
		for i in $(MANPAGES) ; do \
			rm -f $(MANDIR)/$$i ; \
		done
		for i in $(MANPAGES) ; do \
			rm -f $(HTMLDIR)/man_$$i.html ; \
		done
endif

install-recursive-info:
ifdef SUB_PACKAGES
		suif-recursive-make install-info
endif

preinstall-check: $(VERDATAFILE)
ifdef VERDATAFILE
ifneq (,$(VERDATAFILE))
		@if test -f $(BINDIR)/ververver ; then \
			ververver -check $(PACKAGE_NAME) $(VERDATAFILE) \
				$(VERDATADIR) ; \
		else \
			echo > /dev/null ; \
		fi
else
		@echo > /dev/null
endif
else
		@echo > /dev/null
endif

install-verdata:
ifdef VERDATAFILE
ifneq (,$(VERDATAFILE))
		@ververver -install $(PACKAGE_NAME) $(VERDATAFILE) \
			$(VERDATADIR)
endif
endif

deinstall-verdata:
ifdef VERDATAFILE
ifneq (,$(VERDATAFILE))
		@if test -f $(BINDIR)/ververver ; then \
			ververver -deinstall $(PACKAGE_NAME) $(VERDATAFILE) \
				$(VERDATADIR) ; \
		else \
			echo > /dev/null ; \
		fi
endif
endif

source:		$(REALSRCS)

clean:		clean-src
		rm -f core $(OBJS) $(TARGET) $(STATIC_LIBNAME)
		rm -f $(SHARED_LIBNAME) $(ALLTMPSRCS)
		rm -f Makefile.deps
		rm -f versions.ver
		rm -f prog_ver.cc prog_ver.o
		rm -f lib_ver.cc lib_ver.o
		rm -f suif_init.cc suif_init.o
ifneq ($(PACKAGE_NAME),suif)
		rm -f suif_logo.epsf
endif
ifdef INFOS
		for i in $(INFOS) ; do \
			rm -f $$i.cp $$i.fn $$i.ky $$i.pg $$i.tp $$i.vr ; \
			rm -f $$i.cps $$i.fns $$i.kys $$i.pgs $$i.tps ; \
			rm -f $$i.vrs $$i.toc $$i.info* $$i*.html ; \
			rm -f $$i.dvi $$i.ps $$i.aux $$i.log ; \
		done
endif
		rm -f stamp-info
		rm -f stamp-ps
		rm -f stamp-html
ifdef SUB_PACKAGES
		suif-recursive-make clean
		for i in $(SUB_PACKAGES) ; do \
			if test -d $$i/RCS ; then \
				rm -f $$i/Makefile ; \
				rm -f $$i/RCS ; \
				rmdir $$i 2>&1 > /dev/null ; \
			fi; \
		done ; exit 0
endif

clean-src:
		if test -d RCS ; then \
			find $(ALLSRCS) \
				\( -perm 444 -o -perm 555 -o -type l \) \
				-exec rm -f {} \; 1> /dev/null 2>&1 ; \
			rm -f *~ ; \
			rm -f stamp-version ; \
		else : ; \
		fi

clean-rcs-links:
ifdef SUB_PACKAGES
		suif-recursive-make clean-rcs-links
endif
		if test -d RCS ; then \
			rm RCS > /dev/null 2>&1 ; \
			echo > /dev/null 2>&1 ; \
		else : ; \
		fi

depend:		$(ALLCSRCS) $(ALLCXXSRCS) $(ALLHEADERS)
		@rm -f Makefile.deps
		@echo > Makefile.deps
ifneq ($(strip $(ALLCXXSRCS)),)
		@echo '# Dependencies for C++ files' >> Makefile.deps
		@echo >> Makefile.deps
		$(CXX) $(DEPSFLAG) $(CXXFLAGS) $(ALLCXXSRCS) >> Makefile.deps
		@echo >> Makefile.deps
endif
ifneq ($(strip $(ALLCSRCS)),)
		@echo '# Dependencies for C files' >> Makefile.deps
		@echo >> Makefile.deps
		$(CC) $(DEPSFLAG) $(CFLAGS) $(ALLCSRCS) >> Makefile.deps
		@echo >> Makefile.deps
endif

sub-packages:
		@echo $(SUB_PACKAGES)

recursive:
ifdef SUB_PACKAGES
		suif-recursive-make
endif

ifneq ($(PACKAGE_NAME),suif)
suif_logo.epsf:
		rm -f suif_logo.epsf
		ln -s $(SUIFHOME)/include/suif_logo.epsf .
endif
