#
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}

# Get rid of a bunch of nasty built-in implicit rules,
# to avoid bogus circular dependencies and make things go faster.
# Use the `-r' command line option to make to get even better performance.
.SUFFIXES:

#
# Various install targets.
# 
$(INSTALL_BINDIR)/%: %
	@echo "Installing $<"
	-mkdir -p $(INSTALL_BINDIR)
	$(INSTALL_PROGRAM) $< $@

$(INSTALL_SBINDIR)/%: %
	@echo "Installing $<"
	-mkdir -p $(INSTALL_SBINDIR)
	$(INSTALL_PROGRAM) $< $@

$(INSTALL_LIBDIR)/%: %
	@echo "Installing $<"
	-mkdir -p $(INSTALL_LIBDIR)
	$(INSTALL_PROGRAM) $< $@

$(INSTALL_LIBEXECDIR)/%: %
	@echo "Installing $<"
	-mkdir -p $(INSTALL_LIBEXECDIR)
	$(INSTALL_PROGRAM) $< $@

$(INSTALL_ETCDIR)/%: %
	@echo "Installing $<"
	-mkdir -p $(INSTALL_ETCDIR)
	$(INSTALL_DATA) $< $@

$(addprefix $(INSTALL_SBINDIR)/, $(SETUID_SBIN_SCRIPTS)): $(INSTALL_SBINDIR)/%: %
	echo "Installing (setuid) $<"
	-mkdir -p $(INSTALL_SBINDIR)
	$(SUDO) $(INSTALL) -o root -m 4755 $< $@

$(addprefix $(INSTALL_BINDIR)/, $(SETUID_BIN_SCRIPTS)): $(INSTALL_BINDIR)/%: %
	echo "Installing (setuid) $<"
	-mkdir -p $(INSTALL_BINDIR)
	$(SUDO) $(INSTALL) -o root -m 4755 $< $@

$(addprefix $(INSTALL_LIBEXECDIR)/, $(SETUID_LIBX_SCRIPTS)): $(INSTALL_LIBEXECDIR)/%: %
	echo "Installing (setuid) $<"
	-mkdir -p $(INSTALL_LIBEXECDIR)
	$(SUDO) $(INSTALL) -o root -m 4755 $< $@


CFLAGS += $(LOCALCFLAGS)

clean:

distclean: clean
	rm -f GNUmakefile

#
# Where to find source files.
# Using specific patterns instead of the catch-all VPATH variable
# avoids hitting miscellaneous wrong targets in other directories
# when configuring in the source directory.
#
vpath %.in  $(SRCDIR) $(MOSTLY_SRCDIRS)
vpath %.c   $(SRCDIR) $(MOSTLY_SRCDIRS)
vpath %.cc  $(SRCDIR) $(MOSTLY_SRCDIRS)
vpath %     $(SRCDIR) $(MOSTLY_SRCDIRS)

#
# Define rules which we blew away with .SUFFIXES
# 
%.o: %.c
	$(CC) -c -o $@ $(CFLAGS) $<

%.o: %.cc
	$(CXX) -c -o $@ $(CXXFLAGS) $<

%.o: %.C
	$(CXX) -c -o $@ $(CXXFLAGS) $<

%_xdr.h: %.x
	rpcgen -h $< > $@

%_xdr.c: %.x %_xdr.h
	rpcgen -c $< > $@

%: %-debug
	$(STRIP) -o $@$(EXE) $<$(EXE)

%-version.c:
	echo >$@ "char build_info[] = \"Built on `date` by `id -nu`@`hostname | sed 's/\..*//'`:`pwd`\";"

#
# Rules to regen configure written files.
#
%: %.in $(OBJDIR)/config.status
	@echo "Regenerating $@"
	cd $(OBJDIR) && CONFIG_FILES=$(SUBDIR)/$@ ./config.status

ifeq ($(TESTS),)
.PHONY: check
else
check: $(TESTS)
	@export SRCDIR=${SRCDIR}; for tst in $^; do \
		if test `basename $$tst .sh` != `basename $$tst`; then \
			tst="sh $$tst"; \
		fi; \
		if $$tst; then \
			echo "PASS: $$tst"; \
		else \
			echo "FAIL: $$tst"; \
			exit 1; \
		fi \
	done
endif

